我有一个UIViewController
,其中UIImageView
和UIImage
数组。如何向右或向左开发幻灯片功能以将图像更改为下一个阵列位置?
答案 0 :(得分:8)
常见的方法是UIScrollView,其中一个图像的大小,启用了分页。
将图像添加为子视图,并像这样设置内容大小......
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
}
您可能还会根据所需效果的详细信息关闭反弹,滚动指示器等。
答案 1 :(得分:2)
通过使用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;
}
答案 2 :(得分:1)
您只需使用此{/ 1> UISwipeGestureRecognizer
向左或向右滑动方向即可
swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];
方法detectSwipe
声明用于处理前向和后向以使用UIImage数组。您还可以查看Apple's SimpleGestureRecognizers Demo
希望它可以帮到你!