【问题标题】:UIButton that has an interupptable animation with UIViewPropertyAnimatorUIButton 具有可与 UIViewPropertyAnimator 交互的动画
【发布时间】:2018-04-26 19:51:39
【问题描述】:

我想要一个按钮在用户触摸按钮时展开(放大),并在触摸离开时缩回正常。类似于 iOS 11 之前的 iOS 股票计算器。这是我创建按钮的子类的类。它使用 touchesbegan 和 touchesended 来启动、暂停和反转动画,但它没有正确收缩,也没有动画。

class UIOutlineButton: UIButton {

var squishAnimation:UIPropertyAnimator!

override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)



    squishAnimation = UIViewPropertyAnimator(duration: 0.5, dampingRatio: 30, animations: {
        let scale = self.transform.scaledBy(x: 0.8, y: 0.8)

        let rotate = self.transform.rotated(by: 0.3)
        self.transform.concatenating(rotate)
        self.transform = scale
    })
    squishAnimation.isReversed = true
    squishAnimation.startAnimation()


    self.next?.touchesBegan(touches, with: event)

}

override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    colorAnimation = UIViewPropertyAnimator(duration: 0.5, dampingRatio: 30, animations: {


    })

    squishAnimation = UIViewPropertyAnimator(duration: 0.5, dampingRatio: 30, animations: {
        self.transform = CGAffineTransform.identity
    })

    squishAnimation.stopAnimation(true)

    squishAnimation.startAnimation()
}


}

【问题讨论】:

  • 好的,所以把它分解成几个步骤。您可以使用触地内部事件来触发增长吗?然后补内+补外使其缩回正常?对于动画,请查看 UIView 方法,例如 animate(duration:animations)。看看你能不能解决。如果没有,请发布您的代码并告诉我们它如何无法满足您的需求,我们会为您提供帮助。

标签: ios iphone swift button


【解决方案1】:

这是一个小 sn-p,它将“增长”一个 UIView,然后在短暂延迟后将其恢复为原始大小:

  /// Expands & Then Returns A UIView To It's Original Size
  ///
  /// - Parameters:
  ///   - view: UIView
  ///   - duration: Double
  ///   - enable: (Void)
  func animate(view: UIView, duration: Double, enable: @escaping () -> ()){

       view.isUserInteractionEnabled = false

       UIView.animate(withDuration: duration, animations: {

           view.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)

       }) { (continueSequence) in

           UIView.animate(withDuration: duration, animations: {

                view.transform = .identity

           }, completion: { (seqeunceFinished) in
               enable()
        })
      }
   }

然后您可以在 IBAction 中调用它,如下所示:

@IBAction func enlargeButton(_ sender: UIButton){

      sender.isUserInteractionEnabled = false

      enlarge(view: sender duration: 1) { sender.isUserInteractionEnabled = true }
}

【讨论】:

  • 这是正确的概念,但是,我希望动画是可中断的,这个 sn-p 动画更改,但它不能被中断,动画必须在任何更改发生之前完成。
  • 我已经更新了代码以在按钮被触摸时禁用它,然后在调用完成处理程序时重新启用它。
  • 我已经更新了我的帖子以阐明我想要完成的任务,动画应该能够在它的过程中间停止,如果用户在中间中断,如果需要,可以反转动画。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多