Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change image from UIImageView by sliding right or left from UIViewController

I have a UIViewController with a UIImageView in it and a array of UIImage. How Can I develop slide feature to right or left in order to change the image to the next array position?

like image 343
AmandaSai98b Avatar asked May 06 '12 01:05

AmandaSai98b


People also ask

What is a UIImageView object?

An object that displays a single image or a sequence of animated images in your interface. Image views let you efficiently draw any image that can be specified using a UIImage object. For example, you can use the UIImageView class to display the contents of many standard image files, such as JPEG and PNG files.

What is contentmode in UIImageView in iOS?

Images are an essential component of an app. We use them everywhere, and you might need a different position and size for each place. UIImageView uses its contentMode property to control these behaviors. UIKit offer thirteen different mode for contentMode for us to work with.

Why does the background of an image show up in uiimage?

Any transparency in the image allows the image view’s background to show through. Similarly, any further transparency in the background of the image is dependent on the transparency of the image view and the transparency of the UIImage object it displays.

What is UIView contentmode scaletofill?

The UIView.ContentMode.scaleToFill value scales the image without regard to the original aspect ratio, which can cause the image to appear distorted. Other content modes place the image at the appropriate location in the image view’s bounds without scaling it.


3 Answers

The common approach is a UIScrollView the size of one of the images, with paging enabled.

Add the images as subviews and set the content size like this...

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 might also turn off bounce, scrolling indicators, etc. depending on the details of the effect you want.

like image 76
danh Avatar answered Oct 21 '22 22:10

danh


By using UIScrollView and UIPageControl, drag n drop UIScrollView and UIPageControl make sure they does not overlap,create IBoutLet of both.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;
}
like image 35
Amol Nikam Avatar answered Oct 21 '22 20:10

Amol Nikam


You can simply get swipe direction is left or right with UISwipeGestureRecognizer like this

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

The Method detectSwipe declare for handle your forward and backward to play with UIImage array. You can also check the Apple's SimpleGestureRecognizers Demo

Hope it helps you !

like image 31
Sakares Avatar answered Oct 21 '22 21:10

Sakares