标签:
RootView.m
1 #import "RootView.h" 2 #define YHColor [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1] 3 4 @implementation RootView 5 6 - (instancetype)initWithFrame:(CGRect)frame 7 { 8 self = [super initWithFrame:frame]; 9 if (self) { 10 [self add]; 11 } 12 return self; 13 } 14 15 16 - (void)add { 17 18 // 添加滚动 19 self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame]; 20 self.scrollView.backgroundColor = YHColor; 21 //self.scrollView.contentSize = CGSizeMake(self.frame.size.width * 10, 0); 22 23 [self addSubview:self.scrollView]; 24 25 26 // 添加图片 27 28 self.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"002.jpg"]]; // 用此方法就不用设置frame,因为frame就是图片的大小 29 [self.scrollView addSubview:self.imgView]; 30 31 32 self.scrollView.contentSize = self.imgView.frame.size; //设置滚动范围(根据图片尺寸滚动) 33 34 self.scrollView.showsHorizontalScrollIndicator = NO; // 隐藏垂直滚动条 35 self.scrollView.showsVerticalScrollIndicator = NO; // 隐藏水平滚动条 36 37 38 self.scrollView.bounces = YES; // 设置是否回弹 39 // self.scrollView.alwaysBounceHorizontal = NO; // 设置水平方向是否回弹(必须要设置滚动范围是屏幕大小,否则不起作用) 40 // self.scrollView.alwaysBounceVertical = YES; // 设置垂直方向是否回弹(必须要设置滚动范围是屏幕大小,否则不起作用) 41 42 43 // self.scrollView.contentSize = CGSizeMake(self.imgView.frame.size.width, 0); // 只要水平滚动 44 // self.scrollView.contentSize = CGSizeMake(0, self.imgView.frame.size.height); // 只要垂直滚动 45 46 47 // 设置偏移量,规定要显示的位置 48 self.scrollView.contentOffset = CGPointMake(200, 100); 49 50 51 // 设置缩放 52 self.scrollView.minimumZoomScale = 0.1; // 设置最小比例 53 self.scrollView.maximumZoomScale = 10; // 设置最大比例 54 //self.scrollView.zoomScale = 1; // 设置当前缩放比例(不能在这里设置,要在viewController里设置才行) 55 } 56 @end
RootViewController.m
1 #import "RootViewController.h" 2 #import "RootView.h" 3 4 @interface RootViewController () <UIScrollViewDelegate> 5 6 @property (nonatomic, strong) RootView *rootView; 7 8 @end 9 10 @implementation RootViewController 11 12 - (void)loadView { 13 self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 14 self.view = self.rootView; 15 } 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 20 // 设置代理 21 self.rootView.scrollView.delegate = self; 22 23 // 当前缩放比例 24 self.rootView.scrollView.zoomScale = 0.5; 25 26 } 27 28 #pragma mark - 实现协议方法(监测滚动状态) 29 // 即将开始滚动 30 - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { 31 NSLog(@"开始滚动"); 32 } 33 34 // 滚动完成 35 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 36 NSLog(@"结束滚动"); 37 } 38 39 // 即将开始手动拖拽 40 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 41 NSLog(@"即将开始手动拖拽"); 42 } 43 44 // 手动拖拽完成 45 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { 46 NSLog(@"手动拖拽完成"); 47 } 48 49 // 一直滚动 50 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 51 NSLog(@"一直滚动%f", scrollView.contentOffset.x); 52 } 53 54 55 #pragma mark - 视图缩放 56 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 57 58 // 返回要缩放的视图 59 return self.rootView.imgView; 60 } 61 62 63 // 缩放完成,把图片设置为中间位置 64 - (void)scrollViewDidZoom:(UIScrollView *)scrollView { 65 66 self.rootView.imgView.center = self.view.center; 67 } 68 69 @end
标签:
原文地址:http://www.cnblogs.com/zhizunbao/p/5397666.html