2
スウィフトコードでは、UIView
を起動してアニメーション化すると、円のパスに沿ってモーションを動かし、正常に終了させることができますか?アニメーションCGAffineTransform
を使用してこれを達成することは可能ですか?円環状のパスに沿ってUIViewをアニメーション化するにはどうすればよいですか?
私は達成しようとしているアニメーションの種類を示す以下のGIFを用意しました。
スウィフトコードでは、UIView
を起動してアニメーション化すると、円のパスに沿ってモーションを動かし、正常に終了させることができますか?アニメーションCGAffineTransform
を使用してこれを達成することは可能ですか?円環状のパスに沿ってUIViewをアニメーション化するにはどうすればよいですか?
私は達成しようとしているアニメーションの種類を示す以下のGIFを用意しました。
更新:以下のコードのためにスウィフト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)
は、キャプチャです:
また、ビューがそれの中央にアンカーアニメーションではない作るためにsquareView.layer
のanchorPoint
プロパティを設定することができます。デフォルト値anchorPoint
は(0.5、0.5)です。これは中心点を意味します。
非常にクールです。どうもありがとう! – user4806509