IOS uses Calayer to achieve the effect of animation loading _ios

Source: Internet
Author: User

First look at the effect chart

The implementation process is as follows

The controller calls on a code line:

[Self showLoadingInView:self.view];

To facilitate the controller so called, you need to add a classification for the controller

. h Files

#import <UIKit/UIKit.h>
#import "GQCircleLoadView.h"
@interface Uiviewcontroller (gqcircleload)
//Display animation
-(void) Showloadinginview: (uiview*) view;
Hide Animation
-(void) hideload;
@property (Nonatomic,strong) Gqcircleloadview *loadingview;
@end

. m file

#import "Uiviewcontroller+gqcircleload.h"
#import <objc/runtime.h>
@implementation Uiviewcontroller (gqcircleload)
-(gqcircleloadview*) Loadingview
{return
 objc_getassociatedobject (self, @ "Loadingview");
}
-(void) Setloadingview: (gqcircleloadview*) loadingview
{
 objc_setassociatedobject (self, @ "Loadingview", Loadingview, objc_association_retain_nonatomic);
-(void) Showloadinginview: (uiview*) view{
 if (Self.loadingview = = nil) {
  Self.loadingview = [ Gqcircleloadview alloc]init];
 }
 if (view) {
  [view AddSubview:self.loadingView];
  Self.loadingView.frame = view.bounds;
 } else{
  UIWindow *appkeywindow = [UIApplication Sharedapplication].keywindow;
  [Appkeywindow AddSubview:self.loadingView];
  Self.loadingView.frame = appkeywindow.bounds;
 }
-(void) hideload{
 [Self.loadingview Removefromsuperview];
}
@end

The next step is GQCircleLoadView UIView to inherit, inside by drawRect drawing a circle, and the animation of the realization

#import "GQCircleLoadView.h" #define WINDOW_WIDTH [[UIScreen mainscreen] bounds].size.width #define Window_height [
UIScreen Mainscreen] bounds].size.height static Nsinteger circlecount = 3;
static CGFloat Cornerradius = 10;
static CGFloat magin = 15;
@interface Gqcircleloadview () <CAAnimationDelegate> @property (nonatomic, strong) Nsmutablearray *layerarr; @end @implementation Gqcircleloadview-(Instancetype) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:fram
 E]) {self.backgroundcolor = [uicolor Clearcolor];
return self; //Draw Circle-(void) drawcircles{for (Nsinteger i = 0; i < Circlecount; ++i) {CGFloat x = (Window_width-Cornerradius
  *2) * Circlecount-magin * (circleCount-1))/2.0 + I * (cornerradius*2 + magin) + Cornerradius;
  CGRect rect = CGRectMake (-cornerradius,-cornerradius, 2*cornerradius, 2*cornerradius);
  Uibezierpath *beizpath=[uibezierpath Bezierpathwithroundedrect:rect Cornerradius:cornerradius]; Cashapelayer *layer=[cashapelayeR Layer];
  Layer.path=beizpath.cgpath; Layer.fillcolor=[uicolor Graycolor].
  Cgcolor;
  Layer.position = Cgpointmake (x, Self.frame.size.height * 0.5);

  [Self.layer Addsublayer:layer];
 [Self.layerarr Addobject:layer];

 } [self drawanimation:self.layerarr[0]]; Rotate (open to trial effect)//cabasicanimation* rotationanimation = [Cabasicanimation animationwithkeypath:@ transform.rotation.y
"];
Rotationanimation.tovalue = [NSNumber numberwithfloat:-M_PI * 2.0];
Rotationanimation.duration = 1;
Rotationanimation.cumulative = YES;
Rotationanimation.repeatcount = maxfloat;
[Self.layer addanimation:rotationanimation forkey:@ "Rotationanimation"]; }//Animation implementation-(void) Drawanimation: (calayer*) layer {cabasicanimation *scaleup = [cabasicanimation animationwithkeypath:@ "t
 Ransform.scale "];
 Scaleup.fromvalue = @1;
 Scaleup.tovalue = @1.5;
 Scaleup.duration = 0.25;

 Scaleup.timingfunction = [Camediatimingfunction functionwithname:kcamediatimingfunctioneaseout]; Cabasicanimation *scaledown = [cabasicanimation animationwithkeypath:@ "Transform.scale"];
 Scaledown.begintime = scaleup.duration;
 Scaledown.fromvalue = @1.5;
 Scaledown.tovalue = @1;
 Scaledown.duration = 0.25;

 Scaledown.timingfunction = [Camediatimingfunction functionwithname:kcamediatimingfunctioneaseout];
 Caanimationgroup *group = [[Caanimationgroup alloc] init];
 Group.animations = @[scaleup, Scaledown];
 Group.repeatcount = 0;
 Group.duration = scaleup.duration + scaledown.duration;
 Group.delegate = self;

[Layer Addanimation:group forkey:@ "groupanimation"]; } #pragma mark-caanimationdelegate-(void) Animationdidstart: (caanimation *) Anim {if ([Anim Iskindofclass:caanimationg

  Roup.class]) {Caanimationgroup *animation = (Caanimationgroup *) Anim; [Self.layerarr enumerateobjectsusingblock:^ (Cashapelayer *obj, Nsuinteger idx, BOOL * _nonnull stop)
   {Caanimationgroup *a0 = (Caanimationgroup *) [obj animationforkey:@ "groupanimation"]; If (a0 && a0 = = animation) {Cashapelayer *neXtlayer = self.layerarr[(idx+1) >=self.layerarr.count?0: (idx+1)];
    [Self performselector: @selector (drawanimation:) Withobject:nextlayer afterdelay:0.25];
   *stop = YES;
 }
  }];
 }-(void) DrawRect: (CGRect) rect{[Super Drawrect:rect];
[Self drawcircles];
 }-(Nsmutablearray *) layerarr{if (_layerarr = = nil) {_layerarr = [[Nsmutablearray alloc] init];
return _layerarr; } @end

Demo will not upload, a total of four file code has been all posted!

Summarize

This is the whole story of this article, interested can try to open the above rotation animation code, turn off the rotation code, further modification can also achieve the QQ mailbox Drop-down Refresh effect, I hope this article content for everyone's study or work can bring certain help, if there are questions you can message exchange.

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.