gpt4 book ai didi

ios - UIButton 在第一次点击后不可点击

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:53 30 4
gpt4 key购买 nike

我试图在单击按钮时从底部引入一个 subview 。但只有第一次按钮是可点击的。对于动画按钮不可点击后的第二次点击。

这是代码。

class AnimateView: UIView {
var button: UIButton!
var menuView: UIView!
var mainView: UIView!

override init(frame: CGRect) {
super.init(frame: frame)
mainView = UIView(frame: CGRect(x: 0, y: 0, width:
self.frame.size.width, height: self.frame.size.height))

mainView.clipsToBounds = true
mainView.backgroundColor = .clear
mainView.isUserInteractionEnabled = true
mainView.isExclusiveTouch = true
self.addSubview(mainView)

let theRect = CGRect(x: self.frame.size.width / 2 - 44 / 2, y: 0,
width: 44, height: 44)
button = UIButton(frame: theRect)
check.layer.cornerRadius = btnView.frame.size.height / 2
mainView.addSubview(self.check)
mainView.bringSubview(toFront: check)
check.addTarget(self, action: #selector(buttonClick(_:)),
for:.touchUpInside)

let newRect = CGRect(x: 0, y: check.frame.height, width:
self.frame.size.width, height: self.mainView.frame.size.height)
menuView = UIView(frame: newRect)
menuView.backgroundColor = .blue
mainView.addSubview(menuView)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func buttonClick(_ sender: UIButton) {
toggleMenu()
}


func toggleMenu() {
if check.transform == CGAffineTransform.identity {
UIView.animate(withDuration: 1, animations: {
self.check.transform = CGAffineTransform(scaleX: 12, y: 12)
self.mainView.transform = CGAffineTransform(translationX: 0, y: -120)
self.check.transform = CGAffineTransform(rotationAngle: self.radians(180)) // 180 is 3.14 radian
self.setNeedsLayout()
}) { (true) in
UIView.animate(withDuration: 0.5, animations: {

})
}
} else {
UIView.animate(withDuration: 1, animations: {
// .identity sets to original position
//self.btnView.transform = .identity
self.mainView.transform = .identity
self.button.transform = .identity
})
}

}
}

像这样从另一个 UIView 类调用这个类

class MainViewClass: UIView {
var animateView: AnimateView!

override init(frame: CGRect) {
animateView = AnimateView(frame: CGRect(x: 0, y: self.frame.size.height - 44 - 44 - 20, width: self.frame.size.width, height: 122+44))
//animateView.clipsToBounds = true
self.addSubview(animateView)
self.bringSubview(toFront: animateView)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

当屏幕第一次出现时:

enter image description here

最初按钮是可点击的,当它被点击时它会随着 subview 向上移动。 enter image description here

现在按钮不可点击。当我看到按钮的框架时,它与屏幕最初出现时相同,按钮触摸屏幕底部,即使它向上移动。我的目的是在单击按钮时从底部显示带有动画的屏幕,并在第二次单击时移动到默认位置。

但是如果我将按钮作为 subview 放在自己内部,而不是作为 mainView 的 subview ,那么按钮是可点击的,但即使在 View 被动画化并向上移动后它仍保持在相同的位置。

self.addSubview(按钮)

enter image description here

最佳答案

所以,我终于得到了我想要实现的动画前后的解决方案。

UIButton在动画结束后没有收到任何触摸事件的原因是,在动画结束后,UIButton向上移动,因为它是mainView的 subview 。因此,它超出了其 superView 的范围,并且不响应任何点击事件。虽然我移动了按钮和它的 superView,也就是 mainView,但是按钮没有响应触摸事件。然后,我使用 hitTest 测试绑定(bind)的按钮是否在 mainView 中:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let pointForTargetView: CGPoint = button.convert(point, from: mainView)
if button.bounds.contains(pointForTargetView) {
print("Button pressed")
return button.hitTest(pointForTargetView, with:event)
}
return super.hitTest(point, with: event)
}

这个if条件:

button.bounds.contains(pointForTargetView)

在动画结束后按下按钮时返回 false。

因此,我需要在父 View 框架之外的 subview 上捕获触摸事件。

使用 hitTest() 解决了我的问题。这是帮助我的链接。 Capturing touches on a subview outside the frame of its superview using hitTest:withEvent:

swift 3

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

if clipsToBounds || isHidden || alpha == 0 {
return nil
}

for subview in subviews.reversed() {
let subPoint = subview.convert(point, from: self)
if let result = subview.hitTest(subPoint, with: event) {
return result
}
}

return nil
}

屏幕截图:当屏幕第一次出现时,按钮在屏幕底部, subview 隐藏在按钮下方。

enter image description here

当按钮被点击时,mainView 向上移动,按钮向上移动,因为它是 mainView 的 subview 。

enter image description here

当再次点击按钮时,mainView 会回到之前的位置,按钮也是如此。

enter image description here

关于ios - UIButton 在第一次点击后不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46042830/

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