IOS-CALayer & amp; CAAnimation, ioscalayer plotting

Source: Internet
Author: User

IOS-CALayer & CAAnimation, ioscalayer plotting

1. CALayer

1. CALayer

CALayer belongs to the QuartzCore. framework. We do not need to manually import this library from Xcode5.

CALayer is a layer. When the UIView we draw can be displayed on the screen, the essence is this layer.

The following code describes the basic usage of CALayer.

    CALayer *caLayer = [CALayer layer];    caLayer.backgroundColor = [UIColor cyanColor].CGColor;    caLayer.frame = CGRectMake(10, 20, 100, 100);    caLayer.cornerRadius = 20;
caLayer.masksToBounds = YES; [self.view.layer addSublayer:caLayer];

When we execute the above Code, a layer will be added to the view. Shows the effect.

The size of the cornerRadius value determines the layer shape. The length of the cornerRadius is used as a semi-circle in the four corners. You can also set the border, border color, and other information.

    CALayer *caLayer1 = [CALayer layer];    caLayer1.frame = CGRectMake(10, 20, 200, 200);    caLayer1.contents = (id)[UIImage imageNamed:@"health.jpg"].CGImage;    caLayer1.cornerRadius = 100;    caLayer1.masksToBounds = YES;    caLayer1.borderWidth = 10;    caLayer1.borderColor = [UIColor greenColor].CGColor;    [self.view.layer addSublayer:caLayer1];

The effect is as follows: CALayer also has an important attribute position (0.5, 0.5 by default), and caLayer. anchorPoint (0-1)

We can understand:

CALayer * caLayer = [CALayer layer]; caLayer. backgroundColor = [UIColor cyanColor]. CGColor; caLayer. cornerRadius = 20; caLayer. bounds = CGRectMake (200, 20,200,200); caLayer. position = CGPointMake (100,100); caLayer. anchorPoint = CGPointMake (0.5, 0); caLayer. masksToBounds = YES; [self. view. layer addSublayer: caLayer];

Effect:

2. CATextLayer

CATextLayer is a subclass of CALayer. We can write text on it and set the font and other information.

    CATextLayer *caTextlayer = [CATextLayer layer];    caTextlayer.frame = CGRectMake(10, 20, 300, 100);    caTextlayer.string = @"Roy says hello";    caTextlayer.foregroundColor = [UIColor orangeColor].CGColor;    [self.view.layer addSublayer:caTextlayer];

3. CAGradientLayer

This class also inherits CALayer. You can achieve color gradient.

    CAGradientLayer *dLayer = [CAGradientLayer layer];    dLayer.colors = @[(id)[UIColor yellowColor].CGColor,(id)[UIColor grayColor].CGColor,(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor];    dLayer.startPoint = CGPointMake(0, 0);    dLayer.endPoint = CGPointMake(1, 1);    dLayer.locations = @[@0.0,@0.2,@0.5,@01];//0-1    dLayer.frame = CGRectMake(10, 20, 320, 100);    [self.view.layer addSublayer:dLayer];

Ii. CAAnimation

For CAAnimation, you can understand the following figure and the following code. # Import "ViewController. h "@ interface ViewController () {CALayer * layer;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. layer = [CALayer layer]; layer. frame = CGRectMake (10, 20, 60, 60); layer. backgroundColor = [UIColor grayColor]. CGColor; [self. view. layer addSublayer: layer]; UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; button. frame = CGRectMake (10, 20,100, 30); button. backgroundColor = [UIColor purpleColor]; [button addTarget: self action: @ selector (btnClick) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button]; // [self basicAnimation]; // [self keyframeAnimation]; // [self groupAnimation];}-(void) btnClick {// transition animation, you can only execute [self transitionAnimation];} // basic animation in the click event to inherit the property animation-(void) basicAnimation {/* // CABasicAnimation * animation = [CABasicAnimation animation]; // The key-path describing the property to be animated animation. keyPath = @ "backgroundColor"; // animation cycle animation. duration = 2; // The attribute from which the animation starts. fromValue = (id) [UIColor grayColor]. CGColor; // to which attribute ends the animation. toValue = (id) [UIColor greenColor]. CGColor; [layer addAnimation: animation forKey: nil]; * // move CABasicAnimation * animation = [CABasicAnimation animation]; animation. keyPath = @ "position"; animation. fromValue = [NSValue valueWithCGPoint: CGPointMake (10, 20)]; animation. toValue = [NSValue valueWithCGPoint: CGPointMake (100,200)]; animation. duration = 3; [layer addAnimation: animation forKey: nil];} // Frame animation, inheriting attribute animation-(void) keyframeAnimation {/* CAKeyframeAnimation * animation = [CAKeyframeAnimation]; // animation attributes. keyPath = @ "backgroundColor"; // animation transition value animation. values = @ [(id) [UIColor redColor]. CGColor, (id) [UIColor greenColor]. CGColor, (id) [UIColor purpleColor]. CGColor]; // animation transition time animation. keyTimes = @ [@ 0.0, @ 0.5, @ 1]; animation. duration = 2; [layer addAnimation: animation forKey: nil]; */CAKeyframeAnimation * animation = [CAKeyframeAnimation animation]; animation. keyPath = @ "position"; animation. values = @ [[NSValue usage: CGPointMake (10, 20)], [NSValue valueWithCGPoint: CGPointMake (10,300)], [NSValue valueWithCGPoint: CGPointMake (200,300)], [NSValue usage: CGPointMake (10,300)], [NSValue valueWithCGPoint: CGPointMake (10, 20)], [NSValue valueWithCGPoint: CGPointMake (50, 50)]; animation. autoreverses = YES; animation. duration = 2; [layer addAnimation: animation forKey: nil];} // Group animation, composite animation, multiple animations are executed simultaneously-(void) groupAnimation {// move CABasicAnimation * basic = [CABasicAnimation animation]; basic. keyPath = @ "position"; basic. duration = 2; basic. autoreverses = YES; basic. fromValue = [NSValue valueWithCGPoint: layer. position]; basic. byValue = [NSValue valueWithCGPoint: CGPointMake (20, 0)]; // color change CAKeyframeAnimation * keyframe = [CAKeyframeAnimation animation]; keyframe. keyPath = @ "backgroundColor"; keyframe. values = @ [(id) [UIColor redColor]. CGColor, (id) [UIColor yellowColor]. CGColor, (id) [UIColor greenColor]. CGColor]; keyframe. duration = 2; keyframe. autoreverses = YES; CAAnimationGroup * group = [CAAnimationGroup animation]; group. animations = @ [basic, keyframe]; // The time here is a group based on the group time. duration = 4; [layer addAnimation: group forKey: nil];} // transition animation-(void) transitionAnimation {CATransition * animation = [CATransition animation]; animation. type = @ "pageUnCurl"; animation. delegate = self; animation. duration = 2; animation. autoreverses = YES; [layer addAnimation: animation forKey: nil];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ endAnimation

For the over-animation type, we can use the following effects.

Cube


 SuckEffect triangle

RippleEffect

PageCurl pagination

PageUnCurl

CameraIrisHollowOpen lens shutter opening

CameraIrisHollowClose lens shutter open

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.