--------------轻拍手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundColor:)];
// 将手势监听器加入view中
[_aView addGestureRecognizer:tapGesture];
//要求点击的次数是两次
tapGesture.numberOfTapsRequired = 2;
//要求必须两个手指点击
tapGesture.numberOfTouchesRequired = 2;
手势监听:监听轻拍手势然后改变背景
-(void)changeBackgroundColor:(UITapGestureRecognizer *)gesture
{
UIView *aView = gesture.view;
aView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0alpha:1.0];
}
--------------长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(changeBackgroundColor:)];
[_aView addGestureRecognizer:longPressGesture];
手势监听:监听轻拍手势然后改变背景
-(void)changeBackgroundColor:(UILongPressGestureRecognizer *)gesture
{
//这个手势比较奇葩,得设置响应的是长按的开始结束还是取消的状态,否则它会响应好几次
if (gesture.state == UIGestureRecognizerStateBegan) {
UIView *aView = gesture.view;
aView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0alpha:1.0];
}
--------------轻扫手势
UISwipeGestureRecognizer *swipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundColor:)];
[_aView addGestureRecognizer:swipGesture];
swipGesture.numberOfTouchesRequired = 1;
// 改变响应轻扫响应方向,下面的设置是响应由上往下的手势
swipGesture.direction = UISwipeGestureRecognizerDirectionDown;
手势监听:监听轻拍手势然后改变背景
-(void)changeBackgroundColor:(UISwipeGestureRecognizer *)gesture
{
UIView *aView = gesture.view;
aView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0alpha:1.0];
}
--------------拖动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
[_aView addGestureRecognizer:panGesture];
手势监听:拖动手指实现view跟着移动
-(void)panGestureRecognizer:(UIPanGestureRecognizer *)pan
{
CGPoint offsetPoint = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformMakeTranslation(offsetPoint.x, offsetPoint.y);
}
--------------捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureRecognizer:)];
[_aView addGestureRecognizer:pinch];
手势监听:捏合手指实现view的跟随缩放
-(void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}
--------------旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureRecognizer:)];
[_aView addGestureRecognizer:rotationGesture];
[rotationGesture release];
手势监听:旋转手指实现view的跟随实现旋转
-(void)rotationGestureRecognizer:(UIRotationGestureRecognizer*)rotationGesture
{
rotationGesture.view.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}
tips:
IOS模拟器中可以通过安alt(option)键来实现模拟两个手指按的情况,如果是3个手指捏,呵呵,目前木有那个手机支持