Ios 使用UIViewPropertyAnimator通过触摸中断动画
我试图通过触摸屏来控制动画 当我触摸屏幕时,视图的alpha变为0 但如果在alpha更改为0时再次接触 然后alpha再次变为1(中断使alpha值为0的动画) 所以我写Ios 使用UIViewPropertyAnimator通过触摸中断动画,ios,swift,touchesbegan,uiviewpropertyanimator,Ios,Swift,Touchesbegan,Uiviewpropertyanimator,我试图通过触摸屏来控制动画 当我触摸屏幕时,视图的alpha变为0 但如果在alpha更改为0时再次接触 然后alpha再次变为1(中断使alpha值为0的动画) 所以我写 class MainViewController: UIViewController { var showAnimation:UIViewPropertyAnimator! var hideAnimation:UIViewPropertyAnimator! var isHiding:Bool = false overrid
class MainViewController: UIViewController {
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
var isHiding:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
showAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 1
})
hideAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 0
})
showAnimation.isUserInteractionEnabled = true
showAnimation.isInterruptible = true
hideAnimation.isUserInteractionEnabled = true
hideAnimation.isInterruptible = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isHiding = !isHiding
if self.isHiding {
self.hideAnimation.startAnimation()
self.showAnimation.stopAnimation(true)
}else{
self.hideAnimation.stopAnimation(true)
self.showAnimation.startAnimation()
}
}
}
class MainViewController:UIViewController{
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
变量ishing:Bool=false
重写func viewDidLoad(){
super.viewDidLoad()
self.view.backgroundColor=.blue
showAnimation=UIViewPropertyAnimator(持续时间:2,曲线:.easeInOut,动画:{
self.view.alpha=1
})
hideAnimation=UIViewPropertyAnimator(持续时间:2,曲线:.easeInOut,动画:{
self.view.alpha=0
})
showAnimation.isUserInteractionEnabled=true
showAnimation.isInterruptible=真
hideAnimation.isUserInteractionEnabled=true
hideAnimation.isInterruptible=真
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
ishing=!ishing
如果自私自利{
self.hideAnimation.startAnimation()
self.showAnimation.stopAnimation(真)
}否则{
self.hideAnimation.stopAnimation(真)
self.showAnimation.startAnimation()
}
}
}
但是只有在动画块完成后才开始调用touchesBegin
如何解决此问题这里有两件事您需要知道:
- 初始化
后,不需要将UIViewPropertyAnimator
和isUserInteractionEnabled
设置为true,因为它们的默认值为trueIsInterruptable
- 调用
后,stopAnimation
将变得无效,您无法调用UIViewPropertyAnimator
使其再次工作。因此,您需要在停止后重新初始化startAnimation
和showAnimation
hideAnimation
class MainViewController: UIViewController {
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
var isHiding:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isHiding = !isHiding
if self.isHiding {
self.showAnimation?.stopAnimation(true)
self.hideAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 0.1
})
self.hideAnimation.startAnimation()
}else{
self.hideAnimation?.stopAnimation(true)
self.showAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 1
})
self.showAnimation.startAnimation()
}
}
}
class MainViewController:UIViewController{
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
变量ishing:Bool=false
重写func viewDidLoad(){
super.viewDidLoad()
self.view.backgroundColor=.blue
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
ishing=!ishing
如果自私自利{
self.showAnimation?.stopAnimation(真)
self.hideAnimation=UIViewPropertyAnimator(持续时间:2,曲线:.easeInOut,动画:{
self.view.alpha=0.1
})
self.hideAnimation.startAnimation()
}否则{
self.hideAnimation?.stopAnimation(真)
self.showAnimation=UIViewPropertyAnimator(持续时间:2,曲线:.easeInOut,动画:{
self.view.alpha=1
})
self.showAnimation.startAnimation()
}
}
}
这里有两件事你需要知道:
- 初始化
后,不需要将UIViewPropertyAnimator
和isUserInteractionEnabled
设置为true,因为它们的默认值为trueIsInterruptable
- 调用
后,stopAnimation
将变得无效,您无法调用UIViewPropertyAnimator
使其再次工作。因此,您需要在停止后重新初始化startAnimation
和showAnimation
hideAnimation
class MainViewController: UIViewController {
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
var isHiding:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isHiding = !isHiding
if self.isHiding {
self.showAnimation?.stopAnimation(true)
self.hideAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 0.1
})
self.hideAnimation.startAnimation()
}else{
self.hideAnimation?.stopAnimation(true)
self.showAnimation = UIViewPropertyAnimator(duration: 2, curve: .easeInOut, animations: {
self.view.alpha = 1
})
self.showAnimation.startAnimation()
}
}
}
class MainViewController:UIViewController{
var showAnimation:UIViewPropertyAnimator!
var hideAnimation:UIViewPropertyAnimator!
变量ishing:Bool=false
重写func viewDidLoad(){
super.viewDidLoad()
self.view.backgroundColor=.blue
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
ishing=!ishing
如果自私自利{
self.showAnimation?.stopAnimation(真)
self.hideAnimation=UIViewPropertyAnimator(持续时间:2,曲线:.easeInOut,动画:{
self.view.alpha=0.1
})
self.hideAnimation.startAnimation()
}否则{
self.hideAnimation?.stopAnimation(真)
self.showAnimation=UIViewPropertyAnimator(持续时间:2,曲线:.easeInOut,动画:{
self.view.alpha=1
})
self.showAnimation.startAnimation()
}
}
}
您是否尝试过使用UITapgestureRecognitizer
而不是touchesBegind?完全一样!但是如果不将alpha设置为0,我认为这是可行的,我将alpha设置为0.1您是否尝试使用uitappesturerecognizer
而不是touchsbegind?这是一样的!但是如果不将alpha设置为0,我认为这是可行的,我将alpha设置为0.1,这非常有效!!谢谢!!我没有意识到我应该初始化动画对象,因为它工作得很好!!谢谢!!我没有意识到我应该初始化动画对象