iOS图片预览、放大缩小

61 篇文章 1 订阅
  • 思路

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值