美文网首页
自定义手势

自定义手势

作者: 暖光照 | 来源:发表于2016-11-07 14:53 被阅读0次

简介

系统一共提供了六大手势,如果想要使用其他手势怎么办呢?那就只能来自定义手势,来实现自己想要的酷炫效果。
Demo在GitHub地址
可以留言留下你酷炫的手势想法,我会尝试加进Demo里。

实现方式

1、创建一个手势类,继承UIGestureRecognizer

2、重写必要的方法

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

在这些方法里写一些自己的逻辑,来判断用户的手势是不是自己规定的手势,并在合适的位置修改self.state(因为是只读属性,需要使用KVC修改)。来控制什么时候调用添加的target-action。

示例

目标实现的手势:左右滑动一定距离,一定来回触发的手势(可以认为是抖动)。
主要代码
.h文件
#import <UIKit/UIKit.h>

@interface trembleGestureRecognizer : UIGestureRecognizer

@property (nonatomic,assign) int numberOfTurnRequired; //需要转动几次方向   默认为2.
@property (nonatomic,assign) CGFloat movementRequired;//每次需要向左或者向右多少距离。 默认为50
@property (nonatomic) CGFloat allowableMovement; //允许的误差值,例如向右时突然向左一点,并不判断失败。  默认为20

@end

.m文件主要代码

@property(nonatomic,assign)CGPoint lastTurnPoint;     //上次运动方向的最后位置
@property(nonatomic,assign)Direction lastDirection;   //上次的运动方向
@property(nonatomic,assign)int turnCount;             //目前的转向次数

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch * touch = [touches anyObject];
   self.lastTurnPoint = [touch locationInView:self.view];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

   UITouch * touch = [touches anyObject];
   int nowX = [touch locationInView:self.view].x;

//初始状态
if(self.lastDirection == DirectionNil){
    //向右超过额定允许误差,记录方向
    if(nowX - self.lastTurnPoint.x > self.allowableMovement){
        self.lastDirection = DirectionRight;
    }
    //向左
    else if (nowX - self.lastTurnPoint.x < -self.allowableMovement){
        self.lastDirection = DirectionLeft;
    }


}

//如果上次是向左
if(self.lastDirection == DirectionLeft){
    //如果现在是在左边,则更新转向点为现在的点(不断更新最左点的值)
    if(nowX - self.lastTurnPoint.x < 0){
        self.lastTurnPoint = [touch locationInView:self.view];
    };
    //如果现在是在右边,如果与转向点相差大于要求的距离,则记录该点为转折点,增加转折次数,并更新上次的方向。(变为向右了)
    if (nowX - self.lastTurnPoint.x >self.movementRequired) {
        self.lastTurnPoint = [touch locationInView:self.view];
        self.turnCount++;
        self.lastDirection = DirectionRight;
    }


}
 //如果上次是向右
if(self.lastDirection == DirectionRight){
    
    if(nowX - self.lastTurnPoint.x > 0){
        self.lastTurnPoint = [touch locationInView:self.view];
    };
    
    if(nowX - self.lastTurnPoint.x < -self.movementRequired){
        self.lastTurnPoint = [touch locationInView:self.view];
        self.turnCount++;
        self.lastDirection = DirectionLeft;
    }
    
}

//如果转向已经到达规定次数,结束手势,触发action。并重置手势记录数据
if(self.turnCount >= self.numberOfTurnRequired){
    [self setState:UIGestureRecognizerStateEnded];
    [self reset];
}

}

//重置手势记录数据
- (void)reset {
   self.turnCount = 0;
   self.lastDirection = DirectionNil;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   [self reset];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   [self reset];
}

相关文章

网友评论

      本文标题:自定义手势

      本文链接:https://www.haomeiwen.com/subject/uycouttx.html