Xcode:如何使用滑动手势更改 UIPageControl 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16391048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode: How do I change UIPageControl value with swipe gesture?
提问by Robert Saunders
I have a quick question I was hoping you guys could help me answer. Right now I have a UIPageControl in a storyboard which changes an image depending on what dot you are on, however, as of now you have to press on the dot to change through the dots/images, how can I change through the images/dots by swiping?
我有一个简单的问题,我希望你们能帮我回答。现在我在情节提要中有一个 UIPageControl,它会根据您所在的点更改图像,但是,到目前为止,您必须按下点以更改点/图像,我如何更改图像/点通过刷卡?
Here is my code for my .h
这是我的 .h 代码
#import <UIKit/UIKit.h>
@interface PageViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *dssview;
- (IBAction)changephoto:(UIPageControl *)sender;
@end
Here is my code for my .m
这是我的 .m 代码
#import "PageViewController.h"
@interface PageViewController ()
@end
@implementation PageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)changephoto:(UIPageControl *)sender {
_dssview.image = [UIImage imageNamed:
[NSString stringWithFormat:@"%d.jpg",sender.currentPage+1]];
}
@end
Any help would be greatly appreciated. Thanks
任何帮助将不胜感激。谢谢
回答by Prasad Devadiga
You can add UISwipeGestureRecognizer to your view and in the selector method of UISwipeGestureRecognizer based on the direction update the UIPageControl object, either increment the current page or decrement.
您可以将 UISwipeGestureRecognizer 添加到您的视图中,并在 UISwipeGestureRecognizer 的选择器方法中根据方向更新 UIPageControl 对象,增加当前页面或减少。
You can refer the below code. Adding swipe gesture to the view controller
你可以参考下面的代码。向视图控制器添加滑动手势
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
Swipe gesture selector
滑动手势选择器
- (void)swipe:(UISwipeGestureRecognizer *)swipeRecogniser
{
if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionLeft)
{
self.pageControl.currentPage -=1;
}
else if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionRight)
{
self.pageControl.currentPage +=1;
}
_dssview.image = [UIImage imageNamed:
[NSString stringWithFormat:@"%d.jpg",self.pageControl.currentPage]];
}
Add an outlet to the UIPageControl in the .h file
在 .h 文件中的 UIPageControl 中添加一个插座
@interface PageViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *dssview;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
- (IBAction)changephoto:(UIPageControl *)sender;
@end