新建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中心點(diǎn)
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
{
//畫圖動(dòng)畫
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];
}