gpt4 book ai didi

ios - Swift 动画问题

转载 作者:行者123 更新时间:2023-11-28 11:22:16 24 4
gpt4 key购买 nike

我正在创建一个非常基本的 Swift 函数,它将 UIView 设置为动画到屏幕中央,使其出现并放大一倍,然后在返回到其原始位置时最后缩小回其原始大小。这是代码:

func toTheCorner(view: UIView, delay: Double)
{
// record original position
let originalCenter = view.center
// record original bounds
let b = view.bounds

UIView.animateWithDuration(0.1, delay: delay, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: .CurveEaseOut, animations: {

// move view to center of screen
view.center.x = self.view.bounds.width/2
view.center.y = self.view.bounds.height/2


}, completion: nil)

UIView.animateWithDuration(0.9, delay: delay + 0.1, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: .CurveEaseOut, animations: {

// Fade image in
view.alpha = 1.0

// Increase size
view.bounds.size.height = b.height + b.height/2
view.bounds.size.width = b.width + b.width/2

}, completion: nil)

UIView.animateWithDuration(0.7, delay: delay + 1.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: .CurveEaseOut, animations: {

// Shrink to original size
view.bounds.size.height = b.height
view.bounds.size.width = b.width

// Move to original position
view.center = originalCenter


}, completion: nil)

}

产生问题的行是:

view.center = originalCenter

如果没有这一行,函数将按预期执行。 View 出现在屏幕中央,然后加倍然后缩小。但是,只要我输入“view.center = originalCenter”, View 就会出现在屏幕中心的原始点正对面而不是中间,然后移动到它的原始点。

这条线如何影响前两个动画?这看起来应该可行,而且非常简单......

谢谢!本

最佳答案

你应该为此使用完成 block ;

func toTheCorner(view: UIView, delay: Double)
{
// record original position
let originalCenter = view.center
// record original bounds
let b = view.bounds

UIView.animateWithDuration(0.1, delay: delay, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: .CurveEaseOut, animations: {

// move view to center of screen
view.center.x = self.view.bounds.width/2
view.center.y = self.view.bounds.height/2

}, completion: { finished in
UIView.animateWithDuration(0.9, delay: delay + 0.1, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: .CurveEaseOut, animations: {

// Fade image in
view.alpha = 1.0

// Increase size
view.bounds.size.height = b.height + b.height/2
view.bounds.size.width = b.width + b.width/2

}, completion: { finished in
UIView.animateWithDuration(0.7, delay: delay + 1.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: .CurveEaseOut, animations: {

// Shrink to original size
view.bounds.size.height = b.height
view.bounds.size.width = b.width

// Move to original position
view.center = originalCenter
}, completion: nil)})
})
}

关于ios - Swift 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26764354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com