gpt4 book ai didi

ios - 为什么在为 UIView 设置动画时没有调用我的 UITapGestureRecognizer?

转载 作者:行者123 更新时间:2023-12-04 07:34:32 24 4
gpt4 key购买 nike

我用 UIImageView 做了一个 toast View 在其中,我想要做的是每次用户点击 ImageView 时, toast 都会自行解除。自然我配置了一个UITapGestureRecognizer到我的 ImageView ,但没有调用选择器函数。这是我所做的:

class ToastView: UIView {

private var icon: UIImageView!

init() {
super.init(frame: .zero)
setupViews()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
setupViews()
}

override func layoutSubviews() {
super.layoutSubviews()
setupConstraints()
}

private func setupViews() {
translatesAutoresizingMaskIntoConstraints = false
layer.cornerRadius = 8
isUserInteractionEnabled = true

icon = UIImageView()
icon.translatesAutoresizingMaskIntoConstraints = false
icon.image = UIImage(named: "someImage")
icon.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(iconHandler))
icon.addGestureRecognizer(tapGesture)
addSubview(icon)
}

private func setupConstraints() {
NSLayoutConstraint.activate([
topAnchor.constraint(equalTo: superview!.topAnchor, constant: 55),
leadingAnchor.constraint(equalTo: superview!.leadingAnchor, constant: 16),
trailingAnchor.constraint(equalTo: superview!.trailingAnchor, constant: -16),
icon.heightAnchor.constraint(equalToConstant: 16),
icon.widthAnchor.constraint(equalToConstant: 16),
icon.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
icon.centerYAnchor.constraint(equalTo: centerYAnchor),
])
}

@objc private func iconHandler(_ sender: UITapGestureRecognizer) {
// This function is not called
print("handle icon")
}
}
经过一番研究,我试图给 ToastView手势识别器而不是 ImageView 。所以在我的自定义 UIViewController 中显示 Toast 时,我确实提供了点击手势识别器像这样的类:
class CustomViewController: UIViewController {
private var isShowingToast: Bool = false
private lazy var toast: ToastView = {
let toast = ToastView()
toast.isUserInteractionEnabled = true
toast.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissToast)))
return toast
}()

func showToastWithMessage() {
if !isShowingToast {
view.addSubview(toast)
UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: { [weak self] in
self?.toast.alpha = 1
self?.toast.frame.origin.y += 10
self?.isShowingToast = true
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: 5.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: { [weak self] in
self?.toast.alpha = 0
self?.toast.frame.origin.y -= 10
}, completion: { [weak self] _ in
self?.isShowingToast = false
self?.toast.removeFromSuperview()
})
})
}
}

@objc private func dismissToast() {
// This functions does not get called as well
print("dismiss")
}
}
不幸的是,dismiss 函数不会打印到控制台。有没有办法解决这个问题?

最佳答案

看起来这是由于您的动画而发生的。 View 始终处于动画状态并阻止点击手势。您可以尝试使用延迟调用它,而不是为您的动画添加延迟。

func showToastWithMessage() {
if !isShowingToast {
view.addSubview(toast)
UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: { [weak self] in
self?.toast.alpha = 1
self?.toast.frame.origin.y += 10
self?.isShowingToast = true
}, completion: { _ in
print("Completion")
})

DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: { [weak self] in
self?.toast.alpha = 0
self?.toast.frame.origin.y -= 10
}, completion: { [weak self] _ in
self?.isShowingToast = false
self?.toast.removeFromSuperview()
})
}

}
}
这样 View 会在 5 秒后进入动画状态,而不是 5 秒延迟。

关于ios - 为什么在为 UIView 设置动画时没有调用我的 UITapGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67799149/

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