手繪彩色光圈

2021-09-23 21:33:31 字數 3675 閱讀 4624

手繪彩色光圈

效果(本人根據上述原始碼進行了很大的改動):

原始碼:multicolorview.h

//

// multicolorview.h

// multicolorlayerdemo

//// created by liuyu on 14-7-5.

//// modify by youxianming on 14-7-13

////

#import @inte***ce multicolorview : uiview

@property (nonatomic, assign) cgfloat linewidth; // 圓的線寬

@property (nonatomic, assign) cftimeinterval sec; // 秒

@property (nonatomic, assign) cgfloat percent; // 百分比

@property (nonatomic, strong) nsarray *colors; // 顏色組(cgcolor)

- (void)startanimation;

- (void)endanimation;

@end

multicolorview.m

//

// multicolorview.m

// multicolorlayerdemo

//// created by liuyu on 14-7-5.

//// modify by youxianming on 14-7-13

////

#import "multicolorview.h"

@inte***ce multicolorview ()

@property (nonatomic, strong) cashapelayer *circlelayer;

@end

@implementation multicolorview

#pragma mark - 將當前view的layer替換成漸變色layer

+ (class)layerclass

#pragma mark - 初始化

- (instancetype)initwithframe:(cgrect)frame

return self;

}#pragma mark - 配置顏色

- (void)setupmulticolor

// 給漸變色layer設定顏色

[gradientlayer setcolors:[nsarray arraywitharray:colors]];

}else

}#pragma mark - 配置圓形

- (cashapelayer *)producecircleshapelayer

else

uibezierpath *circlepath = [uibezierpath bezierpathwitharccenter:circlecenter

radius:circleradius

startangle:m_pi

endangle:-m_pi

clockwise:no];

// 生產出乙個圓形路徑的layer

_circlelayer.path = circlepath.cgpath;

_circlelayer.strokecolor = [uicolor whitecolor].cgcolor;

_circlelayer.fillcolor = [[uicolor clearcolor] cgcolor];

if (_linewidth == 0)

else

// 可以設定出圓的完整性

_circlelayer.strokestart = 0;

_circlelayer.strokeend = 1.0;

return _circlelayer;

}#pragma mark - animation

- (void)startanimation

else

animation.repeatcount = maxfloat;

animation.fromvalue = [nsnumber numberwithdouble:0];

animation.tovalue = [nsnumber numberwithdouble:m_pi*2];

[self.layer addanimation:animation forkey:nil];

}@synthesize percent = _percent;

-(cgfloat)percent

- (void)setpercent:(cgfloat)percent

}- (void)endanimation

@end

viewcontroller.m

//

// viewcontroller.m

// multicolorlayerdemo

//// created by liuyu on 14-7-5.

//// modify by youxianming on 14-7-13

////

#import "viewcontroller.h"

#import "multicolorview.h"

@inte***ce viewcontroller ()

@property (nonatomic, strong) nstimer *timer;

@property (nonatomic, strong) multicolorview *showview;

@end

@implementation viewcontroller

- (void)viewdidload

- (void)event:(id)object

@end

原理:1. 改變view的backedlayer為漸變色layer

2. 給這個view新增乙個shapelayer

3. 將這個shapelayer當這個view的backedlayer

4. 動畫

5. 結束:)

可以設定的引數有4個:)