扇形动画

新建UIview的类:paintview

//  PieView.h

#import <UIKit/UIKit.h>

@interface PieView : UIView

- (id)initWithFrame:(CGRect)frame;
- (void)stroke;

@end

//  PieView.m

#import "PieView.h"
#define kAnimationDuration 3.0f

#define kPieBackgroundColor [UIColor grayColor]
#define kPieFillColor [UIColor clearColor].CGColor
#define kPieRandColor [UIColor orangeColor].CGColor
#define kLabelLoctionRatio (1.2*bgRadius)

@interface PieView()

@property (nonatomic) CAShapeLayer *bgLayer;

@end


@implementation PieView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.hidden = YES;
        
        
        //1.pieView中心点
        CGFloat centerWidth = self.frame.size.width * 0.5f;
        CGFloat centerHeight = self.frame.size.height * 0.5f;
        CGFloat centerX = centerWidth;
        CGFloat centerY = centerHeight;
        CGPoint centerPoint = CGPointMake(centerX, centerY);
        CGFloat radiusBasic = centerWidth > centerHeight ? centerHeight : centerWidth;
        
        
        
        
        //2.路径
        
               
        
        
        //背景路径
        CGFloat bgRadius = radiusBasic * 0.5;
        UIBezierPath *bgPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:bgRadius   startAngle:-M_PI_2     endAngle:M_PI_2 * 3  clockwise:YES];
        CAShapeLayer* bgLayer = [CAShapeLayer layer];
        bgLayer.fillColor   = [UIColor clearColor].CGColor;
        bgLayer.strokeColor = [UIColor lightGrayColor].CGColor;
        bgLayer.zPosition   = 1;
        bgLayer.lineWidth   = bgRadius * 2.0f;//线宽是扇形半径*2
        bgLayer.path        = bgPath.CGPath;
        self.bgLayer = bgLayer;
        
 
        //pie路径
        CGFloat pieRadius = radiusBasic * 0.5;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:pieRadius startAngle:-M_PI/2 endAngle:M_PI*3/2 clockwise:YES];
        
        CAShapeLayer *pie = [CAShapeLayer layer];
        pie.fillColor   = kPieFillColor;
        pie.strokeColor = kPieRandColor;
        pie.lineWidth   = pieRadius * 2.0f;//线宽是扇形半径*2
        pie.zPosition   = 2;
        pie.path        = path.CGPath;
            
        [self.layer addSublayer:pie];
        
        self.layer.mask = bgLayer;
        
    }
    return self;
}


- (void)stroke
{
    //画图动画
    self.hidden = NO;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration  = kAnimationDuration;
    animation.fromValue = @0.0f;
    animation.toValue   = @1.0f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 animation.removedOnCompletion = YES;
    [self.bgLayer addAnimation:animation forKey:@"circleAnimation"];
    
}

- (void)dealloc
{
    [self.layer removeAllAnimations];
}

@end

//
//  ViewController.m
//  xiazai
//
//  Created by chenvinci on 2017/2/10.
//  Copyright © 2017年 cuijing. All rights reserved.
//

#import "ViewController.h"
#import "PieView.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width



//#define width [UIScreen mainScreen].bounds.size.width
#define height [UIScreen mainScreen].bounds.size.height
#define centerX width/2
#define centerY height/2
#define radius1 100



@interface ViewController ()
@property(nonatomic) PieView*pie;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.pie = [[PieView alloc] initWithFrame:CGRectMake((kScreenWidth - 200) * 0.5f, 100, 200, 200) ];
    [self.view addSubview:self.pie];
    

    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.pie stroke];
    
  }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用到的类及其简单的解释: 1、UIBezierPath:使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线...
    snydder阅读 527评论 0 2
  • 前言:最近比较闲,正好利用这段时间把现在项目用的东西封装一下,方便以后复用,当然好的东西还是要分享。一起学习,一起...
    码渣阅读 3,452评论 5 9
  • 一、定时任务 方法1:performSelector 方法2:GCD 方法3:NSTimer NSTimer「定时...
    _凉风_阅读 1,312评论 2 8
  • 最近在做iOS界面转场的动画,写完转场入口后基本元素还是回归到我们常用的基本动画代码,有关动画的帖子网络上一搜一大...
    大雄記阅读 5,545评论 0 18
  • 今天一朋友很郁闷,中午陪他聊了两个多小时,缘由是业绩和人缘比他差的一位同事提拔了,他很愤愤不平,而且听说那位同事搞...
    凌晨陈玲阅读 338评论 1 2