添加自定义手势,UIGestureRecognizer

- (void)addCustomGestures {
    /*
     *注意:
     *添加自定义手势时,必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,
     *否则影响地图内部的手势处理
     */
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTap.delegate = self;
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.cancelsTouchesInView = NO;
    doubleTap.delaysTouchesEnded = NO;
    
    [self.view addGestureRecognizer:doubleTap];
    
    /*
     *注意:
     *添加自定义手势时,必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,
     *否则影响地图内部的手势处理
     */
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTap.delegate = self;
    singleTap.cancelsTouchesInView = NO;
    singleTap.delaysTouchesEnded = NO;
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [self.view addGestureRecognizer:singleTap];
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fingers is your one stop choice for all gesture handling in Unity (and other C# platforms as Fingers has no Unity specific code). Fingers has been tested and runs great on MAC, Windows, iOS and Android. Full C# source code and shader code is provided! No other asset matches Fingers Gestures on price and feature set. Fingers - Touch Gesture for Unity has been featured in multiple Unity promotions and tutorial videos, showing the high quality and full feature set and value proposition. With Fingers, you get all the common touch gestures you are used to built in. This includes: tap, double tap, long tap, pan, rotate, scale and swipe gestures. In addition, creating your own custom gestures is simple and easy. I've recently began adding on-screen controls. Right now a joystick and dpad are included with more controls planned for the future. Adding gestures in the inspector has never been easier. Simply add from the component menu and configure your gesture. Set the callback to your script and you are done! Pan/rotate/scale and drag and drop is simple. Built in scripts for these scenarios, with demo scenes are provided. I've also added image recognition gestures. With a few minutes of tweaking, you can recognize shapes like circles, lightning bolts, check marks and the letter X. Previewing gestures in the player is easy, even without a touch screen. Just use the shift and control key plus mouse wheel to simulate pinch and rotate gestures, along with each button to simulate a different finger. Fingers has a similar architecture to UIGestureRecognizer from iOS, but is built using all C# code and will easily run on any platform that has a mouse or touch screen. Gestures are restricted to most UI elements by default, but this is easily configured if you want the gestures to pass through additional UI elements. This asset works great with the default Unity UI and Event System. Gestures can be executed simultaneously or exclusively (the default). In addition
苹果提供了UIGestureRecognizer类,可以用来实现自定义手势。下面是一个简单的例子,实现在屏幕上绘制一个“V”形手势来触发事件: 1. 首先在视图控制器中创建一个手势识别器对象: ``` @property (nonatomic, strong) UIGestureRecognizer *gestureRecognizer; ``` 2. 在viewDidLoad方法中,创建手势识别器并将其添加到视图上: ``` self.gestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [self.view addGestureRecognizer:self.gestureRecognizer]; ``` 3. 实现手势处理方法,当手势被识别时,执行自定义的事件: ``` - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { NSArray *points = [gestureRecognizer valueForKey:@"touches"]; CGPoint startPoint = [[points firstObject] locationInView:self.view]; CGPoint endPoint = [[points lastObject] locationInView:self.view]; // 计算手势的方向 CGFloat dx = endPoint.x - startPoint.x; CGFloat dy = endPoint.y - startPoint.y; if (dx > 0 && dy < 0 && fabs(dx) > fabs(dy)) { // 手势为向右上方,执行自定义事件 [self doSomething]; } } } ``` 在上面的代码中,我们使用了touches属性获取手势的起和终,然后计算手势的方向,最后根据手势的方向执行自定义事件。 注意:在创建手势识别器时,需要指定手势的类型,例如: ``` UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRecognizer]; ``` 上面的代码创建了一个向右滑动的手势识别器,并将其添加到视图上。当手势被识别时,会执行handleSwipe:方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值