国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS利用CALayer實(shí)現(xiàn)動(dòng)畫加載的效果

2020-07-26 03:08:14
字體:
供稿:網(wǎng)友

首先來看看效果圖

實(shí)現(xiàn)過程如下

控制器調(diào)用就一句代碼:

[self showLoadingInView:self.view];

方便控制器如此調(diào)用,就要為控制器添加一個(gè)分類

.h文件

#import <UIKit/UIKit.h>#import "GQCircleLoadView.h"@interface UIViewController (GQCircleLoad)//顯示動(dòng)畫- (void)showLoadingInView:(UIView*)view;//隱藏動(dòng)畫- (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畫出圓圈,并且動(dòng)畫的實(shí)現(xiàn)

#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]]; // 旋轉(zhuǎn)(可打開試試效果)// 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"];}// 動(dòng)畫實(shí)現(xiàn)- (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就不上傳了,總共四個(gè)文件代碼已經(jīng)全貼上了!

總結(jié)

以上就是這篇文章的全部內(nèi)容了,有興趣可以試試打開上面的旋轉(zhuǎn)的動(dòng)畫代碼,關(guān)閉旋轉(zhuǎn)代碼,進(jìn)一步修改也可實(shí)現(xiàn)出QQ郵箱的下拉刷新效果,希望這篇文章的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂托克前旗| 唐河县| 册亨县| 晋宁县| 和平县| 山阴县| 龙门县| 广河县| 东明县| 玉龙| 慈利县| 类乌齐县| 通化市| 肇州县| 原阳县| 安化县| 桦甸市| 兴城市| 呼玛县| 汤原县| 顺平县| 台南市| 古田县| 隆化县| 东山县| 绥滨县| 皋兰县| 浮梁县| 遂昌县| 惠东县| 祁东县| 阿合奇县| 潢川县| 新邵县| 永泰县| 靖宇县| 漯河市| 金沙县| 昂仁县| 丁青县| 吴忠市|