gpt4 book ai didi

ios - 2 GestureRecognizer 之间的区别

转载 作者:行者123 更新时间:2023-11-28 12:50:10 25 4
gpt4 key购买 nike

我有 2 个 GesturerRecognizer,一个是自定义 3DTouch,另一个是单个 UITapGestureRegognizer。我想做的是,如果你点击一次它执行一个 segue,如果你去 trought 3dtouch 一个你会得到另一个 View 。我已经尝试了很多东西,但它没有用(我什至尝试过计时器,但在开始时总是 0,不管它是否是 3dtouch。)我的 3dtouch 自定义实现:

//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
}
}

这就是我在 View 中所做的:

     self.forceTouchRecognizer = ForceTouchGestureRecognizer(target: self, action: #selector(PLView.handleForceTouchGesture(_:)))
self.addGestureRecognizer(forceTouchRecognizer)

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

最佳答案

如果我对你的理解是正确的,你想将 2 个不同的 GestureRecognizers 添加到同一个 UIView。一种检测正常敲击,另一种检测用力敲击。

对于正常的点击,您可以使用 UITapGestureRecognizer。对于用力敲击,您必须创建自己的自定义手势识别器,它会监控触摸的力度,确定力度是否足够高以符合用力敲击手势的条件。

这是自定义用力点击手势识别器:

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
}
}

然后您可以将两个识别器添加到您的 View 中,并为每个识别器添加一个 Action 。

要使两个识别器同时工作,您必须告诉 UITapGestureRecognizer 它应该只检测点击,而 ForceTapGestureRecognizer 没有检测到用力点击。您可以使用 requireGestureRecognizerToFail(_:) 执行此操作。如果您不设置此项,则只会识别普通的敲击:

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")
}
}

关于ios - 2 GestureRecognizer 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com