【问题标题】:Swift Animation Not WorkingSwift 动画不工作
【发布时间】:2016-03-19 02:53:47
【问题描述】:

一旦发生某些事情,我试图将 UILabel 和 UIButton 移动到我的应用程序的屏幕上,但是,当我使用动画来执行此操作时,它不起作用,并且当动画应该发生时,组件不要移动到屏幕上。这是动画的代码:

UIView.animateWithDuration(2) { () -> Void in
                    self.label.center = CGPointMake(self.view.center.x, self.label.center.y)
                    self.again.center = CGPointMake(self.view.center.x, self.again.center.y)
                }

任何有关此问题的帮助将不胜感激!

【问题讨论】:

  • 我们需要更多的上下文——你在哪里称呼它?
  • 我同意@TroyT。我们还需要更多的代码。您是在故事板中创建对象吗?
  • 不,我在 main.storyboard 中使用图形界面创建了它们,而不是以编程方式。在 viewDidLayoutSubviews 中,我让组件离开屏幕,然后每当执行某个事件时,我知道何时执行,组件应该被动画化回到屏幕上。
  • 我可以发布我的整个故事板代码

标签: ios swift animation uibutton uilabel


【解决方案1】:

你应该试试这个。使用 layoutIfNeeded() 将重新定位所有内容。

UIView.animateWithDuration(2) { () -> Void in

                 self.label.center = CGPointMake(self.view.center.x, self.label.center.y)
                 self.again.center = CGPointMake(self.view.center.x, self.again.center.y)
                 self.view.layoutIfNeeded()
            }

【讨论】:

  • 对不起,这不起作用,我实际上在这个动画期间打印了一些东西到控制台,它打印了,但是组件没有移动到 iPhone 屏幕上
  • 代替 self.view.layoutIfNeeded() 尝试 self.label.layoutIfNeeded() 和 self.again.layoutIfNeeded()
【解决方案2】:

试试这个!

UIView.animateWithDuration(1.0,
delay: 2.0,
options: .CurveEaseInOut ,
animations: {
self.label.center = CGPointMake(self.view.center.x, self.label.center.y)
self.again.center = CGPointMake(self.view.center.x, self.again.center.y)    },
completion: { finished in
  println("Bug moved left!")
  self.faceBugRight()
})
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 2013-10-02
    • 2018-10-03
    • 2013-05-09
    相关资源
    最近更新 更多