加载内容时,Swift UIView.animateWithDuration消失

时间:2016-01-25 07:11:48

标签: ios swift swift2 uianimation

我使用一个简单的动画向下滑动一个小视图(此视图包含一些标签和imageView)。 动画有效,但当我尝试设置标签和imageView的内容时,视图会消失。

任何帮助?

UIView.animateWithDuration(1, delay: 0,
                usingSpringWithDamping: springDamping,
                initialSpringVelocity: velocity, options: option!, animations: {
                    self.sliderUserInfo.frame.origin = self.sliderUserInfoPosition
                }, completion: {finished  in
                    if finished {
                        self.loadContentView()
                    }
            })

2 个答案:

答案 0 :(得分:0)

将您的代码修改为:

    var newFrame = self.sliderUserInfo.frame
    newFrame.origin = self.sliderUserInfoPosition
    UIView.animateWithDuration(1, delay: 0,
        usingSpringWithDamping: springDamping,
        initialSpringVelocity: velocity, options: option!, animations: {
            self.sliderUserInfo.frame = newFrame
        }, completion: {finished  in
            if finished {
                self.loadContentView()
            }
    })

答案 1 :(得分:0)

Autolayout将视图放回原来的位置。在动画中,不要更改帧原点,而是更改视图和超视图顶部之间约束的约束常量。

创建一个:

var cHeight: NSLayoutConstraint!

// Start the bottom of the view off the screen
cHeight = NSLayoutConstraint(item: sliderUserInfo, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: -1.0)

或者从故事板中引用它:

@IBOutlet var cHeight: NSLayoutConstraint!

然后为更改设置动画:

// Animate the change of the constraint
self.cHeight.constant = self.sliderUserInfo.frame.height
// Note we animate the layoutIfneeded
UIView.animateWithDuration(1, delay: 0,
            usingSpringWithDamping: springDamping,
            initialSpringVelocity: velocity, options: option!, animations: {
                self.sliderUserInfo.layoutIfNeeded()
            }, completion: {finished  in
                if finished {
                    self.loadContentView()
                }
        })