iOS点击手势UITapGestureRecognizer :
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 150, 150)];
imgview.image = [UIImage imageNamed:@"6.jpg"];
//注意开启用户交互
imgview.userInteractionEnabled = YES;
[self.view addSubview:imgview];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
// tap.delegate = self; //协议
//点击次数 单击还是双击
tap.numberOfTapsRequired = 2;
//设置属性 一个手指头还是多个手指头点击
tap.numberOfTouchesRequired = 1;
//添加
[imgview addGestureRecognizer:tap];
}
//触发手势点击事件
-(void)imageVIewtap:(UIGestureRecognizer *)tap{
self.view.backgroundColor=[UIColor greenColor];
}
//默认为单击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapClick:)];
tap.delegate = self;
[view addGestureRecognizer :tap];
iOS清扫手势 UISwipeGestureRecognizer:
- (void)viewDidLoad
{
[super viewDidLoad];
_imgview=[[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
_imgview.image=[UIImage imageNamed:@"6.jpg"];
_imgview.userInteractionEnabled=YES;
UISwipeGestureRecognizer *pan = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
pan.delegate = self;
pan.direction = UISwipeGestureRecognizerDirectionRight;
//设置属性 不设置默认为1
pan.numberOfTouchesRequired = 1;
//添加手势到视图上
[_imgview addGestureRecognizer:pan];
[self.view addSubview:_imgview];
}
//判断手势移动方向 -上-下-左-右
-(void)imageVIewtap:(UISwipeGestureRecognizer *)tap{
if (tap.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"left");
}
else if(tap.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"right");
}
/*
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
*/
}
iOS移动手势
UIPanGestureRecognizer:
- (void)viewDidLoad
{
[super viewDidLoad];
_imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
_imgview.image = [UIImage imageNamed:@"6.jpg"];
_imgview.userInteractionEnabled = YES;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
pan.delegate=self;
//设置属性
pan.minimumNumberOfTouches = 1;
[_imgview addGestureRecognizer:pan];
[self.view addSubview:_imgview];
}
-(void)imageVIewtap:(UIGestureRecognizer *)tap{
self.view.backgroundColor=[UIColor greenColor];
if (tap.state==UIGestureRecognizerStateBegan) {
NSLog(@"开始移动");
//缩放图片
self.imgview.transform=CGAffineTransformMakeScale(0.5, 0.5);
}
else if (tap.state==UIGestureRecognizerStateChanged){
NSLog(@"移动中");
self.imgview.center=[tap locationInView:self.view];
}
else if(tap.state==UIGestureRecognizerStateEnded){
NSLog(@"移动完成");
self.imgview.transform=CGAffineTransformIdentity;
}
}
iOS长按手势 UILongPressGestureRecognizer:
- (void)viewDidLoad
{
[super viewDidLoad];
_imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
_imgview.image = [UIImage imageNamed:@"6.jpg"];
_imgview.userInteractionEnabled = YES;
UILongPressGestureRecognizer *pan = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
pan.delegate=self;
//设置属性
pan.minimumPressDuration = 2;
[_imgview addGestureRecognizer:pan];
[self.view addSubview:_imgview];
}
-(void)imageVIewtap:(UILongPressGestureRecognizer *)tap{
if (tap.state == UIGestureRecognizerStateEnded) {
NSLog(@"长按手势完毕");
}else{
NSLog(@"识别了点击手势");
}
}
iOS旋转手势
UIRotationGestureRecognizer:
- (void)viewDidLoad
{
[super viewDidLoad];
_imgview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 200, 200)];
_imgview.image = [UIImage imageNamed:@"6.jpg"];
_imgview.userInteractionEnabled = YES;
UIRotationGestureRecognizer *pan = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(imageVIewtap:)];
pan.delegate = self;
[_imgview addGestureRecognizer:pan];
[self.view addSubview:_imgview];
}
-(void)imageVIewtap:(UIRotationGestureRecognizer *)tap{
tap.view.transform = CGAffineTransformRotate(tap.view.transform, tap.rotation);
tap.rotation = 0.0;
}