关于ios:iOS图片预览放大缩小

3次阅读

共计 2043 个字符,预计需要花费 6 分钟才能阅读完成。

  • 思路

    图片预览,优先思考根底控件 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
正文完
 0