迅捷:沿椭圆贝塞尔曲线的路径顺时针旋转90度

时间:2018-10-09 16:04:36

标签: swift animation uibezierpath

我正在尝试将UIView沿椭圆贝塞尔曲线路径旋转90度。我可以使用滑动手势作为触发来沿贝塞尔曲线路径设置视图动画。问题是:

  1. 视图位于屏幕的顶部中央,但是动画从90度点(顺时针)开始,进行完整的360度,然后将视图返回到中央顶部。我希望它从视图的中心点开始。

  2. 我只希望视图在每个动画上绕圆环移动1/4。因此,如果它从0度开始并在第一次滑动时移动到90,则在第二次滑动时,它从90开始移动到180,依此类推。

到目前为止,这是我的代码:

C_SelectionWrapper-自定义UIView:

@IBDesignable class C_SelectionWrapper: UIView {

var myOvalPath:UIBezierPath?

override func draw(_ rect: CGRect) {

    myOvalPath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.width))

    UIColor.gray.setStroke()
    myOvalPath!.lineWidth = 5.0

    //add the dashes
    var dashes: [CGFloat] = [5, 25, 5, 25]
    myOvalPath!.setLineDash(&dashes, count: dashes.count, phase: 0)

    //voila!
    myOvalPath!.stroke()
}

}

在我的ViewController中:

@IBOutlet weak var vSelectionWrapper: C_SelectionWrapper!
...

override func viewDidLoad() {
    super.viewDidLoad()

...

//add a swipe gesture
    let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rotateButtons(_:)))
    downSwipe.direction = .down
    self.view.addGestureRecognizer(downSwipe)

}

@objc func rotateButtons(_ mySwipe:UISwipeGestureRecognizer) {

    if let thisPath = vSelectionWrapper.myOvalPath  {

        let kfAnimation = CAKeyframeAnimation(keyPath: "position")
        kfAnimation.path = thisPath.cgPath
        kfAnimation.rotationMode = CAAnimationRotationMode.rotateAuto
        kfAnimation.repeatCount = 0
        kfAnimation.duration = 2.5

        blueOval!.layer.add(kfAnimation, forKey: "animation")
    }

}

这是实际的情况:

https://youtu.be/9F44tShWJ88

任何帮助将不胜感激。

0 个答案:

没有答案