UI手势识别器

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor whiteColor];

//UIGestureRecognizer 手势识别器

// 手势 :有规律的触摸

//  UIGestureRecognizer 抽象类

//七种手势:轻拍(tap) 长按(longPress) 旋转(rotation)

//捏合(pinch) 拖拽(pan) 轻扫(swipe) 屏幕边缘拖拽(screenEdgePan)

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

imgView.center = self.view.center;

imgView.image = [UIImage imageNamed:@"spurs.jpg"];

[self.view addSubview:imgView];

[imgView release];

//打开用户交互

imgView.userInteractionEnabled = YES;

#if 0  /*轻拍tap*/

// 创建对象

//获取到轻拍手势时  让self 调用tapAction:方法

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

//添加手势

[imgView addGestureRecognizer:tap];

//内存管理

[tap release];

//点击次数 几次才能识别

tap.numberOfTapsRequired = 2;

//手指个数

tap.numberOfTouchesRequired = 2;

#endif

#if 0/* 长按 longPress*/

UILongPressGestureRecognizer *tap1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction1:)];

[imgView addGestureRecognizer:tap1];

[tap1 release];

//长按时间

tap1.minimumPressDuration = 1;

#endif

#if 0

/*旋转rotation*/

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

[imgView addGestureRecognizer:rotation];

[rotation release];

#endif

#if 0

/*捏合pinch*/

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

[imgView addGestureRecognizer:pinch];

[pinch release];

#endif

#if 1

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

[imgView addGestureRecognizer:pan];

[pan release];

}

#endif

#pragma mark - 轻拍tap

-(void)tapAction:(UITapGestureRecognizer *)tap{

NSLog(@"轻拍");

}

-(void)tapAction1:(UILongPressGestureRecognizer *)tap1{

//长安手势是很多手势的基础

//通过状态区分

if (tap1.state == UIGestureRecognizerStateBegan) {

NSLog(@"长按");

}

}

#pragma mark -  旋转rotation

-(void)rotationAction:(UIRotationGestureRecognizer *)rotation{

//UIView transform 属性 专门用来进行形变(位置position/ 旋转rotation/ 缩放scale)设置

//获取当前手势触发的视图

//强制类型转换(UIImageView *)

UIImageView *imgView = (UIImageView *)rotation.view;

//设置transform实现旋转

imgView.transform = CGAffineTransformMakeRotation(rotation.rotation);

NSLog(@"旋转");

}

#pragma  mark - 捏合pinch

-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{

//获取

UIImageView *imgView = (UIImageView *)pinch.view;

//缩放scale(比例)

imgView.transform = (CGAffineTransformScale(imgView.transform, pinch.scale, pinch.scale));

pinch.scale = 1;

NSLog(@"捏合");

}

#pragma mark -  拖拽pan

-(void)panAction:(UIPanGestureRecognizer *)pan{

NSLog(@"拖拽");

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

推荐阅读更多精彩内容