iOS图片浏览器(UIScrollView + UIPageCo
2015-07-27 本文已影响1038人
MWY
UIScrollView
- (void)setUpScrollView {
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = self.scrollView.bounds.size.height;
for (NSInteger i = 0; i < [self.imagArr count]; i++) {
UIImage *image = [UIImage imageNamed:self.imagArr[i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(i * width, 0, width, height)];
[self.scrollView addSubview:imageView];
}
}
将图片添加到UIScrollView
!
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imagArr = @[@"1.jpg", @"2.jpg", @"3.jpg"];
[self setUpScrollView];
//设置总体的宽度
[self.scrollView setContentSize:CGSizeMake([self.imagArr count] * self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)];
[self.scrollView setPagingEnabled:YES];
self.scrollView.bounces = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.pagecontoll = [[UIPageControl alloc] init];
self.pagecontoll.backgroundColor = [UIColor clearColor];
self.pagecontoll.bounds = CGRectMake(0, 0, 200, 100);
self.pagecontoll.center = CGPointMake(self.view.bounds.size.width / 2, self.scrollView.bounds.size.height);
self.pagecontoll.numberOfPages = [self.imagArr count];
self.pagecontoll.currentPage = 0;
self.scrollView.delegate = self;
[self.pagecontoll addTarget:self action:@selector(switchPage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.pagecontoll];
}
通过UIPageControl改变UIScrollView
- (void)switchPage:(id)sender {
UIPageControl *currentControl = (UIPageControl *)sender;
NSInteger currentPage = currentControl.currentPage;
_scrollView.contentOffset = CGPointMake(currentPage * self.view.bounds.size.width, 0);
}
通过UIScrollView改变UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger currentPage = scrollView.contentOffset.x / self.view.bounds.size.width;
self.pagecontoll.currentPage = currentPage;
}