質問

iOSアプリで CALayer をドラッグしようとしています。

positionプロパティを変更するとすぐに、新しい位置へのアニメーション化が試行され、場所全体で点滅します。

 layer.position = CGPointMake(x, y)

CALayers を即座に移動するにはどうすればよいですか? Core Animation APIについて頭を悩ますようには思えません。

役に立ちましたか?

解決

次のように通話をラップします。

[CATransaction begin]; 
[CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
layer.position = CGPointMake(x, y);
[CATransaction commit];

他のヒント

Swift 3拡張機能:

extension CALayer {
    class func performWithoutAnimation(_ actionsWithoutAnimation: () -> Void){
        CATransaction.begin()
        CATransaction.setValue(true, forKey: kCATransactionDisableActions)
        actionsWithoutAnimation()
        CATransaction.commit()
    }
}

使用法:

CALayer.performWithoutAnimation(){
    someLayer.position = newPosition
}

便利な機能も使用できます

[CATransaction setDisableActions:YES] 

同様。

注:Yogev Shellyのコメントを読んで、発生する可能性のある問題を理解してください。

他の人が示唆したように、 CATransaction を使用できます。
CALayerのデフォルトの暗黙的なアニメーション期間は0.25秒であるため、問題が発生します。

したがって、 setDisableActions の(私の意見では)より簡単な代替手段は、値 0.0 setAnimationDuration を使用することです。

[CATransaction begin];
[CATransaction setAnimationDuration:0.0];
layer.position = CGPointMake(x, y);
[CATransaction commit];

アニメーションの継続時間を明確にするために、Swift 4の以前の回答をここに組み合わせます...

extension CALayer
{
    class func perform(withDuration duration: Double, actions: () -> Void) {
        CATransaction.begin()
        CATransaction.setAnimationDuration(duration)
        actions()
        CATransaction.commit()
    }
}

使用法...

CALayer.perform(withDuration: 0.0) {
            aLayer.frame = aFrame
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top