gpt4 book ai didi

swift - UIPanGestureRecognizer 的重写目标

转载 作者:行者123 更新时间:2023-11-30 12:48:29 25 4
gpt4 key购买 nike

我有一个基类,它处理单元格的一些基本动画。在我的子类中,我已经覆盖了识别器目标并想做一些其他事情,但翻译始终是 0 因为我在基类中调用翻译被重置为 .zero .

有没有办法让我的基类执行基本动画并使用子类覆盖该方法并执行其他一些操作,如下所示?

我有一个基类:

class Base: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
addGestureRecognizer(gesture)
}

func handlePan(recognizer: UIPanGestureRecognizer) {
if recognizer.state == .changed {
let translation = recognizer.translation(in: self)

...

recognizer.setTranslation(.zero, in: self)
}
}
}

还有我的子类:

class Sub: Base {

override func handlePan(recognizer: UIPanGestureRecognizer) {
super.handlePan(recognizer: recognizer)

if recognizer.state == .changed {
let translation = recognizer.translation(in: self)

print(translation.x) // is always 0

recognizer.setTranslation(.zero, in: self)
}
}
}

最佳答案

在基类中,您可以将通用功能拆分为不同的功能吗?这样你就不需要调用 super.handlePan:

class Base: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
addGestureRecognizer(gesture)
}

func handlePan(recognizer: UIPanGestureRecognizer) {
if recognizer.state == .changed {
let translation = recognizer.translation(in: self)
performBaseAnimation(recognizer)
recognizer.setTranslation(.zero, in: self)
}
}
func performBaseAnimation(_ recognizer: UIPanGestureRecognizer) {
// perform common actions here
}
}
class Sub: Base {
override func handlePan(recognizer: UIPanGestureRecognizer) {
super.handleBaseAnimation(recognizer)
if recognizer.state == .changed {
// perform subclass actions here
}
}
}

关于swift - UIPanGestureRecognizer 的重写目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310155/

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