经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
scrollView浏览图片并缩放
来源:cnblogs  作者:春天里的花骨朵  时间:2018/12/13 9:23:04  对本文有异议

一、应用场景

类似微信发朋友圈添加图片(相册或拍照),点击图片可以浏览,浏览时可对当前图片进行缩放,并且该缩放不影响其他图片正常显示;浏览时可删除图片

二、关键代码

//这里主要的问题是:如果采用CGAffineTransformScale对scrollview进行缩放,则浏览时移到下一张图片也会跟着缩放;如果捏合手势添加到scrollview的imageview中,同样采用CGAffineTransformScale对该图片缩放(pinGes.view.transform = CGAffineTransformScale(self.scrollView.transform, newScale, newScale);)并没有效果;

//因此,采用的策略是:在scrollview中嵌套scrollview,然后再嵌套imageview,并且采用zoomToRect缩放,而非CGAffineTransformScale

  1. - (void)cuLayoutSubViews
  2. {
  3. BL_LoadNavHeight;
  4. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth_N(), ScreenHeight_N())];
  5. self.scrollView.backgroundColor = [UIColor blackColor];
  6. [self.view addSubview:self.scrollView];
  7. self.scrollView.delegate = self;
  8. self.scrollView.pagingEnabled = YES;
  9. self.scrollView.scrollEnabled = YES;
  10. self.scrollView.showsHorizontalScrollIndicator = NO;
  11. [self.imageArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  12. UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(idx*ScreenWidth_N(), navViewH, ScreenWidth_N(), ScreenHeight_N()-navViewH)];
  13. subScrollView.backgroundColor = [UIColor blackColor];
  14. subScrollView.contentSize = CGSizeMake(ScreenWidth_N(), ScreenHeight_N()-navViewH);
  15. subScrollView.delegate = self;
  16. subScrollView.minimumZoomScale = self.minScale;
  17. subScrollView.maximumZoomScale = self.maxScale;
  18. [subScrollView setZoomScale:1.0];
  19. UIImage *img = (UIImage *)obj;
  20. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth_N(), ScreenHeight_N()-navViewH)];
  21. imgView.userInteractionEnabled = YES;
  22. imgView.contentMode = UIViewContentModeScaleAspectFit;
  23. imgView.image = img;
  24. UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGesClick:)];
  25. //捏合手势
  26. [imgView addGestureRecognizer:pin];
  27. [subScrollView addSubview:imgView];
  28. [self.scrollView addSubview:subScrollView];
  29. [self.subScrollViewArr addObject:subScrollView];
  30. [self.imgViewArr addObject:imgView];
  31. }];
  32. [self adjustScrollViewOffset];
  33. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(navHidden)];
  34. // [self.scrollView addGestureRecognizer:tap];
  35. }

 

  1. - (void)pinGesClick:(UIPinchGestureRecognizer *)pinGes
  2. {
  3. // switch (pinGes.state) {
  4. // case UIGestureRecognizerStateBegan://scale began
  5. // case UIGestureRecognizerStateChanged://scale changed
  6. // {
  7. // CGFloat currentScale = [[self.scrollView.layer valueForKeyPath:@"transform.scale"] floatValue];
  8. // CGFloat newScale = pinGes.scale - self.lastScale + 1;
  9. //
  10. // newScale = MIN(newScale, self.maxScale / currentScale);
  11. // newScale = MAX(newScale, self.minScale / currentScale);
  12. //
  13. // self.scrollView.transform = CGAffineTransformScale(self.scrollView.transform, newScale, newScale);
  14. //
  15. // for (int i = 0; i < self.imgViewArr.count; i++) {
  16. // UIImageView *subImgView = self.imgViewArr[i];
  17. // NSLog(@"subImgView----%d---%@", i, subImgView);
  18. // }
  19. //
  20. // self.lastScale = pinGes.scale;
  21. //
  22. //
  23. // }
  24. // break;
  25. // case UIGestureRecognizerStateEnded://scale ended
  26. // self.lastScale = 1;
  27. // break;
  28. // default:
  29. // break;
  30. // }
  31.  
  32. float newScale = [(UIScrollView*)pinGes.view.superview zoomScale];
  33. CGRect zoomRect = [self zoomRectForScale:newScale inView:(UIScrollView*)pinGes.view.superview withCenter:[pinGes locationInView:pinGes.view]];
  34. [(UIScrollView*)pinGes.view.superview zoomToRect:zoomRect animated:YES];
  35. }

 

//必须返回当前缩放视图——实现代理方法

  1. //放回当前缩放图片
  2. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  3. {
  4. for (UIView *v in scrollView.subviews){
  5. return v;
  6. }
  7. return nil;
  8. }

 

三、效果

 

GitHub

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号