Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UIViewPropertyAnimator通过触摸中断动画_Ios_Swift_Touchesbegan_Uiviewpropertyanimator - Fatal编程技术网

Ios 使用UIViewPropertyAnimator通过触摸中断动画

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

我试图通过触摸屏来控制动画

当我触摸屏幕时,视图的alpha变为0

但如果在alpha更改为0时再次接触

然后alpha再次变为1(中断使alpha值为0的动画)

所以我写

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
    IsInterruptable
    设置为true,因为它们的默认值为true
  • 调用
    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
    IsInterruptable
    设置为true,因为它们的默认值为true
  • 调用
    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,这非常有效!!谢谢!!我没有意识到我应该初始化动画对象,因为它工作得很好!!谢谢!!我没有意识到我应该初始化动画对象