How to change image from UIImageView by moving left or right of UIViewController

I have UIViewControllerwith UIImageViewit, and an array UIImage. How to create a slide function on the right or left to change the image to the next position of the array?

+5
source share
3 answers

A common approach is UIScrollView the size of one of the images, with support for paging.

Add images as subzones and set the content size as follows:

NSArray *images;
CGSize imageSize;

self.scrollView.frame = CGRectMake(10,10,imageSize.width,imageSize.height);
self.scrollView.contentSize = CGSizeMake(imageSize.width * images.count, imageSize.height);
self.scrollView.pagingEnabled = YES;

CGFloat xPos = 0.0;

for (UIImage *image in images) {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.width);
    [self.scrollView addSubview:imageView];
    xPos += imageSize.width;
    // assuming ARC, otherwise release imageView
}

You can also disable bounce, scroll indicators, etc. depending on the details of the desired effect.

+8
source

UIScrollView UIPageControl, n drop UIScrollView UIPageControl , , IBoutLet . Set

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1
imageArray = [NSArray arrayWithObjects:
                     [UIImage imageNamed:@"image2.jpg"],
                     [UIImage imageNamed:@"man-actor-Ashton-Kutcher-Wallpaper.jpg"],
                     [UIImage imageNamed:@"image1.jpg"],
                     [UIImage imageNamed:@"man-curl.jpg"],
                     [UIImage imageNamed:@"man-in-suit.jpg"],
                     nil];


  for (int i = 0; i < imageArray.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = [imageArray objectAtIndex:i];
    subview.contentMode = UIViewContentModeScaleAspectFit;
    [self.scrollView addSubview:subview];
  }

  self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height);
  pageControl.numberOfPages = imageArray.count;
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    // Update the page when more than 50% of the previous/next page is visible
  CGFloat pageWidth = self.scrollView.frame.size.width;
  int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  self.pageControl.currentPage = page;
}
+2

You can simply get the direction of movement left or right using UISwipeGestureRecognizer, like this

swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];

The method detectSwipedeclares to control your forward and backward to play with the UIImage array. You can also check out the Apple SimpleGestureRecognizers demo.

Hope this helps you!

+1
source

All Articles