// // ViewController.m // UIImageView01 // // Created by cqy on 16/2/13. // Copyright © 2016年 程清杨. All rights reserved. // #import "ViewController.h" @interface ViewController (){ UIImageView *img; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建UIImageView //通过图⽚名来初始化⼀个UIImageView类。 img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cat01"]]; img.frame = CGRectMake(120, 220, 200, 300); [self.view addSubview:img]; //注意:ImageView的默认是NO,是关闭交互。要将userInteractionEnabled(响应者链是否交互)设置为YES img.userInteractionEnabled = YES; //添加手势 //轻拍⼿势: //创建轻拍手势识别器 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; [self->img addGestureRecognizer:tap]; // ⻓按⼿势 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; longPress.minimumPressDuration = 0.5; [self->img addGestureRecognizer:longPress]; //旋转⼿势 // 旋转 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)]; [self->img addGestureRecognizer:rotation]; // 捏合⼿势: UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; [self->img addGestureRecognizer:pinch]; // 平移⼿势: UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; [self->img addGestureRecognizer:pan]; //轻扫 // Do any additional setup after loading the view, typically from a nib. } // 轻拍⼿势响应⽅法 - (void)tapAction:(UITapGestureRecognizer *)sender{ NSLog(@"我是轻拍⼿势.."); // 换图⽚ // 获取⼿势图⽚ UIImageView *temp = (UIImageView *)sender.view; temp.image = [UIImage imageNamed:@"cat02"]; } // ⻓按响应⽅法 - (void)longPressAction:(UILongPressGestureRecognizer *)sender{ NSLog(@"⻓按⼿势"); if (sender.state == UIGestureRecognizerStateBegan) { NSLog(@"⻓按开始.."); [UIView animateWithDuration:1 animations:^{ // 修改view的frame CGRect temp1 = sender.view.frame; temp1.size.width +=50; temp1.size.height +=50; // 修改完成附回 sender.view.frame = temp1; sender.view.alpha = 0.4; }]; }else if (sender.state == UIGestureRecognizerStateEnded){ NSLog(@"⻓按结束.."); [UIView animateWithDuration:1 animations:^{ // 修改view的frame CGRect temp1 = sender.view.frame; temp1.size.width -=50; temp1.size.height -=50; // 修改完成附回 sender.view.frame = temp1; sender.view.alpha = 1; }]; } } // 旋转⼿势响应⽅法 - (void)rotationAction:(UIRotationGestureRecognizer *)sender{ NSLog(@"我是旋转⼿势.."); sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation); sender.rotation = 0; } // 捏合⼿势响应⽅法 - (void)pinchAction:(UIPinchGestureRecognizer *)sender{ NSLog(@"我是捏合⼿势"); sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale); sender.scale = 1; } // 平移⼿势响应⽅法 - (void)panAction:(UIPanGestureRecognizer *)sender{ NSLog(@"我是平移⼿势.."); CGPoint point = [sender translationInView:sender.view]; sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y); // 将偏移量归零 [sender setTranslation:CGPointZero inView:sender.view]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
@end