Ios 子类化UIButton和覆盖触摸事件-不工作

Ios 子类化UIButton和覆盖触摸事件-不工作,ios,swift,uibutton,uiviewanimation,Ios,Swift,Uibutton,Uiviewanimation,我需要一个项目中所有按钮的缩放弹簧动画。 所以我将UIButton子类化并覆盖触摸事件函数 import UIKit class UIAnimatedButton: UIButton { override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { UIView.animateWithDuration(0.1, animations: { () -> Void in

我需要一个项目中所有按钮的缩放弹簧动画。 所以我将UIButton子类化并覆盖触摸事件函数

import UIKit

class UIAnimatedButton: UIButton {

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.transform = CGAffineTransformMakeScale(0.8, 0.8)

    })
    super.touchesBegan(touches, withEvent: event)

}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {


    UIView.animateWithDuration(0.5,
        delay: 0,
        usingSpringWithDamping: 0.2,
        initialSpringVelocity: 6.0,
        options: UIViewAnimationOptions.AllowUserInteraction,
        animations: { () -> Void in
            self.transform = CGAffineTransformIdentity
    }) { (Bool) -> Void in
        super.touchesCancelled(touches, withEvent: event)
    }

}


override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    UIView.animateWithDuration(0.5,
        delay: 0,
        usingSpringWithDamping: 0.2,
        initialSpringVelocity: 6.0,
        options: UIViewAnimationOptions.AllowUserInteraction,
        animations: { () -> Void in
            self.transform = CGAffineTransformIdentity
        }) { (Bool) -> Void in
            super.touchesEnded(touches, withEvent: event)
    }
  }
} 
导入UIKit
类UIAnimatedButton:UIButton{
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
animateWithDuration(0.1,动画:{()->Void in
self.transform=CGAffineTransformMakeScale(0.8,0.8)
})
super.touchsbegind(touchs,withEvent:event)
}
覆盖func touchesCancelled(touchs:Set!,withEvent:UIEvent!){
UIView.animateWithDuration(0.5,
延迟:0,
使用带阻尼的弹簧:0.2,
初始速度:6.0,
选项:UIViewAnimationOptions.AllowUserInteraction,
动画:{()->Void in
self.transform=CGAffineTransformity
}){(Bool)->Void in
super.touchscancelled(touchs,withEvent:event)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
UIView.animateWithDuration(0.5,
延迟:0,
使用带阻尼的弹簧:0.2,
初始速度:6.0,
选项:UIViewAnimationOptions.AllowUserInteraction,
动画:{()->Void in
self.transform=CGAffineTransformity
}){(Bool)->Void in
super.touchesend(touchs,withEvent:event)
}
}
} 
这在快速点击时效果很好,但当我长时间(1-2秒)触摸按钮时,我不会在动作事件中得到任何润色。 当我将其切换回常规UIButton时,一切都正常


知道为什么会发生这种情况吗?

而不是在
touchscancelled
touchsended
方法的完成块中调用super,我在那里调用了
self.sendActionsForControlEvents(UIControlEvents.TouchUpInside)

还不确定具体原因,但您需要调用
super.touchsended(触摸,带有:事件)
动画外部

So(Swift 5)

override func touchesend(touch:Set,带有事件:UIEvent?){
UIView.animate(持续时间:0.5,
延迟:0,
使用带阻尼的弹簧:0.2,
初始速度:6.0,
选项:UIView.AnimationOptions.allowUserInteraction,
动画:{()->Void in
self.transform=CGAffineTransform.identity
}){(Bool)->Void in
}
super.touchesend(触摸,带有:事件)
}

如果文档中未另行指定,则重写方法中要做的第一件事是调用super。在动画块中调用它有点奇怪。如果在动画结束前调用super,动画将不会动画。您有办法制作此动画吗?请改为重写UIGestureRecognitor m方法尝试使用UIButton是其子类的UIControl类的方法
BegingWithTouch:withEvent:
continueTrackingWithTouch:withEvent:
endTrackingWithTouch:withEvent:
。这不起作用…动画未被设置动画
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    UIView.animate(withDuration: 0.5,
        delay: 0,
        usingSpringWithDamping: 0.2,
        initialSpringVelocity: 6.0,
        options: UIView.AnimationOptions.allowUserInteraction,
        animations: { () -> Void in
            self.transform = CGAffineTransform.identity
        }) { (Bool) -> Void in
    }
    super.touchesEnded(touches, with: event)
}