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

euoag5mw  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(191)

我使用了一个简单的动画,它向下滑动一个小视图(该视图包含一些标签和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()
                    }
            })
mfpqipee

mfpqipee1#

将代码修改为:

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()
            }
    })
v7pvogib

v7pvogib2#

自动布局是将视图放回其所属的位置。在动画中,不要更改帧原点,而是更改视图和超级视图顶部之间的约束的约束常量。
创建一个:

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()
                }
        })

相关问题