为了账号安全,请及时绑定邮箱和手机立即绑定

UITapGestureRecognizer-使它在触地而不是触地工作吗?

UITapGestureRecognizer-使它在触地而不是触地工作吗?

慕码人2483693 2019-11-27 09:55:26
我使用tap事件的时间非常敏感,因此我很好奇是否有可能在用户简单按下时激活UITapGestureRecognizer,而不是要求他们同时按下吗?
查看完整描述

3 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

创建您的自定义TouchDownGestureRecognizer子类,并在touches中实现手势。


TouchDownGestureRecognizer.h


#import <UIKit/UIKit.h>


@interface TouchDownGestureRecognizer : UIGestureRecognizer


@end

TouchDownGestureRecognizer.m


#import "TouchDownGestureRecognizer.h"

#import <UIKit/UIGestureRecognizerSubclass.h>


@implementation TouchDownGestureRecognizer

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

    if (self.state == UIGestureRecognizerStatePossible) {

        self.state = UIGestureRecognizerStateRecognized;

    }

}


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

    self.state = UIGestureRecognizerStateFailed;

}


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

    self.state = UIGestureRecognizerStateFailed;

}



@end

实施:


#import "TouchDownGestureRecognizer.h"

    TouchDownGestureRecognizer *touchDown = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchDown:)];

    [yourView addGestureRecognizer:touchDown];


-(void)handleTouchDown:(TouchDownGestureRecognizer *)touchDown{

    NSLog(@"Down");

}

迅捷的实现:


import UIKit

import UIKit.UIGestureRecognizerSubclass


class TouchDownGestureRecognizer: UIGestureRecognizer

{

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)

    {

        if self.state == .Possible

        {

            self.state = .Recognized

        }

    }


    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent)

    {

        self.state = .Failed

    }


    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent)

    {

        self.state = .Failed

    }

}

这是2017年要粘贴的Swift语法:


import UIKit.UIGestureRecognizerSubclass


class SingleTouchDownGestureRecognizer: UIGestureRecognizer {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {

        if self.state == .possible {

            self.state = .recognized

        }

    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {

        self.state = .failed

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {

        self.state = .failed

    }

}

请注意,这是的替代品UITap。所以在像这样的代码中


func add(tap v:UIView, _ action:Selector) {

    let t = UITapGestureRecognizer(target: self, action: action)

    v.addGestureRecognizer(t)

}

您可以安全地交换到...。


func add(hairtriggerTap v:UIView, _ action:Selector) {

    let t = SingleTouchDownGestureRecognizer(target: self, action: action)

    v.addGestureRecognizer(t)

}

测试表明它不会被多次调用。它可以作为替代品。您可以在两个通话之间进行交换。


查看完整回答
反对 回复 2019-11-27
?
三国纷争

TA贡献1804条经验 获得超7个赞

使用UILongPressGestureRecognizer并将其设置minimumPressDuration为0。在UIGestureRecognizerStateBegan状态期间,它将像着陆一样。


对于Swift 4

func setupTap() {

    let touchDown = UILongPressGestureRecognizer(target:self, action: #selector(didTouchDown))

    touchDown.minimumPressDuration = 0

    view.addGestureRecognizer(touchDown)

}


@objc func didTouchDown(gesture: UILongPressGestureRecognizer) {

    if gesture.state == .began {

        doSomething()

    }

}

对于Objective-C

例:


-(void)setupLongPress

{

   self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];

   self.longPress.minimumPressDuration = 0;

   [self.view addGestureRecognizer:self.longPress];

}


-(void)didLongPress:(UILongPressGestureRecognizer *)gesture

{

   if (gesture.state == UIGestureRecognizerStateBegan){

      [self doSomething];

   }

}


查看完整回答
反对 回复 2019-11-27
?
慕容森

TA贡献1853条经验 获得超18个赞

Swift(无子类)

这是Swift版本,类似于Rob Caraway的Objective-C答案。


想法是使用minimumPressDuration设置为零的长按手势识别器,而不是使用轻击手势识别器。这是因为长按手势识别器报告触摸开始事件,而点击手势没有报告。


import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var myView: UIView!


    override func viewDidLoad() {

        super.viewDidLoad()


        // Add "long" press gesture recognizer

        let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler))

        tap.minimumPressDuration = 0

        myView.addGestureRecognizer(tap)

    }


    // called by gesture recognizer

    @objc func tapHandler(gesture: UITapGestureRecognizer) {


        // handle touch down and touch up events separately

        if gesture.state == .began {

            // do something...

            print("tap down")

        } else if gesture.state == .ended { // optional for touch up event catching

            // do something else...

            print("tap up")

        }

    }

}


查看完整回答
反对 回复 2019-11-27
  • 3 回答
  • 0 关注
  • 600 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信