在Swift中寻找像Uber这样的驱动程序动画

时间:2018-10-19 12:22:11

标签: ios swift uiview uianimation

我希望制作一个动画,例如当圆圈增加和减小时,应用程序开始寻找驱动程序时。 enter image description here

这是我的代码,我在自动反向动画中增加和减小圆的宽度和高度,但是它有2个问题:

  1. 它不能像动画从中心发生那样工作。它的起点和终点是一个角。

  2. 圆的半径不一致。关闭时变成正方形

代码:

let duration = 2.0


UIView.animate(withDuration: duration, delay: 0, options: [.repeat, .autoreverse] , animations: {
        let cntr = self.circularView.center
        self.circularView.layer.cornerRadius = min(self.circularView.frame.size.height,self.circularView.frame.size.width)/2
        self.circularView.center = cntr
        self.circularView.frame.size.width = (self.circularView.frame.size.width) * 2
        self.circularView.frame.size.height = (self.circularView.frame.height) * 2

1 个答案:

答案 0 :(得分:0)

尝试一下:将角半径保留在动画之外。使用变换动画,而不是更改宽度或约束。在情节提要/代码中,设置带有中心相关约束的圆形视图的位置:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.circularView!.layer.cornerRadius = self.circularView!.frame.width / 2

    UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse] , animations: {
        self.isZooming = !self.isZooming
        var zoomTransform = CGAffineTransform.identity
        let scale: CGFloat = self.isZooming ? 0.5 : 1.5
        zoomTransform = zoomTransform.scaledBy(x: scale, y: scale)
        self.circularView?.transform = zoomTransform
    })
}

enter image description here enter image description here