亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 631 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號