如何仅使用动画转换视图的按钮边缘

时间:2017-02-01 13:02:33

标签: swift animation

我试图获得一种效果,当我在视图上向上滑动时,按钮边缘将向顶部移动,直到它消失(视图高度将变得越来越小)。 我希望顶边保持在原位,只有臀部边缘移动。我该如何调整这段代码来实现呢?

     UIView.animate(withDuration: 5.0, animations: { () in
     self.topView.translatesAutoresizingMaskIntoConstraints = false
     self.topView.transform = CGAffineTransform(scaleX: 1,y: 5)
     })
    self.topView.transform = CGAffineTransform.identity

1 个答案:

答案 0 :(得分:1)

首先你需要方法的顺序调用你的代码(不是绝对必要,但看起来更好):

self.topView.transform = CGAffineTransform.identity
UIView.animate(withDuration: 5.0, animations: { () in
    self.topView.translatesAutoresizingMaskIntoConstraints = false
    self.topView.transform = CGAffineTransform(scaleX: 1,y: 5)
})

此缩放变换会将视图缩小到中心;如果你想保持最高位置,你可以添加一个translateBy(x:y :)转换并将其添加到比例变换中,或多或少像这样:

UIView.animate(withDuration: 5.0, animations: { () in
    self.topView.translatesAutoresizingMaskIntoConstraints = false
    let scaleTransform = CGAffineTransform(scaleX: 1,y: 5)
    let translateTransform = CGAffineTransform(translationX: 0,y: -100)
    let combined = scaleTransform.concatenating(translateTransform)
    self.topView.transform = combined 
})

或者您可以为视图的框架设置动画,如下所述: Core Animation for UIView.frame