2016-07-17 5 views
2

スウィフトコードでは、UIViewを起動してアニメーション化すると、円のパスに沿ってモーションを動かし、正常に終了させることができますか?アニメーションCGAffineTransformを使用してこれを達成することは可能ですか?円環状のパスに沿ってUIViewをアニメーション化するにはどうすればよいですか?

私は達成しようとしているアニメーションの種類を示す以下のGIFを用意しました。

enter image description here

答えて

14

更新:以下のコードのためにスウィフト3とスイフト4との間に差がないスイフト4に更新コード。

UIBezierPathおよびCAKeyFrameAnimationを使用してください。ここで

はコードです:ここでは

//The code is within viewDidLoad method 
let circlePath = UIBezierPath(arcCenter: view.center, radius: 20, startAngle: 0, endAngle: .pi*2, clockwise: true) 

let animation = CAKeyframeAnimation(keyPath: "position") 
animation.duration = 1 
animation.repeatCount = MAXFLOAT 
animation.path = circlePath.cgPath 

let squareView = UIView() 
//whatever the value of origin for squareView will not affect the animation 
squareView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 
squareView.backgroundColor = .lightGray 
view.addSubview(squareView) 
// You can also pass any unique string value for key 
squareView.layer.add(animation, forKey: nil) 

// circleLayer is only used to locate the circle animation path 
let circleLayer = CAShapeLayer() 
circleLayer.path = circlePath.cgPath 
circleLayer.strokeColor = UIColor.black.cgColor 
circleLayer.fillColor = UIColor.clear.cgColor 
view.layer.addSublayer(circleLayer) 

は、キャプチャです:

enter image description here

また、ビューがそれの中央にアンカーアニメーションではない作るためにsquareView.layeranchorPointプロパティを設定することができます。デフォルト値anchorPointは(0.5、0.5)です。これは中心点を意味します。

+0

非常にクールです。どうもありがとう! – user4806509

関連する問題