Ios 图像移动到按钮上方后,按钮是否发生变化?

Ios 图像移动到按钮上方后,按钮是否发生变化?,ios,swift,xcode,Ios,Swift,Xcode,我正在制作一个“幻灯片确认”按钮,设置为自定义UIButton+常规UIImageView,图像被传递到按钮,按钮收听触摸并相应地移动图像。 除了按钮被释放后的一个细节之外,所有的东西都工作得很好,所有的东西都应该回到它们的初始状态,但是按钮保持不变,就像它的alpha值减半一样 我对alpha进行了注释,因为按钮将要隐藏,但我禁用了,一旦遇到此问题,无论是否设置alpha,问题都会发生 这是自定义按钮的代码 class SlideButton: UIButton { var ar

我正在制作一个“幻灯片确认”按钮,设置为自定义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)
    }
 }
class SlideButton:UIButton{
变量arrowImage:UIImageView?
变量初始位置:CGPoint?
变量初始箭头位置:CGPoint?
func设置箭头(箭头:UIImageView){
箭头图像=箭头
初始箭头位置=arrow.frame.origin
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
让我们先接触!
让位置=touch.location(在:self.superview中)
初始位置=位置
UIView.animate(持续时间:0.2){
//self.alpha=0.0
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
UIView.animate(持续时间:0.3){
self.arrowImage!.frame.origin=self.initialArrowPosition!
//self.alpha=1.0
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
让我们先接触!
让currentLocation=touch.location(在:self.superview中)
arrowImage!.frame.origin.x=initialArrowPosition!.x-initialLocation!.x+(currentLocation.x)
}
}

您可能还必须添加对touchesCancelled的覆盖,因为您可能并不总是点击ToucheSensed端点。

您记得为您的一种
触摸方法调用
super

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
override func touchsbegind(touch:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)

但是对于其他的
触摸
方法,您忘记了这样做,因此您破坏了按钮的核心功能。

问题是,通过对UIButton手动执行
触摸
,您正在干扰按钮自身的触摸响应。您忘记为所有这些按钮调用
超级
,而您需要这样做真的需要这样做。但是最好不要这样做。谢谢@matt,它修复了它!触摸按钮的默认行为是改变了它一点,由于忘记做超级呼叫,我打破了它。我希望你能把它作为一个答案发布,因为它是正确的答案!我很好奇,你如何制作幻灯片来确认按钮n如果不是这样的话?谢谢!我会加上!但这并不能解决问题