scollView中实现缩放
2015-06-09 22:48
190 查看
RootViewController.m
RootView.m
ImageScollView.m
#import "RootViewController.h" #import "RootView.h" #import "ImagescrollView.h" @interface RootViewController () <UIScrollViewDelegate> { NSInteger _startIndex; } #pragma mark - 声明私有属性 @property (nonatomic, retain) RootView *rootView; @end @implementation RootViewController #pragma mark - 自定义视图 代替 自带视图 - (void)loadView { self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease]; self.view = _rootView; } - (void)viewDidLoad { [super viewDidLoad]; //设置代理 _rootView.scrollView.delegate = self; } #pragma mark - 代理方法 #pragma mark - 开始拖拽 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { _startIndex = scrollView.contentOffset.x / scrollView.frame.size.width; } #pragma mark - 结束自由滚动 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { //获取当前页面 NSInteger currentIndex = scrollView.contentOffset.x / scrollView.frame.size.width; //如果不是当前页面 if (currentIndex == _startIndex) { return; } //给数组中的每个元素都发送 setScaleone消息 perform执行完成 [scrollView.subviews makeObjectsPerformSelector:@selector(setScaleOne)]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - 重写 #pragma mark - dealloc -(void)dealloc { [_rootView release]; [super dealloc]; }
RootView.m
#import "RootView.h" #import "ImageScrollView.h" @implementation RootView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor cyanColor]; [self addAllViews]; } return self; } #pragma mark - 添加全部控件 - (void)addAllViews { // 添加scrollView self.scrollView = [[[UIScrollView alloc] initWithFrame:self.frame] autorelease]; [self addSubview:_scrollView]; // 图片总数 NSInteger count = 9; // for循环,添加ImageScrollView for (int i = 0; i < count; i++) { NSString *imgName = [NSString stringWithFormat:@"bg_%d.JPG", i]; UIImage *image = [UIImage imageNamed:imgName]; ImageScrollView *imageScrollView = [[ImageScrollView alloc] initWithFrame:CGRectMake(i * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) image:image]; [_scrollView addSubview:imageScrollView]; [imageScrollView release]; } // 设置scrollView的一些属性 _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * count, _scrollView.frame.size.height); _scrollView.pagingEnabled = YES; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; } #pragma mark - 重写 #pragma mark dealloc - (void)dealloc { [_scrollView release]; [super dealloc]; }
ImageScollView.m
#import "ImageScrollView.h" @interface ImageScrollView () <UIScrollViewDelegate> @property (nonatomic, retain) UIImageView *imageView; @end @implementation ImageScrollView - (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image { if (self = [super initWithFrame:frame]) { // 初始化图片 self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; _imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); [self addSubview:_imageView]; // 设置自身为代理 self.delegate = self; // 设置最大和最小缩放比例 self.minimumZoomScale = 0.5; self.maximumZoomScale = 2; } return self; } #pragma mark - 代理方法 #pragma mark 设置缩放的视图 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageView; } #pragma mark 缩放结束,修改到中心 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { CGRect frame = view.frame; if (scrollView.frame.size.width < scrollView.contentSize.width) { frame.origin.x = 0; } else { frame.origin.x = (scrollView.frame.size.width - scrollView.contentSize.width) / 2; } if (scrollView.frame.size.height < scrollView.contentSize.height) { frame.origin.y = 0; } else { frame.origin.y = (scrollView.frame.size.height - scrollView.contentSize.height) / 2; } view.frame = frame; } #pragma mark 设置视图恢复到原来的位置 - (void)setScaleOne { CGRect frame = _imageView.frame; frame.origin.x = 0; frame.origin.y = 0; _imageView.frame = frame; self.zoomScale = 1; } #pragma mark - 重写 #pragma mark dealloc - (void)dealloc { [_imageView release]; [super dealloc]; }
相关文章推荐
- awk模式匹配
- 黑马程序员——Java基础---网络编程&正则
- pulltorefresh自定义属性
- listview的divider边距
- java的replace和replaceAll
- js零碎知识
- Android Studio 导入第三方库
- oc文件操作NSFileManager/NSData
- HDOJ 1010 Tempter of the Bone诱惑者的骨
- Number of 1 Bits
- MT7621A 硬件调试总结---(2)
- C++ 中的类型转换函数
- hiho一下第1周 最长回文子串
- Model元数据解析
- 05.linux目录结构
- jms基础概念和应用场景
- hokuyo rviz配置
- 安装pywin32
- 使用mplayer在终端下播放音乐
- js初步 (二)