【问题标题】:swift: fix buttons animationswift:修复按钮动画
【发布时间】:2018-07-20 05:32:33
【问题描述】:

当我的应用统计时,我想显示按钮动画。在情节提要中,我的按钮有height = 50y = -50。我在代码中有这个动作。

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() 
{
    super.viewDidLoad()
    buttonAnimation()
}

func buttonAnimation() 
{
    self.buttonTopConstraint.constant += self.ButtonHeightConstraint.constant
    UIView.animate(withDuration: 1.5, delay: 0.0, options: [], animations: 
        {
            self.view.layoutIfNeeded()
        }
    )
}

动画效果很好。但是当应用程序在storyboard 中启动我的背景imageView 并且不同的imageView 也动画了。但我只需要按钮动画。如何解决?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    不要在viewDidLoad 中调用动画,因为那时控制器的所有视图都将设置。

    使用viewDidAppear调用动画方法。

      override func viewDidAppear() 
       {
        buttonAnimation()
       }
    

    【讨论】:

      【解决方案2】:

      使用CGAffineTransform 为您的按钮设置动画。 Remove y=-50 来自故事板并将其保存在所需的位置。然后在viewWillAppearviewDidAppear中调用这个函数:

      func animateButton() {
          yourButton.transform = CGAffineTransform(translationX: yourButton.frame.origin.x, y: yourButton.frame.origin.y - 50)
          UIView.animate(withDuration: 1.5, delay: 0.0, options: [], animations: {
              self.yourButton.transform = .identity
          }, completion: nil)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多