用手指跟随路径移动对象[Swift]

时间:2016-06-24 22:24:11

标签: swift uibezierpath cgpath cakeyframeanimation

我的视图中有一个圆圈,有一个可以围绕那个大圆圈移动的小椭圆,移动小椭圆的想法是改变时钟的时间。我试图移动椭圆形但只是沿着路径移动,所以它不会离开圆圈。好像它是一条铁路,火车只跟随铁路,但当时跟随手指的位置。 在这里,我附上了图片Image

我尝试制作动画,然后使用该动画然后跟随手指。我真的不知道该怎么做。有什么帮助吗?

这是我的动画代码(不起作用):

let customPath = UIBezierPath()
    customPath.move(to: CGPoint.init(x: 160, y: 165))
    customPath.addLine(to: CGPoint.init(x: 260, y: 245))
    customPath.addLine(to: CGPoint.init(x: 165, y: 365))
    customPath.addLine(to: CGPoint.init(x: 60, y: 260))

    let movingImage = UIImage(named: "MoveOval")
    let movingLayer = CALayer()
    movingLayer.anchorPoint = CGPoint.zero
    movingLayer.contents = moveOval
    movingLayer.frame = CGRect.init(x: 0.0, y: 0.0, width: (movingImage?.size.width)!, height: (movingImage?.size.height)!)
    self.view.layer.addSublayer(movingLayer)

    let pathAnimation = CAKeyframeAnimation(keyPath: "anim")
    pathAnimation.duration = 4.0
    pathAnimation.path = customPath.cgPath
    pathAnimation.calculationMode = kCAAnimationLinear
    movingLayer.add(pathAnimation, forKey: "anim")

0 个答案:

没有答案