IOS animation activity indicator

Source: Internet
Author: User

IOS animation activity indicator
1. Result Display

2. Implementation ideas

1. Create a copy Layer

    CAReplicatorLayer *replicator = [CAReplicatorLayer layer];    replicator.frame = CGRectMake(50, 50, 200, 200);    replicator.backgroundColor = [UIColor redColor].CGColor;    [self.view.layer addSublayer:replicator];

2. Create a rectangular layer and set the scaling animation.

    CALayer *indicator = [CALayer layer];    indicator.transform = CATransform3DMakeScale(0, 0, 0);    indicator.position = CGPointMake(100, 20);    indicator.bounds = CGRectMake(0, 0, 10, 10);    indicator.backgroundColor = [UIColor greenColor].CGColor;    [replicator addSublayer:indicator];    CGFloat durtion = 1;    CABasicAnimation *anim = [CABasicAnimation animation];    anim.keyPath = @"transform.scale";    anim.fromValue = @1;    anim.toValue = @0.1;    anim.repeatCount = MAXFLOAT;    anim.duration = durtion;    [indicator addAnimation:anim forKey:nil];

3. Copy the rectangular layer and set the angle deformation of each copy layer.

Int count = 10; // set the number of child layers replicator. instanceCount = count; // sets the child layer deformation angle CGFloat angle = M_PI * 2/count; replicator. instanceTransform = CATransform3DMakeRotation (angle, 0, 0, 1 );

4. Set the extension time of the copy animation. After the first animation is executed, the animation duration must be evenly distributed to each child layer)
Formula: Extend time = animation duration/total number of child Layers

If there are two layers, the animation time is 1 second, and the extension time is 0.5 seconds. When the first animation is half executed (0.5), the second animation starts to be executed. The second execution is complete.

// Set the child layer animation extension time replicator. instanceDelay = durtion/count;
3. complete code
# Import "ViewController. h "# define Count 20.0 # define DurationTime 1.0 @ interface ViewController () @ property (nonatomic, strong) CAReplicatorLayer * replicator; @ property (nonatomic, strong) CALayer * indicator; @ property (nonatomic, strong) CABasicAnimation * anim; @ end @ implementation ViewController // lazy loading, replication layer-(CAReplicatorLayer *) replicator {if (_ replicator = nil) {_ replicator = [CAReplicatorLayer]; _ replicator. frame = CGRectMake (50, 50,200,200); _ replicator. backgroundColor = [UIColor clearColor]. CGColor; // set the number of child layers _ replicator. instanceCount = Count; // sets the child animation extension time _ replicator. instanceDelay = DurationTime/Count; // sets the child layer deformation angle CGFloat angle = M_PI * 2/Count; _ replicator. instanceTransform = CATransform3DMakeRotation (angle, 0, 0, 1);} return _ replicator;} // common layer-(CALayer *) indicator {if (_ indicator = nil) {_ indicator = [CALayer layer]; _ indicator. transform = CATransform3DMakeScale (0, 0, 0); _ indicator. position = CGPointMake (100, 20); _ indicator. bounds = CGRectMake (0, 0, 10, 10); _ indicator. cornerRadius = 5; _ indicator. backgroundColor = [UIColor greenColor]. CGColor;} return _ indicator;} // animation-(CABasicAnimation *) anim {if (_ anim = nil) {_ anim = [CABasicAnimation animation]; _ anim. keyPath = @ "transform. scale "; _ anim. fromValue = @ 1; _ anim. toValue = @ 0.1; _ anim. repeatCount = MAXFLOAT; _ anim. duration = DurationTime;} return _ anim;}-(void) viewDidLoad {[super viewDidLoad]; // Add the replication layer [self. view. layer addSublayer: self. replicator]; // Add layer [self. replicator addSublayer: self. indicator]; // Add an animation [self. indicator addAnimation: self. anim forKey: nil] ;}@ end
 

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.