2016-10-08 4 views
0

こんにちは、私は三角形の形状のCAShapeLayerを回転しようとしています。 northPole CAShapeLayerは、カスタムUIViewクラスで定義されています。クラスプロパティにはレイヤープロパティを設定する関数と回転をアニメーション化する関数があります。回転アニメーションは正常に機能しますが、回転が完了した後、元の位置に戻ります。回転アニメーションの後、指定した角度(positionTo = 90.0)でレイヤーを保持します。CATransform3DMakeRotationアンカーポイントとスウィフトの位置

アニメーションの後に正しい位置を保持するために、変換後の位置を設定しようとしています。私もpresentation()メソッドからその位置を取得しようとしましたが、これは役に立たないです。私はフレーム、境界、アンカーポイントについて多くの記事を読んだことがありますが、なぜこの変換の回転がうまくいかないのかは分かりません。

private func setupNorthPole(shapeLayer: CAShapeLayer) { 
    shapeLayer.frame = CGRect(x: self.bounds.width/2 - triangleWidth/2, y: self.bounds.height/2 - triangleHeight, width: triangleWidth, height: triangleHeight)//self.bounds 
    let polePath = UIBezierPath() 
    polePath.move(to: CGPoint(x: 0, y: triangleHeight)) 
    polePath.addLine(to: CGPoint(x: triangleWidth/2, y: 0)) 
    polePath.addLine(to: CGPoint(x: triangleWidth, y: triangleHeight)) 
    shapeLayer.path = polePath.cgPath 
    shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 1.0) 

アニメーション機能:

private func animate() { 
    let positionTo = CGFloat(DegreesToRadians(value: degrees)) 
    let rotate = CABasicAnimation(keyPath: "transform.rotation.z") 
    rotate.fromValue = 0.0 
    rotate.toValue = positionTo //CGFloat(M_PI * 2.0) 
    rotate.duration = 0.5 
    northPole.add(rotate, forKey: nil) 
    let present = northPole.presentation()! 
    print("presentation position x " + "\(present.position.x)") 
    print("presentation position y " + "\(present.position.y)") 


    CATransaction.begin() 
    northPole.transform = CATransform3DMakeRotation(positionTo, 0.0, 0.0, 1.0) 
    northPole.position = CGPoint(x: self.bounds.width/2, y: self.bounds.height/2) 
    CATransaction.commit() 
} 
+0

さて、私について申し訳ありません早めに悪い回答。私はCABasicAnimationを使ってローテーションをする方法を学ぶために 'present'を落とし、' begin'を落とし、 'commit'を落とし、私の本を読んでほしいと勧めています:http://www.apeth.com /iOSBook/ch17.html#_using_a_cabasicanimation – matt

答えて

0

私は次のようにlayoutSubviewsを()をオーバーライドしなければならなかったカスタムUIViewクラスで、問題を解決するには、次の

super.layoutSubviews() 
    let northPoleTransform: CATransform3D = northPole.transform 
    northPole.transform = CATransform3DIdentity 
    setupNorthPole(northPole) 
    northPole.transform = northPoleTransform 
関連する問題