在这里主要讲解一下自己理解的关于6种手势的相关内容
六种手势包括:
轻击手势(TapGestureRecognizer),
轻扫手势 (SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
(1)、轻击手势(包括单击和双击)
//单击
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
[imageV addGestureRecognizer:tap];
}
//方法
-(void)tapAction:(UITapGestureRecognizer*)tap{
NSLog(@"单击");
}
//双击
{
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imageV addGestureRecognizer:doubleTap];
//双击失败了才算单击(双击成功就是双击)
[tap requireGestureRecognizerToFail:doubleTap];
}
-(void)doubleTapAction:(UITapGestureRecognizer*)tap{
NSLog(@"双击");
}
(2)轻扫手势
{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//属性:设置轻扫的方向(默认 右)
//swipe.direction = UISwipeGestureRecognizerDirectionRight;
[imageV addGestureRecognizer:swipe];
}
-(void)swipeAction:(UISwipeGestureRecognizer*)swipe{
NSLog(@"轻扫");
}
(3)长按手势
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[imageV addGestureRecognizer:longPress];
//其中属性allowableMovement:是允许抖动的范围
}
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if (longPress.state ==UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
}
}
(4)拖动手势
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[imageV addGestureRecognizer:pan];
//轻扫和移动的冲突,一旦有冲突,默认移动
//轻扫失败算移动 (轻扫成功就是轻扫)
[pan requireGestureRecognizerToFail:swipe];
}
-(void)panAction:(UIPanGestureRecognizer*)pan{
// NSLog(@"移动...");
//设置当前位置的坐标(相对 起始点 )
CGPoint p1 =[pan translationInView:imageV];
NSLog(@"p1 = %@",NSStringFromCGPoint(p1));
//移动点的速度
// CGPoint v1 = [pan velocityInView:imageV];
// NSLog(@"v1 = %@",NSStringFromCGPoint(v1));
//设置当前位置
// CGPoint p2 = CGPointMake(100, 100);
// [pan setTranslation:p2 inView:imageV];
}
(5)捏合手势
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageV addGestureRecognizer:pinch];
}
-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{
/*属性
scale:缩放的大小比例
velocity:缩放的速度
*/
if (pinch.state ==UIGestureRecognizerStateChanged) {
NSLog(@"一直捏合");
CGFloat pScale =pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(pScale, pScale);
}else if (pinch.state == UIGestureRecognizerStateEnded){
NSLog(@"捏合结束");
[UIView animateWithDuration:0.2 animations:^{
pinch.view.transform =CGAffineTransformIdentity;
}];
}
}
(6)旋转手势
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageV addGestureRecognizer:rotation];
}
-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{
/*
rotation:旋转的弧度 角度:0~360 弧度:0~2π
velocity:旋转的速度
*/
if (rotation.state == UIGestureRecognizerStateChanged) {
NSLog(@"一直旋转");
CGFloat angle = rotation.rotation;
rotation.view.transform = CGAffineTransformMakeRotation(angle);
}else if (rotation.state == UIGestureRecognizerStateEnded){
NSLog(@"旋转结束");
[UIView animateWithDuration:0.2 animations:^{
rotation.view.transform = CGAffineTransformIdentity;
}];
}
}
在模拟器上进行手势操作时可能会出现错误,建议尽量在真机上进行操作。