首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2 GestureRecognizer之间的差异

2 GestureRecognizer之间的差异
EN

Stack Overflow用户
提问于 2016-04-20 17:55:51
回答 1查看 472关注 0票数 0

我有两个GesturerRecognizers,一个是定制的3 3DTouch,另一个是单UITapGestureRegognizer。我想要做的是,如果你点击一次,它执行一个圣格,如果你去做的3 3dtouch之一,你可以得到另一个视图。我尝试了很多事情,但它不起作用(我甚至试过用计时器,但总是0在开始,不管它的3 3dtouch是否。)我的3 3dtouch自定义实现:

代码语言:javascript
复制
//Without this import line, you'll get compiler errors when implementing your touch methods since they aren't part of the UIGestureRecognizer superclass
import UIKit.UIGestureRecognizerSubclass

//Since 3D Touch isn't available before iOS 9, we can use the availability APIs to ensure no one uses this class for earlier versions of the OS.
@available(iOS 9.0, *)
public class ForceTouchGestureRecognizer: UIGestureRecognizer {

    var timer = NSTimer()
    var counter = Double()

    //Since it also doesn't make sense to have our force variable as a settable property, I'm using a private instance variable to make our public force property read-only
    private var _force: CGFloat = 0.0

    //Because we don't know what the maximum force will always be for a UITouch, the force property here will be normalized to a value between 0.0 and 1.0.
    public var force: CGFloat { get { return _force } }
    public var maximumForce: CGFloat = 4.0

    func timerAction() {
        counter += 1
    }


    override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        counter = 0

        timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        print("COUNTER: \(counter)")

        normalizeForceAndFireEvent(.Began, touches: touches)
    }

    override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)

        normalizeForceAndFireEvent(.Changed, touches: touches)
    }

    override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        print("COUNTER: \(counter)")

        normalizeForceAndFireEvent(.Ended, touches: touches)
        timer.invalidate()

    }

    override public func touchesCancelled(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesCancelled(touches, withEvent: event)

        normalizeForceAndFireEvent(.Cancelled, touches: touches)
    }

    func normalizeForceAndFireEvent(state: UIGestureRecognizerState, touches: Set<UITouch>) {
        //Putting a guard statement here to make sure we don't fire off our target's selector event if a touch doesn't exist to begin with.
        guard let firstTouch = touches.first else {
            return
        }

        //Just in case the developer set a maximumForce that is higher than the touch's maximumPossibleForce, I'm setting the maximumForce to the lower of the two values.
        maximumForce = min(firstTouch.maximumPossibleForce, maximumForce)

        //Now that I have a proper maximumForce, I'm going to use that and normalize it so the developer can use a value between 0.0 and 1.0.
        _force = firstTouch.force / maximumForce

        //Our properties are now ready for inspection by the developer. By setting the UIGestureRecognizer's state property, the system will automatically send the target the selector message that this recognizer was initialized with.
        self.state = state
    }

    //This function is called automatically by UIGestureRecognizer when our state is set to .Ended. We want to use this function to reset our internal state.
    public override func reset() {
        super.reset()

        _force = 0.0
    }
}

在我看来,这就是我所做的

代码语言:javascript
复制
     self.forceTouchRecognizer = ForceTouchGestureRecognizer(target: self, action: #selector(PLView.handleForceTouchGesture(_:)))
    self.addGestureRecognizer(forceTouchRecognizer)

     self.nowTap = UITapGestureRecognizer(target: self, action: #selector(StoryTableViewController.didTapRightNow2(_:)))
    self.addGestureRecognizer(nowTap)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-21 20:16:56

如果我正确地理解了您的意思,那么您希望将两个不同的GestureRecognizers添加到同一个UIView中。一个检测正常的抽头,另一个用力检测抽头。

对于普通的点击,您可以使用UITapGestureRecognizer。对于力的点击,你必须创建你自己的自定义手势识别器,它监视触摸的力量,决定力量是否足够高,是否有资格做一个力量点击手势。

以下是自定义的强制点击手势识别器:

代码语言:javascript
复制
import UIKit.UIGestureRecognizerSubclass

@available(iOS 9.0, *)
public class ForceTapGestureRecognizer: UIGestureRecognizer {

    private var forceTapRecognized = false

    override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        forceTapRecognized = false
    }

    override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesMoved(touches, withEvent: event)
        guard let touch = touches.first else { return }
        if touch.force >= touch.maximumPossibleForce {
            forceTapRecognized = true
        }
    }

    override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
        super.touchesEnded(touches, withEvent: event)
        state = forceTapRecognized ? .Ended : .Failed
    }
}

然后,可以将两个识别器添加到视图中,并为每个识别器添加一个操作。

要使这两个识别器同时工作,您必须告诉UITapGestureRecognizer,当ForceTapGestureRecognizer没有检测到一个力抽头时,它应该只检测一个抽头。你用requireGestureRecognizerToFail(_:)来做这件事。如果不设置此设置,则只会识别正常的水龙头:

代码语言:javascript
复制
class ViewController: UIViewController {

    let touchView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))

    override func viewDidLoad() {
        super.viewDidLoad()

        let touchView = UIView()
        view.addSubview(touchView)

        let forceTapRecognizer = ForceTapGestureRecognizer(target: self, action: #selector(ViewController.didForceTap(_:)))
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(_:)))
        tapRecognizer.requireGestureRecognizerToFail(forceTapRecognizer)
        touchView.addGestureRecognizer(forceTapRecognizer)
        touchView.addGestureRecognizer(tapRecognizer)
    }

    func didTap(recognizer: UITapGestureRecognizer) {
        print("tap")
    }

    func didForceTap(recognizer: ForceTapGestureRecognizer) {
        print("force tap")
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36751580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档