[英]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
. 我有一个带有
UIImageView
的UIViewController
和一个UIImage
数组。 How Can I develop slide feature to right or left in order to change the image to the next array position? 如何将幻灯片功能向右或向左开发以将图像更改为下一个阵列位置?
The common approach is a UIScrollView the size of one of the images, with paging enabled. 常见的方法是UIScrollView,其中一个图像的大小,启用了分页。
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. 您也可以根据所需效果的详细信息关闭反弹,滚动指示器等。
By using UIScrollView and UIPageControl, drag n drop UIScrollView and UIPageControl make sure they does not overlap,create IBoutLet of both.Set 通过使用UIScrollView和UIPageControl,拖放UIScrollView和UIPageControl确保它们不重叠,创建两者的IBoutLet。
- (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;
}
You can simply get swipe direction is left or right with UISwipeGestureRecognizer
like this 你可以像
UISwipeGestureRecognizer
一样向左或向右滑动方向
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. 方法
detectSwipe
声明用于处理你的前进和后退以使用UIImage数组。 You can also check the Apple's SimpleGestureRecognizers Demo 您还可以查看Apple的SimpleGestureRecognizers演示
Hope it helps you ! 希望它能帮到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.