iOS - CABasicAnimation (basic animation)

Brief introduction

The main basis for the animation of CALayer provides an operating variable attributes for simple animation. For example: displacement, transparency, scaling, rotation, background color, and so on.
The basic idea is to use CABaseAnimation set to adjust the properties of the initial value, end value, interpolation mode, then the animation added to the appropriate layer up.
CABasicAnimation can set the start value keyPath, the end value of the animation will move along the set point. CABasicAnimation can be seen as only two key points of special CAKeyFrameAnimation.

Attributes

Attributes Explanation
fromValue corresponding to the initial value keyPath
toValue Corresponding to the end value keyPath
byValue The same amount of change of the initial value of the attribute change

keyPath property

Attributes Explanation
transform 3D transformation, [NSValue valueWithCATransform3D: CATransform3DMakeScale (0.5, 0.5, 0)], directly the 3D transformation
transform.scale Scaling, @ (1.5), 1.5 enlarge
transform.scale.x Scaling width, @ (1.5), Wide Zoom 1.5
transform.scale.y Height scaling, @ (1.5), a high amplification 1.5
transform.rotation.x Around the x axis, @ (M_PI), x-axis rotated by 180 degrees
transform.rotation.y About the y axis, @ (M_PI), y-axis is rotated 180 degrees
transform.rotation.z About the z axis, @ (M_PI), z-axis is rotated 180 degrees
position Position (center point changes), [NSValue valueWithCGPoint: CGPointMake (100, 100)], becomes a central point (100, 100)
opacity Transparency, @ (0.5), the transparency becomes 0.5
bounds Changes in the size, center point remains unchanged, [NSValue valueWithCGRect: CGRectMake (0, 0, 300, 300)], size becomes (300,300)
cornerRadius Rounded set @ (5), rounded to 5
backgroundColor Background color conversion, (id) [UIColor redColor] .CGColor, background changed to red contents, you can change the image layer display, (id) [UIImage imageNamed: @ "12.png"] CGImage, the UIView display picture to change. as 12.png
strokeStart Changes from the start point, fromValue = 0, toValue = 1, the properties of CAShapeLayer
strokeEnd Starts to change from the end position, fromValue = 1, toValue = 0.5, the attribute CAShapeLayer
path The path to change
rotaion.x Rotation, in radians, X-axis
rotaion.y Rotation, in radians, Y-axis
rotaion.z Rotation, in radians, Z axis
rotaion Rotation, in radians, Z-axis, exactly the same rotation.z
contents Content, such as pictures UIImageView imageAnima.toValue = (id) [UIImage imageNamed: @ "to"] CGImage;.
opacity Transparency as @ (0.7)
contentsRect.size.width @ Transverse stretching scaling (0.4) is preferably between 0 and 1
margin layout
zPosition Overturn
borderWidth Border width
frame Size Location
hidden Show hidden
mask Mask
masksToBounds Mask
shadowColor Shadow Color
shadowOffset Shadow Offset
shadowOpacity Opaque shadow
shadowRadius Shadow Radius

practice

Position Animation

    self.animationView = [[UIView alloc]init];
    self.animationView.frame = CGRectMake(0, kHeight/2-50, 50, 50);
    self.animationView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.animationView];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(16, 16)];
    animation.toValue = [NSValue valueWithCGPoint:self.view.center];
    animation.duration = 1.0;
    animation.fillMode = kCAFillModeBoth;
    animation.removedOnCompletion = NO;//动画结束后不回到原始状态
    animation.autoreverses = YES;//允许反向动画
    animation.repeatCount = HUGE_VALF;//无限循环
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [self.animationView.layer addAnimation:animation forKey:@"positonAnimation"];

The background color animation

    self.animationView = [[UIView alloc]init];
    self.animationView.frame = CGRectMake(0, kHeight/2-50, 50, 50);
    self.animationView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.animationView];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    animation.fromValue = (id)[UIColor cyanColor].CGColor;
    animation.toValue = (id)[UIColor magentaColor].CGColor;
    animation.duration = 1.0;
    animation.fillMode = kCAFillModeBoth;
    animation.removedOnCompletion = NO;//动画结束后不回到原始状态
    animation.autoreverses = YES;//允许反向动画
    animation.repeatCount = HUGE_VALF;//无限循环
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [self.animationView.layer addAnimation:animation forKey:@"backgroundColorAnimation"];
发布了38 篇原创文章 · 获赞 5 · 访问量 9047

Guess you like

Origin blog.csdn.net/zj382561388/article/details/103794711