美文网首页iOS常用
iOS图片预览、放大缩小

iOS图片预览、放大缩小

作者: iOS鑫 | 来源:发表于2021-03-01 13:19 被阅读0次
    • 思路

      图片预览,优先考虑基础控件UIImageView、UIButton

      图片预览中可能需设置不同的mode,优先考虑UIImageView

      typedef NS_ENUM(NSInteger, UIViewContentMode) {
          UIViewContentModeScaleToFill,
          UIViewContentModeScaleAspectFit,      
          UIViewContentModeScaleAspectFill,     
          UIViewContentModeRedraw,             
          UIViewContentModeCenter,              
          UIViewContentModeTop,
          UIViewContentModeBottom,
          UIViewContentModeLeft,
          UIViewContentModeRight,
          UIViewContentModeTopLeft,
          UIViewContentModeTopRight,
          UIViewContentModeBottomLeft,
          UIViewContentModeBottomRight,
      }
      
      

      图片放大、缩小的思路:1. 手势+frame 2.scrollview的zoomScale

      很愉快的决定选择2,不要问为什么,因为我懒,能用系统提供的,坚决不折腾

    • 上菜

      • 设置页面属性
      @property (nonatomic, strong) UIScrollView *mScroll;
      @property (nonatomic, strong) UIImageView *imgInfo;
      
      
      • 界面初始化
      self.mScroll = [[UIScrollView alloc] initWithFrame:CGRectZero];
      self.mScroll.backgroundColor = [UIColor colorWithHexs:0x3f3f3f];
      self.mScroll.maximumZoomScale = 3.0;
      self.mScroll.minimumZoomScale = 1;
      self.mScroll.delegate = self;
      self.mScroll.showsVerticalScrollIndicator = NO;
      self.mScroll.showsHorizontalScrollIndicator = NO;
      [self.view addSubview:self.mScroll];
      [self.mScroll mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.equalTo(self.preView);
      }];
      
      self.imgInfo = [[UIImageView alloc] initWithFrame:CGRectZero];
      self.imgInfo.clipsToBounds = YES;
      [self.imgInfo setUserInteractionEnabled:YES];
      self.imgInfo.backgroundColor = [UIColor colorWithHexs:0x3f3f3f];
      [self.mScroll addSubview:self.imgInfo];
      [self.imgInfo mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.equalTo(self.mScroll);
          make.width.equalTo(self.mScroll);
      }];
      
      

      其中maximumZoomScale与minimumZoomScale表示可缩放程度

      • 顺带加个双击手势,比如双击可放大,再放大,再缩小
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandlerTwice)];
      tap.numberOfTapsRequired = 2;
      [self.imgInfo addGestureRecognizer:tap];
      
      
      • double click直接控制缩放
      - (void)tapHandlerTwice {
        if (self.mScroll.zoomScale < 2) {
            [self.mScroll setZoomScale:2];
        } else if (self.mScroll.zoomScale < 3) {
            [self.mScroll setZoomScale:3];
        } else {
            [self.mScroll setZoomScale:1];
        }
      }
      
      
      • UIScrollViewDelegate设置scrollview的Zooming View
      #pragma mark - UIScrollViewDelegate
      - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
          return self.imgInfo;
      }
      
      

    这里有一个iOS开发交流群:130595548!如果你也是一个有梦想的iOS开发者,欢迎你的加入!

    相关文章

      网友评论

        本文标题:iOS图片预览、放大缩小

        本文链接:https://www.haomeiwen.com/subject/eenmfltx.html