Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Last active July 1, 2019 19:10
Show Gist options
  • Save aybekckaya/c89c2f08be0c01309c0d478fb41186a4 to your computer and use it in GitHub Desktop.
Save aybekckaya/c89c2f08be0c01309c0d478fb41186a4 to your computer and use it in GitHub Desktop.
override func viewDidLoad() {
super.viewDidLoad()
let viewBox = UIView()
viewBox.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
viewBox.backgroundColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
self.view.addSubview(viewBox)
let path = createPath()
self.debugPath(path: path)
moveAlongPath(view: viewBox, path: path)
}
private func moveAlongPath(view:UIView , path:CGPath) {
let pathAnimation = CAKeyframeAnimation(keyPath: "position")
pathAnimation.path = path
pathAnimation.calculationMode = .paced
pathAnimation.fillMode = CAMediaTimingFillMode.forwards
pathAnimation.duration = 5.0
pathAnimation.isRemovedOnCompletion = false
view.layer.add(pathAnimation, forKey: nil)
}
private func createPath()->CGPath {
let bezierPath:UIBezierPath = UIBezierPath()
let midXPt:CGFloat = view.frame.size.width/2
let midYPt:CGFloat = view.frame.size.height/2
bezierPath.move(to: CGPoint(x: midXPt, y: midYPt*2))
bezierPath.addQuadCurve(to: CGPoint(x: midXPt , y: 0), controlPoint: CGPoint(x: midXPt + 100, y: midYPt))
return bezierPath.cgPath
}
private func debugPath(path:CGPath) {
let shapeLayer = CAShapeLayer()
shapeLayer.path = path
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.lineWidth = 1
self.view.layer.addSublayer(shapeLayer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment