caanimationgroup与CATransaction的区别

动画的组合;

caanimationgroup:同一个layer;

CATransaction:不同layer;

In Core Animation, transactions are a way to group multiple animation-related changes together. Transactions ensure that the desired animation changes are committed to Core Animation at the same time:

CATransaction.begin()

backingLayer1.opacity = 1.0
backingLayer2.position = CGPoint(x: 50.0, y: 50.0)
backingLayer3.backgroundColor = UIColor.red.cgColor

CATransaction.commit()

UIView itself has a handful of functions involved with enabling and disabling animations, such as setAnimationsEnabled(_:) and performWithoutAnimation(_:). However, to ensure that both UIView-style and CALayer-style animations are suppressed, you can always just use CATransaction.

https://www.calayer.com

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];

    rotationAnimation.duration = 1;

    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    

    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];

    scaleAnimation.duration = 1;

    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

    animationGroup.duration = 1;

    animationGroup.autoreverses = YES;

    animationGroup.repeatCount = 1;

    animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];

    [view.layer addAnimation:animationGroup forKey:@"animationGroup"];

CATransaction.begin()

    CATransaction.setAnimationDuration(1.0)

    CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut))

    // Layer animation

    let myAnimation = CABasicAnimation(keyPath: "frame");

    myAnimation.toValue = NSValue(cgRect: myNewFrame)

    myAnimation.fromValue = NSValue(cgRect: myLayer.frame)

    myLayer.frame = myNewFrame

    myLayer.add(myAnimation, forKey: "someKeyForMyAnimation")

    // Outer animation

    let outerAnimation = CABasicAnimation(keyPath: "frame")

    outerAnimation.toValue = NSValue(cgRect: myNewOuterFrame)

    outerAnimation.fromValue = NSValue(cgRect: outerView.frame)

    outerView.layer.frame = myNewOuterFrame

    outerView.layer.add(outerAnimation, forKey: "someKeyForMyOuterAnimation")

    CATransaction.commit()

//保证 insert row 不闪屏

        UIView.setAnimationsEnabled(false)

        CATransaction.begin()

        CATransaction.setDisableActions(true)

        self.beginUpdates()

        self.insertRows(at: rows, with: .none)

        self.endUpdates()

        self.scrollToRow(at: rows[0], at: .bottom, animated: false)

        CATransaction.commit()

        UIView.setAnimationsEnabled(true)

原文地址:https://www.cnblogs.com/feng9exe/p/10362366.html