一、UIScrollView
1.contentSize:UIScrollView可滚动的范围
[self.scroll setContentSize:CGSizeMake((KNumberOfPages + 2) * imageWidth, imageHeight)];
2.contentOffset:UIScrollView原点在contentSize上的位置
self.scroll.contentOffset = CGPointMake(imageWidth, 0);
3.contentInset:UIScrollView四周的扩展范围
4.PagingEnabled:是否有分页效果
[self.scroll setPagingEnabled:YES];
5.scrollEnabled:是否有滚动效果
[self.scroll setScrollEnabled:YES];
5.showsHorizontalScrollIndicator:是否显示水平滚动条
[self.scroll setShowsHorizontalScrollIndicator:YES];
6.bounces:是否有弹簧效果
[self.scroll setBounces:NO];
7.缩放:1.设置代理<UIScrollViewDelegate>
2.设置最大、最小缩放比例
self.myScrollerView.minimumZoomScale = 0.5; self.myScrollerView.maximumZoomScale = 2;
3.实现缩放代理方法,指定缩放的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{}
二、UIPageControl
注:一定要用initWithFrame初始化,不然不能换页
1.numberOfPages设置页数
[self.pageControl setNumberOfPages:KNumberOfPages];
2.currentPage设置当前页
[self.pageControl setCurrentPage:0];
3.pageIndicatorTintColor设置普通页指示器的颜色
[self.pageControl setPageIndicatorTintColor:[UIColor blackColor]];
4.currentPageIndicatorTintColor设置当前页指示器的颜色
小例子:实现5张图片的循环滚动
#define KNumberOfPages 5 @interface ViewController () @property (nonatomic, strong) UIScrollView *scroll; @property (nonatomic, strong) NSMutableArray *imageArray; @property (nonatomic, strong) UIPageControl *pageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.imageArray = [NSMutableArray array]; CGFloat imageWidth = self.view.frame.size.width; CGFloat imageHeight = self.view.frame.size.height; self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds]; [self.scroll setPagingEnabled:YES]; [self.scroll setBounces:NO]; [self.scroll setShowsHorizontalScrollIndicator:YES]; [self.scroll setContentSize:CGSizeMake((KNumberOfPages + 2) * imageWidth, imageHeight)]; [self.imageArray addObject:[UIImage imageNamed:@"5.jpg"]]; for (NSInteger i = 0; i < KNumberOfPages; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i + 1]]; [self.imageArray addObject:image]; } [self.imageArray addObject:[UIImage imageNamed:@"1.jpg"]]; NSLog(@"%@", self.imageArray); for (NSInteger i = 0; i < KNumberOfPages + 2; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * imageWidth, 0, imageWidth, imageHeight)]; imageView.image = self.imageArray[i]; [self.scroll addSubview:imageView]; } self.scroll.contentOffset = CGPointMake(imageWidth, 0); self.scroll.delegate = self; [self.view addSubview:self.scroll]; self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 440)];//不这样初始化就无法点击 [self.pageControl setCenter:CGPointMake(160, 420)]; [self.pageControl setNumberOfPages:KNumberOfPages]; [self.pageControl setCurrentPage:0]; [self.pageControl setPageIndicatorTintColor:[UIColor blackColor]]; [self.pageControl addTarget:self action:@selector(pageDidClicked) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:self.pageControl]; } - (void)pageDidClicked{ [self.scroll setContentOffset:CGPointMake((self.pageControl.currentPage + 1) * 320, 0) animated:YES]; } //结束减速 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (self.scroll.contentOffset.x == 0) { [self.scroll setContentOffset:CGPointMake(KNumberOfPages * 320, 0)]; }else if (self.scroll.contentOffset.x == (KNumberOfPages + 1) * 320){ [self.scroll setContentOffset:CGPointMake(320, 0)]; } [UIView animateWithDuration:0.5 animations:^{ NSInteger number = self.scroll.contentOffset.x / 320; self.pageControl.currentPage = number - 1; }]; } @end