iOS 自定义手势

下面是实践过后总结的具体步骤,例子代码实现了一个一横一竖画一个十字的手势:



首先创建UIGestureRecognizer的子类。


#import <UIKit/UIKit.h>


@interface CustomTouch2: UIGestureRecognizer


@end


.m文件import UIGestureRecognizerSubclass.h

#import <UIKit/UIGestureRecognizerSubclass.h>


.m中实现


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;


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


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;



其中,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

中可能需要实现记录手势的起始位置的操作



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

中可能要实现一些检查,当手势动作达到设定位置时触发手势回调方法,在这里需要设置stateUIGestureRecognizerStateEnded

    if (self.state == UIGestureRecognizerStatePossible) {

        [self setState:UIGestureRecognizerStateEnded];

    }



另外,viewController中要对对应的View添加 addGestureRecognizer 

- (void)viewDidLoad {

    [super viewDidLoad];

    CustomTouch2  *customTouch = [[CustomTouch2 alloc] initWithTarget:self action:@selector(handleTouch:)];

    [self.view addGestureRecognizer:customTouch];

}


- (void) handleTouch:(UIRotationGestureRecognizer*) recognizer

{

    NSLog(@"你触发的手势");

}




下面是我实现的一个十字手势的代码:

#import "CustomTouch2.h"

#import <UIKit/UIGestureRecognizerSubclass.h>

@interface CustomTouch2()

{

     CGPoint curTickleStart;

    BOOL arriveFirstPoint;

    BOOL arriveLastPoint;

}

@end


static int touchTimes = 0;


@implementation CustomTouch2

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch * touch = [touches anyObject];

    curTickleStart = [touch locationInView:self.view];

}


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

{

    

    UITouch * touch = [touches anyObject];

    CGPoint ticklePoint = [touch locationInView:self.view];

    CGFloat horizontal = ticklePoint.x - curTickleStart.x;

    CGFloat vertical = ticklePoint.y - curTickleStart.y;

    

    if (!arriveFirstPoint &&  vertical >= 50 && (horizontal <= 10 || horizontal >= -10)) {

        arriveFirstPoint = YES;

            touchTimes ++;

    }

    

    

    if (arriveFirstPoint && (vertical <= 10 || vertical >= -10) && horizontal >= 50){

        arriveLastPoint = YES;

            touchTimes ++;

    }

    

    


    

    if (touchTimes == 2 && self.state == UIGestureRecognizerStatePossible ) {

        [self setState:UIGestureRecognizerStateEnded];

        touchTimes = 0;

        arriveFirstPoint = NO;

        

    }

}

- (void)reset {

    curTickleStart = CGPointZero;

    if (self.state == UIGestureRecognizerStatePossible) {

        [self setState:UIGestureRecognizerStateFailed];

    }

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self reset];

}


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self reset];

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值