iOS中手势一共有六种
一、点击手势
1.一个手指单击效果:
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signleTap:)];
//点击的次数
tap1.numberOfTapsRequired = 1;
//点击手指的个数
tap1.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap1];
-(void)signleTap:(UITapGestureRecognizer *)signle
{
NSLog(@"单击一个手指");
}
2.两个手指双击
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
//点击的次数
tap2.numberOfTapsRequired = 2;
//点击手指的个数
tap2.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:tap2];
-(void)doubleTap:(UITapGestureRecognizer *)signle
{
NSLog(@"双击两个手指");
}
此处注意如果两个点击事件在同一个方法中,点击事件之间是相互排斥的例如:
-(void)tapGesture{
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signleTap:)];
//点击的次数
tap1.numberOfTapsRequired = 1;
//点击手指的个数
tap1.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap1];
/***********************************************/
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
//点击的次数
tap2.numberOfTapsRequired = 2;
//点击手指的个数
tap2.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:tap2];
//当A(tap1)手势发生时,即便A已经满足条件,但要等到B(tap2)手势失败后才触发
[tap1 requireGestureRecognizerToFail:tap2]; // 这句是重点
}
************************************************************************************************
二、轻扫手势
UISwipeGestureRecognizer *swip =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
//轻扫方向 swip.direction = UISwipeGestureRecognizerDirectionLeft;
//轻扫手势手指个数
swip.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swip];
-(void)swipAction:(UISwipeGestureRecognizer *)swip
{
}
三、捏合手势
-(void)pinchGesture
{
UIPinchGestureRecognizer *pich= [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pich];
}
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
//捏合比例
float scale =pinch.scale;
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
if (scale > 1) {
NSLog(@"捏合放大");
}else{
NSLog(@"捏合缩小");
}
}
四、拖移手势
-(void)panGesture{
UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//自己设置的视图Label,可以添加到任何视图上自己决定就好
[label addGestureRecognizer:pan];
}
-(void)panAction:(UIPanGestureRecognizer *)pan
{
//移动的坐标值
CGPoint translation =[pan translationInView:self.view];
//移动后的坐标
pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y);
//设置坐标和速度
[pan setTranslation:CGPointZero inView:self.view];
}
五、旋转属性
-(void)rotationGesture
{
UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[label addGestureRecognizer:rotation];
}
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
//旋转效果 第一个参数是本身的大小,第二个参数是旋转的弧度
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
//设置旋转的弧度是180°
rotation.rotation =M_PI;
}
六、长按手势
-(void)longPressGesture
{
UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//按得最少时间
longPress.minimumPressDuration = 0.5;
// 允许移动的次数
longPress.allowableMovement = 2;
[self.view addGestureRecognizer:longPress];
}
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
}
//
以上简单的介绍了手势的使用,其中一部分属性没有写入,可根据需要查找官方文档