Iphone 如何在iOS中沿曲线路径移动视图
如何在曲线路径中设置/移动视图的动画,是否可以使用UIAnimationIphone 如何在iOS中沿曲线路径移动视图,iphone,objective-c,ios,core-animation,uianimation,Iphone,Objective C,Ios,Core Animation,Uianimation,如何在曲线路径中设置/移动视图的动画,是否可以使用UIAnimation 就像在图像中移动路径中的视图一样。可以使用核心动画和CAKeyFrameAnimation来定义曲线点。请参阅本教程:您可以通过将动画嵌套在completion子句中来堆叠动画。上述方法可以通过以下方式实现:- i) ii)iii)制作动画 自定义视图的图层 该示例指的是设置层动画,而不是整个视图。我有一个例子,我需要沿着一条路径移动整个UIView。我可以很容易地使用animateWithDuration从这里到那里,
就像在图像中移动路径中的视图一样。可以使用核心动画和CAKeyFrameAnimation来定义曲线点。请参阅本教程:您可以通过将动画嵌套在completion子句中来堆叠动画。上述方法可以通过以下方式实现:- i) ii)iii)制作动画 自定义视图的图层
该示例指的是设置层动画,而不是整个视图。我有一个例子,我需要沿着一条路径移动整个UIView。我可以很容易地使用animateWithDuration从这里到那里,但是我不知道如何使用它来使用给定的路径从这里到那里。当我尝试将一组animateWithDuration调用串在一起时,最后一个调用总是取消前面的调用。
import UIKit
import CoreGraphics
class ViewController: UIViewController {
var moveAlongPath:CAAnimation!
override func viewDidLoad() {
super.viewDidLoad()
addAnimation()
initiateAnimation()
}
func curevedPath() -> UIBezierPath {
let path = createCurvePath()
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 1.0
self.view.layer.addSublayer(shapeLayer)
return path
}
func addAnimation() {
let moveAlongPath = CAKeyframeAnimation(keyPath: "position")
moveAlongPath.path = curevedPath().cgPath
moveAlongPath.duration = 5
moveAlongPath.repeatCount = HUGE
moveAlongPath.calculationMode = kCAAnimationPaced
moveAlongPath.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
self.moveAlongPath = moveAlongPath
}
func initiateAnimation() {
let layer = createLayer()
layer.add(moveAlongPath, forKey: "animate along Path")
}
//MARK:- Custom View Path
func createLayer() -> CALayer {
let customView = CustomView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(customView)
let customlayer = customView.layer
customlayer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
customlayer.position = CGPoint(x: 25, y: 25)
return customlayer
}
//MARK:- Custom Curve Path
func createCurvePath() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 10, y: 200))
path.addQuadCurve(to: CGPoint(x: 300, y: 200), controlPoint: CGPoint(x: 150, y: 10) )
return path
}
}
class CustomView:UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setUpView()
}
func setUpView() {
let image = UIImage(named: "Go.png")
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
addSubview(imageView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}