Xcode: How to change the value of a UIPageControl using swipe gestures?

I have a quick question that I was hoping you guys would help me answer. Right now I have a UIPageControl in the storyboard that changes the image depending on which point you are on, however now you need to click on the point to change it through the points / images, how can I change the image / points by scrolling?

Here is my code for my .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

 #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

+6
source share
1 answer

You can add a UISwipeGestureRecognizer to your view and in the selection method of the UISwipeGestureRecognizer based on the direction that the UIPageControl object updates, either increase the current page or decrease it.

You can link to the code below. Adding swipe gestures 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]; 

Scroll 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 output to UIPageControl in .h file

 @interface PageViewController : UIViewController @property (strong, nonatomic) IBOutlet UIImageView *dssview; @property (strong, nonatomic) IBOutlet UIPageControl *pageControl; - (IBAction)changephoto:(UIPageControl *)sender; @end 
+15
source

Source: https://habr.com/ru/post/944333/


All Articles