您的位置:首页 > 移动开发 > IOS开发

iOS 手势集合

2015-12-30 17:44 357 查看
#import "ViewController.h"

@interface ViewController ()
{
UIImageView *imageView;
UILabel *label;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super
viewDidLoad];
//背景图片设置
imageView=[[UIImageView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
imageView.image=[UIImage
imageNamed:@"jay.jpg"];
[self.view
addSubview:imageView];
imageView.userInteractionEnabled=YES;

//将手势类型显示在Label上
label=[[UILabel
alloc]initWithFrame:CGRectMake(100,
self.view.frame.size.height-50,
200, 50)];
label.backgroundColor=[UIColor
grayColor];
[imageView addSubview:label];
label.textAlignment=NSTextAlignmentCenter;
label.textColor=[UIColor
greenColor];

//1.一只手单击
UITapGestureRecognizer *tap=[[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tapAction:)];
[imageView
addGestureRecognizer:tap];

//2.一只手双击
UITapGestureRecognizer *tapDouble=[[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tapDoubleAction:)];
//点击次数
tapDouble.numberOfTapsRequired=2;
[imageView
addGestureRecognizer:tapDouble];

//解决单双击冲突
双击生效的时候让单击手势失效
[tap requireGestureRecognizerToFail:tapDouble];

//3.两只手单击
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(doubleTap:)];

doubleTap.numberOfTapsRequired=1;
//手指个数
doubleTap.numberOfTouchesRequired=2;
[imageView
addGestureRecognizer:doubleTap];

//4.两只手双击
UITapGestureRecognizer *doubleTapDouble=[[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(doubleTapDouble:)];
//设置点击次数
doubleTapDouble.numberOfTapsRequired=2;
//手指个数
doubleTapDouble.numberOfTouchesRequired=2;
[imageView addGestureRecognizer:doubleTapDouble];

[doubleTap requireGestureRecognizerToFail:doubleTapDouble];

//5.向左轻扫手势
UISwipeGestureRecognizer *swipeLeft=[[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(swipeLeftAction:)];
//滑动方向
swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft;
[imageView
addGestureRecognizer:swipeLeft];

//6.向右轻扫手势
UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(swipeRightAction:)];
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
[imageView
addGestureRecognizer:swipeRight];

//7.移动
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panAction:)];
[imageView
addGestureRecognizer:pan];

//解决轻扫和移动的冲突:
只有轻扫生效的时候才让移动失效
[pan requireGestureRecognizerToFail:swipeLeft];
[pan requireGestureRecognizerToFail:swipeRight];

//旋转和捏合手势不能同时响应,二选一
//8.旋转
UIRotationGestureRecognizer *rotate=[[UIRotationGestureRecognizer
alloc]initWithTarget:self
action:@selector(rotateAction:)];
[imageView
addGestureRecognizer:rotate];

//9.捏合
UIPinchGestureRecognizer *pin=[[UIPinchGestureRecognizer
alloc]initWithTarget:self
action:@selector(pinchAction:)];
[imageView
addGestureRecognizer:pin];
[pin requireGestureRecognizerToFail:rotate];

//10.长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(longPress:)];
[imageView
addGestureRecognizer:longPress];
}

//1.一只手单击
-(void)tapAction:(UITapGestureRecognizer *)tapGesture{

label.text=@"一只手单击";
NSLog(@"%@",label);
}

//2.一只手双击
-(void)tapDoubleAction:(UITapGestureRecognizer *)tapDoubleGesture{

label.text=@"一只手双击";
NSLog(@"%@",label);
}

//3.两只手单击
-(void)doubleTap:(UITapGestureRecognizer *)doubleTap{

label.text=@"两只手单击";
NSLog(@"两只手单击");
}

//4.两只手双击
-(void)doubleTapDouble:(UITapGestureRecognizer *)doubleTap{

label.text=@"两只手双击";
NSLog(@"两只手双击");
}

//5.向左轻扫手势
-(void)swipeLeftAction:(UITapGestureRecognizer *)left{

label.text=@"向左轻扫";
NSLog(@"%@",label);
}

//6.向右轻扫
-(void)swipeRightAction:(UITapGestureRecognizer *)left{

label.text=@"向右轻扫";
NSLog(@"%@",label);
}

//7.移动
-(void)panAction:(UIPanGestureRecognizer *)pan{

label.text=@"移动";
NSLog(@"%@",label);
}

//8.旋转
-(void)rotateAction:(UIRotationGestureRecognizer *)rotate{
//正在旋转
if(rotate.state==UIGestureRecognizerStateChanged){
//取得弧度
CGFloat radius=rotate.rotation;
rotate.view.transform=CGAffineTransformMakeRotation(radius);
}else
if(rotate.state==UIGestureRecognizerStateEnded){
//还原transform
[UIView animateWithDuration:0.35
animations:^{
rotate.view.transform=CGAffineTransformIdentity;
}];
}
label.text=@"旋转";
}

//9.捏合
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{
if(pinch.state==UIGestureRecognizerStateChanged){
//取得缩放的比例
CGFloat scale=pinch.scale;
pinch.view.transform=CGAffineTransformMakeScale(scale, scale);
}else
if(pinch.state==UIGestureRecognizerStateEnded){
[UIView animateWithDuration:0.35
animations:^{
pinch.view.transform=CGAffineTransformIdentity;
}];
}
label.text=@"捏合";
}

//10.长按手势
-(void)longPress:(UILongPressGestureRecognizer *)longPress{

label.text=@"长按手势";
NSLog(@"长按手势");
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: