2018/4/16 15:09:18当前位置推荐好文程序员浏览文章

想到写加载动画起因:目前公司的两个项目一直使消耗的是自己封装的一个简易动画,现在打算把动画简单的优化重构一下,做为动画工具调消耗。动画效果非常简洁轻量,并且不依赖任何库,欢迎各位来围观吐槽~。

动画效果示例如下图所示:

loading.png

1.简单的调消耗API:

+(instancetype)shareAnimation; //开始动画- (void)startAnimating; //结束动画- (void)stopAnimating;// 动画状态- (BOOL)isAnimating;//能设置动画提醒文字-(void)startAnimatingWithTitle:(NSString )title;

1.简单的调消耗API:

使消耗UIView类实现动画a.初始化动画视图控件,同时保存视图transform和center初始值-(instancetype)init{    if (self = [super init]) {        self.frame = CGRectMake(0, 0, 100, 50);        self.center = CGPointMake(kScreenWidth/2.0, kScreenHeight/2.0);                UIView blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];        blueView.center = CGPointMake(35, 25);        blueView.backgroundColor = [UIColor blueColor];        [self drawCircleView:blueView];        self.blueView = blueView;        [self addSubview:blueView];                UIView greenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];        greenView.center = CGPointMake(75, 25);        greenView.backgroundColor = [UIColor greenColor];        self.greenView = greenView;//通过CAShapeLayer结合UIBezierPath生成圆角        [self drawCircleView:greenView];        [self addSubview:greenView];                blueCenter = self.blueView.center;        greenCenter= self.greenView.center;                blueTransform = self.blueView.transform;        greenTransform = self.greenView.transform;        self.hidden = YES;    }    return self;} 绘制圆角- (void)drawCircleView:(UIView)view{    CAShapeLayer maskLayer = [CAShapeLayer layer];    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:view.bounds.size.width/2.0].CGPath;    view.layer.mask = maskLayer;}注意:当然下面两个方法也能生成圆角,但是会产生离屏渲染,产出性可以问题,故不推荐使消耗。    self.view.layer.masksToBounds = YES;    self.view.layer.cornerRadius = 5;b.基本写法,代码必需放在Begin和Commit之间:    [UIView beginAnimations:@"LoadingAnimation" context:nil];    [UIView setAnimationDelegate:self];    [UIView setAnimationDuration:0.5];    [UIView setAnimationRepeatAutoreverses:YES];    [UIView setAnimationRepeatCount:MAXFLOAT];    //通过    self.blueView.transform = CGAffineTransformMakeScale(5/3.0, 5/3.0);    self.blueView.center = CGPointMake(25, 25);    self.greenView.transform = CGAffineTransformMakeScale(0.6, 0.6);    self.greenView.center = CGPointMake(65, 25);        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];    [UIView commitAnimations];c.动画结束时候通过代理商方法恢复至原始的视图transform和center  -(void) animationDidStop:(NSString )animationID finished:(NSNumber )finished context:(void )context   {      _blueView.center = blueCenter;      _blueView.transform = blueTransform;      _greenView.center = greenCenter;      _greenView.transform =greenTransform;   }

github地址 524429264/LoadingAnimation.git

扫描下方二维码关注我

零距离仰望星空

在此感谢各位读者的来访,您的关注是我写作分享的最大动力。

网友评论