iOS圖片預覽、放大縮小

  • 思路ide

    圖片預覽,優先考慮基礎控件UIImageView、UIButtonui

    圖片預覽中可能需設置不一樣的mode,優先考慮UIImageViewatom

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

    圖片放大、縮小的思路:1. 手勢+frame 2.scrollview的zoomScale code

    很愉快的決定選擇2,不要問爲何,由於我懶,能用系統提供的,堅定不折騰圖片

  • 上菜ip

    • 設置頁面屬性
      @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);
    }];get

    其中maximumZoomScale與minimumZoomScale表示可縮放程度
    
    - 順帶加個雙擊手勢,好比雙擊可放大,再放大,再縮小

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandlerTwice)];
    tap.numberOfTapsRequired = 2;
    [self.imgInfo addGestureRecognizer:tap];it

    - 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
相關文章
相關標籤/搜索