欢迎光临朱凯的个人博客!
朱凯,mzetire.com

给图片添加捏合和旋转手势,应用仿射变换写出相应的响应方法

 发布时间: 2018-12-15 15:19      原创:  朱凯      评论: .       .人查看

北京智美方成网络科技有限公司面试题,薪资: 9k-11k

题目: 给图片添加捏合和旋转手势,应用仿射变换写出相应的响应方法

// 核心代码
#import "ViewController.h"
 
@interface ViewController () 
@property (nonatomic,strong) UIImageView *imageView;
@end
 
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self gestures];
}
- (void)gestures{
    self.imageView.userInteractionEnabled = YES;
    self.imageView.multipleTouchEnabled = YES;
    // 添加手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    [self.imageView addGestureRecognizer:pinch];
    UIRotationGestureRecognizer *rot = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rot:)];
    rot.delegate = self;
    [self.imageView addGestureRecognizer:rot];
}
// 多手势代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
// 手势响应方法,要求手势结束后图片恢复原状
- (void)pinch:(UIPinchGestureRecognizer *)pinch{
    UIView *view = pinch.view;
    if (pinch.state == UIGestureRecognizerStateBegan || pinch.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
        pinch.scale = 1;
    }else if (pinch.state == UIGestureRecognizerStateEnded){
        view.transform = CGAffineTransformIdentity;
    }
}
- (void)rot:(UIRotationGestureRecognizer *)rot{
    UIView *view = rot.view;
    if (rot.state == UIGestureRecognizerStateBegan || rot.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformRotate(view.transform, rot.rotation);
        rot.rotation = 1 / 360;
    }else if (rot.state == UIGestureRecognizerStateEnded){
        view.transform = CGAffineTransformIdentity;
    }
}
- (void)setUpUI{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 340, 120)];
    self.imageView.center = self.view.center;
    self.imageView.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:self.imageView];
}
@end

demo下载: 多手势+仿射变换

密码: t3y5

分享是一种快乐,也是一种美德:
¥打开支付宝,扫码领红包¥ 红包

支付宝 aliPay

微信 wxPay