• 思路

    图片预览,优先考虑基础控件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;
  • Game Over

善斋书屋
1 声望0 粉丝