iOS利用CALayer實現動畫載入的效果_IOS

來源:互聯網
上載者:User

首先來看看效果圖

實現過程如下

控制器調用就一句代碼:

[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.heightstatic 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郵箱的下拉重新整理效果,希望這篇文章的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.