iOS利用CALayer实现动画加载的效果

所属分类: 软件编程 / IOS 阅读数: 535
收藏 0 赞 0 分享

首先来看看效果图

实现过程如下

控制器调用就一句代码:

[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开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多