起首來看看後果圖
完成進程以下
掌握器挪用就一句代碼:
[self showLoadingInView:self.view];
便利掌握器如斯挪用,就要為掌握器添加一個分類
.h文件
#import <UIKit/UIKit.h> #import "GQCircleLoadView.h" @interface UIViewController (GQCircleLoad) //顯示動畫 - (void)showLoadingInView:(UIView*)view; //隱蔽動畫 - (void)hideLoad; @property (nonatomic,strong) GQCircleLoadView *loadingView; @end
.m文件
#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
接上去就是GQCircleLoadView
繼續UIView
,外面經由過程drawRect
畫出圓圈,而且動畫的完成
#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:frame]) { self.backgroundColor = [UIColor clearColor]; } return self; } // 畫圓 - (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]]; // 扭轉(可翻開嘗嘗後果) // 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"]; } // 動畫完成 - (void)drawAnimation:(CALayer*)layer { CABasicAnimation *scaleUp = [CABasicAnimation animationWithKeyPath:@"transform.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:CAAnimationGroup.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就不上傳了,總共四個文件代碼曾經全貼上了!
總結
以上就是這篇文章的全體內容了,有興致可以嘗嘗翻開下面的扭轉的動畫代碼,封閉扭轉代碼,進一步修正也可完成出QQ郵箱的下拉刷新後果,願望這篇文章的內容對年夜家的進修或許任務能帶來必定的贊助,假如有疑問年夜家可以留言交換。
【iOS應用CALayer完成動畫加載的後果】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!