流畅的动画问题

Smooth Animation Troubles

我有一个按钮,我正在尝试对其进行动画处理以表示某种下拉菜单效果。类似于这里看到的

https://medium.com/@phillfarrugia/building-a-tinder-esque-card-interface-5afa63c6d3db

我使用了 CGAffineTransform 属性 并将度数转换为弧度以正确旋转。但是,当我以另一种方式将其旋转回来时,问题就来了。它没有返回它来的方向,而是做了一些尴尬的翻转回到相同的位置。

任何人都可以帮助我重新创建在我提供的 link 中看到的更平滑的过渡

这是我当前的代码。

@objc func showCalendarPressed(){
    print("show calendar pressed")
    //will check if there has been any animation work done on the UIVIEW
    if self.showCalendar.transform == .identity {

        UIView.animate(withDuration: 1.5) {
            self.showCalendar.transform = CGAffineTransform(rotationAngle: self.radians(degrees: 180) )

        }

    }else {
        //there is a transform on it and we need to change it back
        UIView.animate(withDuration: 0.5, animations: {
            //will remove the transform and animate it back
            self.showCalendar.transform = .identity
        }) { (true) in

        }
    }
}

弧度函数

func radians(degrees: Double) -> CGFloat{
    return CGFloat(degrees * .pi / 180)
}

你刚好走了 1/2 路(可能稍微多一点,这取决于你对 radians 的转换),当你从 return 到 .identity 时,它会保持原样方向,因为它更近(旋转最少的方向)。最初尝试使用稍小的角度。我发现这个有用:

替换:

self.radians(degrees: 180)

与:

self.radians(degrees: 179.999)

通过稍微少于 1/2 圈(这将是难以察觉的),return转向 .identity 将旋转它来自的方向。