最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年10月21日 10:35:53   投稿:daisy  
網(wǎng)上關(guān)于動(dòng)畫加載的效果大多每一個(gè)圓圈都是使用UIView,因?yàn)檫@種容易控制,但是這里用的是CALayer,文中給出了詳細(xì)的實(shí)現(xiàn)示例代碼,相信會(huì)對(duì)大家的學(xué)習(xí)和理解很有幫助,感興趣的朋友們下面來一起看看吧。

首先來看看效果圖

實(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.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]];

 // 旋轉(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é)

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

相關(guān)文章

最新評(píng)論

彩票| 松滋市| 宜都市| 忻城县| 义乌市| 东丰县| 和田市| 疏勒县| 永济市| 石屏县| 峡江县| 神池县| 新安县| 秭归县| 黄大仙区| 勃利县| 达孜县| 德化县| 高青县| 崇义县| 北流市| 郯城县| 冀州市| 洛川县| 福泉市| 梁平县| 莱西市| 乐亭县| 甘肃省| 谢通门县| 特克斯县| 都兰县| 南华县| 五常市| 绵阳市| 昌平区| 津市市| 大港区| 仪征市| 亳州市| 西充县|