关闭

iOS开发 UI高级 图片放大缩小的实现

标签: ios开发图片ui
232人阅读 评论(0) 收藏 举报
分类:
#import <UIKit/UIKit.h>

@interface ChangeScaleView : UIImageView
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIImageView *scaleImageView;
@end
#import "ChangeScaleView.h"

@implementation ChangeScaleView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self createTap];
    }
    return self;
}

-(void)createTap{
    
    self.userInteractionEnabled = YES;
    // 初始化手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tapGesture];
}

#pragma mark - 放大手势的响应事件
-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    [self createView];
    
    [UIView animateWithDuration:.5 animations:^{
        _scaleImageView.frame = _scrollView.frame;
        _scrollView.backgroundColor = [UIColor orangeColor];
        self.hidden = YES;
    }];
}

-(void)createView{
    //初始化滑动视图
    _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.window addSubview:_scrollView];
    // 移到原来所在位置
    CGRect frame = [self convertRect:self.bounds toView:self.window];
    _scaleImageView = [[UIImageView alloc] initWithFrame:frame];
    
    _scaleImageView.image = self.image;
    
    _scaleImageView.userInteractionEnabled = YES;
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallTapGesture:)];
    [_scaleImageView addGestureRecognizer:tap];
    [_scrollView addSubview:_scaleImageView];

}

//缩小手势的响应事件
-(void)smallTapGesture:(UITapGestureRecognizer *)tap{
    
    [UIView animateWithDuration:.3 animations:^{
        
        _scaleImageView.frame = self.frame;
        
    } completion:^(BOOL finished) {
        self.hidden = NO;
        [_scrollView removeFromSuperview];
        
    }];
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16248次
    • 积分:622
    • 等级:
    • 排名:千里之外
    • 原创:48篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    最新评论