I have 3 minor issues related to implementing a UIPageControl. I'm not using a UIPageViewController, so I would not like to use that, if possible.
I have implemented a UIPageControl, where, when a user swipes either left or right on the view, it will move to the next "page" and display a different image.
Here is the following code:
ViewController.h:
@property (nonatomic) NSInteger Count;
@property (strong, nonatomic) IBOutlet UIPageControl *Control;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) UISwipeGestureRecognizer *swipeRight, *swipeLeft;
@property (strong, nonatomic) NSArray *array;
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.array = @[@"page1", @"page2", @"page3", @"page4"];
self.swipeRight = [ [UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(Right:)];
self.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.swipeRight];
self.swipeLeft = [ [UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(Left:)];
self.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:self.swipeLeft];
self.Control.numberOfPages = 3;
self.Control.currentPage = 0;
}
-(void)Right: (UITapGestureRecognizer *)sender
{
self.Count = self.Count - 1;
if (self.Count > 3)
{
self.Count = 1;
}
else if (self.Count < 1)
{
self.Count = 3;
}
if (self.Count == 1)
{
self.Control.currentPage = 0;
self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
// self.label.text = @"this is page 1";
}
else if (self.Count == 2)
{
self.Control.currentPage = 1;
self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
// self.label.text = @"this is page 2";
}
else if (self.Count == 3)
{
self.Control.currentPage = 2;
self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
// self.label.text = @"this is page 3";
}
}
-(void)Left: (UITapGestureRecognizer *)sender
{
self.Count = self.Count + 1;
if (self.Count > 3)
{
self.Count = 1;
}
else if (self.Count < 1)
{
self.Count = 3;
}
if (self.Count == 1)
{
self.Control.currentPage = 0;
self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
//self.label.text = @"this is page 1";
}
else if (self.Count == 2)
{
self.Control.currentPage = 1;
self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
//self.label.text = @"this is page 2";
}
else if (self.Count == 3)
{
self.Control.currentPage = 2;
self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:self.Control.currentPage]];
// self.label.text = @"this is page 3";
}
}
The first issue: how do I make the view display the first image at index 0 on launch?
Right now, if you start the app initially, the image is blank on screen even though the page control shows the current view as being highlighted with the little dot. You have to swipe left to get the first image in index 0 to display.
The second issue: how do I add a scroll transition style animation when swiped left or right?
Right now, if you swipe either left or right, the image transition is instant, and you don't see that swiping animation where one image pushes the other image out of the view.
The third issue: how do I make the swipe gesture recognition not be the whole view area, but only be swipeable if you swipe on the upper portion of the view?
Feel free to download the project code as it's just a template that I'm using: project code
Thanks