我正在制作一个“滑动确认”按钮,设置是自定义 UIButton + 常规 UIImageView,图像被传递到按钮,按钮监听触摸并相应地移动图像。 一切都很好,除了按钮释放后的一个细节外,一切都应该回到初始状态,但按钮保持变化,就好像它的 alpha 减半一样
我评论了 alpha,因为该按钮将要隐藏,但是一旦我遇到这个问题,我就禁用了它,无论是否设置 alpha,问题都会发生。
这是自定义按钮的代码
class SlideButton: UIButton {
var arrowImage: UIImageView?
var initialLocation: CGPoint?
var initialArrowPosition: CGPoint?
func setArrow(arrow:UIImageView){
arrowImage = arrow
initialArrowPosition = arrow.frame.origin
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch = touches.first!
let location = touch.location(in: self.superview)
initialLocation = location
UIView.animate(withDuration: 0.2) {
//self.alpha = 0.0
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.3) {
self.arrowImage!.frame.origin = self.initialArrowPosition!
//self.alpha = 1.0
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let currentLocation = touch.location(in: self.superview)
arrowImage!.frame.origin.x = initialArrowPosition!.x - initialLocation!.x + (currentLocation.x)
}
}
最佳答案
您要记得为您的touches
方法之一调用super
:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
但对于其他 touches
方法,您忘记了这样做,因此您破坏了按钮的核心功能。
关于ios - 图像移过后按钮会改变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564068/