码迷,mamicode.com
首页 > 其他好文 > 详细

一个扇形的动画效果

时间:2015-05-20 23:57:54      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

用cashapelayer和core animation实现的一个扇形的动画效果。

直接贴代码 可以让cashapelayer跟着动画里面的一个path动态的绘图

-(void)addarcanimation
{
    CAShapeLayer *linelayer=[CAShapeLayer layer];
    linelayer.strokeColor=[[UIColor colorWithRed:0.400 green:1.000 blue:1.000 alpha:1.000] CGColor];
    linelayer.fillColor=nil;//不能动态填充,你可以试试把设置个颜色,把linewidth设置小一些 看有什么效果。
    linelayer.lineWidth=40.0f;//线条宽度必须设高一点,设低了就是空心的
    linelayer.lineCap=kCALineCapButt;
    [self.view.layer addSublayer:linelayer];
//把layer插入你需要绘图的layer
    
    float x=self.view.bounds.size.width/2;
    float y=self.totaltimelable.frame.origin.y+self.totaltimelable.frame.size.height+(self.xuanzeLable.frame.origin.y-(self.totaltimelable.frame.origin.y+self.totaltimelable.bounds.size.height))/2;
    
    float radius=(self.xuanzeLable.frame.origin.y-(self.totaltimelable.frame.origin.y+self.totaltimelable.bounds.size.height))/2/2-5;
    
    UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:radius startAngle:0 endAngle:M_PI*2*[self totalworktimeproportionoftotaltime] clockwise:YES];
    
    linelayer.path=path.CGPath;//把layer的path设为动画里尼创建的path 必须是cgpath类型

    
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    
    animation.duration=1.0f;
    
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    animation.fromValue=[NSNumber numberWithFloat:0.0f];
    
    animation.toValue=[NSNumber numberWithFloat:1.0f];
    
    animation.autoreverses=NO;
    
    animation.fillMode=kCAFillModeForwards;
    
    animation.repeatCount=1;
    
    [linelayer addAnimation:animation forKey:@"strokeEndAnimation"];
    
    linelayer.strokeEnd=1.0f;
    //一定要把strokeEnd是什么搞清楚,它代表了layer的绘制程度,从0到1,0的话就是什么都不绘制,1的话就是完全绘制。
    
}

 

一个扇形的动画效果

标签:

原文地址:http://www.cnblogs.com/xiaomingtongxue/p/4518441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!