1 UIScrollView 与 delegate的通讯
1.1 用户开始拖拽时,调用scrollViewWillBeginDragging
1.2 具体滚动到某个位置,调用scrollViewDidScroll
1.3 调用scrollViewDidEndDragging:willDecelerate
(1) 声明协议 <UIScrollViewDelegate>
(2) 设置代理
(3) 实现对应的方法
// // ViewController.m // // // Created by xin on 15-4-4. // Copyright (c) 2015年 Jackey. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIButton *lastContent; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGFloat height = CGRectGetMaxY(self.lastContent.frame)+44.f; self.scrollView.contentSize = CGSizeMake(0, height); //声明协议 self.scrollView.delegate = self; } //设置代理 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ //实现方法 NSLog(@"start dragging"); } @end
2 用户使用捏合手势时调用
viewForZoomingInScrollView
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGFloat height = CGRectGetMaxY(self.lastContent.frame)+44.f; self.scrollView.contentSize = CGSizeMake(0, height); //声明协议 self.scrollView.delegate = self; //定义缩放的范围 self.scrollView.maximumZoomScale = 2.f; self.scrollView.minimumZoomScale = .5f; } //返回uiview -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return self.imageView; }
3 pageControl 的2个属性
self.pageControl.numberOfPages = ImageCount; self.pageControl.currentPage = pageNum;
4 scrollView的图片滚动需要注意的点
// 让动画一页一页的效果 self.scrollView.pagingEnabled = YES; -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGPoint offSet = self.scrollView.contentOffset; CGFloat offsetX = offSet.x; CGFloat width = self.scrollView.frame.size.width; //加多0.5width,提前预判 int pageNum = (offsetX + 0.5f * width) / width ; self.pageControl.currentPage = pageNum; }
所有的code
// // ViewController.m // scrollPicture // // Created by xin on 15/4/7. // Copyright (c) 2015年 Jackey. All rights reserved. // #import "ViewController.h" #define ImageCount 5 @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGFloat width = self.scrollView.bounds.size.width; CGFloat height = self.scrollView.bounds.size.height; //image draw for (int i=0; i<ImageCount+1; i++) { UIImageView *imageView = [[UIImageView alloc]init]; CGFloat imageX = (i-1)*width; imageView.frame = CGRectMake(imageX, 0.f, width, height); imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d",i]]; [self.scrollView addSubview:imageView]; } self.scrollView.contentSize = CGSizeMake((ImageCount*width), 0); // 让动画一页一页的效果 self.scrollView.pagingEnabled = YES; self.scrollView.delegate = self; self.pageControl.numberOfPages = ImageCount; } #pragma mark - UIScrollViewDelegate实现方法 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGPoint offSet = self.scrollView.contentOffset; CGFloat offsetX = offSet.x; CGFloat width = self.scrollView.frame.size.width; //加多0.5width,提前预判 int pageNum = (offsetX + 0.5f * width) / width ; self.pageControl.currentPage = pageNum; } @end
4 NSTimer
//usage self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; //invalidate [self.timer invalidate]; self.timer = nil;
上面的写法会引发冲突,如果存在其它的dragging事件
所以
self.timer = [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];