想要App启动加载时显示Loading循环图像,图如下:
1) DrawLoadingGif.h
2) DrawLoadingGif.m
#import "DrawLoadingGif.h"
@implementation DrawLoadingGif
- (id)initWithFrame:(NSRect)frameRect
{
self= [superinitWithFrame:frameRect] ;
if(self){
width= frameRect.size.width;
height= frameRect.size.height;
amount=15;
intervalTime = 0.05;
fullDiameter = 100;
minorDiameter = 15;
LoadingState = 0;
fontState=0;
}
return self ;
}
- (DrawLoadingGif*)initWithSize:(CGFloat)boundWidth andHeight:(CGFloat)boundHeight
{
if([superinit] ==self)
{
width= boundWidth ;
height= boundHeight ;
amount=15;
intervalTime = 0.05;
fullDiameter = 100;
minorDiameter = 15;
LoadingState = 0;
fontState=0;
}
return self ;
}
- (void)setAmount:(int)iamount
{
amount= iamount ;
}
- (void)setMoment:(int)imoment
{
moment= imoment ;
}
- (void)setInterval:(double)dinterval
{
intervalTime= dinterval ;
}
- (void)setFullDiameter:(double)dfullDimeter
{
fullDiameter= dfullDimeter ;
}
- (void)setMinorDiameter:(double)dminorDiameter
{
minorDiameter= dminorDiameter ;
}
- (void)start
{
if(isLoading)
{
return;
}
LoadingState = 1;
[NSThread detachNewThreadSelector:@selector(startDrawLoadingGif) toTarget:self withObject:nil];
}
- (void)startDrawLoadingGif {
isLoading = YES;
while (isLoading) {
for(moment=0;moment
[NSThread sleepForTimeInterval:intervalTime];
dispatch_async(dispatch_get_main_queue(), ^{
[selfsetNeedsDisplay:YES];
});
if(!isLoading) {
break;
}
}
LoadingState = 2;
}
}
- (void)stop
{
isLoading = NO ;
}
- (void)drawRect:(NSRect)dirtyRect
{
// [super drawRect:dirtyRect];
// Drawing code here.
// NSColor* color =[NSColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0 ];
// NSColor* color =[NSColor blackColor];
// [self drawRect:dirtyRect setFill:color setStroke:color];
if(LoadingState==0) {
return;
}elseif(LoadingState==1){
for(inti =0; i
CGFloat w = (i*1.0/amount)*(minorDiameter-1)*moment/amount+1;
CGFloath = w;
CGFloatx = (int)(width/2+fullDiameter*sin((moment+i)*2*M_PI/amount)-w/2);
CGFloaty = (int)(height/2+fullDiameter*cos((moment+i)*2*M_PI/amount)-w/2);
NSBezierPath*bezierPath = [selfdrawBezierPath:x
:y
:w
:h];
CGFloatcircleAlpha = i*0.8/amount+0.2;
[bezierPathclosePath];
[[NSColor colorWithSRGBRed:1.0 green:1.0 blue:0.0 alpha:circleAlpha] setFill];
[bezierPathfill];
}
}else{
for(inti =0; i
NSBezierPath*bezierPath = [selfdrawBezierPath:(int)(width/2+fullDiameter*sin((moment+i)*2*M_PI/amount)-((i*1.0/amount)*(minorDiameter-1)+1)/2)
:(int)(height/2+fullDiameter*cos((moment+i)*2*M_PI/amount)-((i*1.0/amount)*(minorDiameter-1)+1)/2)
:(i*1.0/amount)*(minorDiameter-1)+1
:(i*1.0/amount)*(minorDiameter-1)+1];
CGFloatcircleAlpha = i*0.6/amount+0.4;
[bezierPathclosePath];
[[NSColor colorWithSRGBRed:1.0 green:1.0 blue:0.0 alpha:circleAlpha] setFill];
[bezierPathfill];
}
}
NSString*title =@"Loading";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
CGFloattitleRed;
if(fontState==0) {
titleRed =1-moment*1.0/amount;
}else{
titleRed =moment*1.0/amount;
}
if(moment==amount-1) {
fontState=fontState==0?1:0;
}
dict[NSForegroundColorAttributeName] = [NSColor colorWithSRGBRed:titleRed green:1.0 blue:0.0 alpha:1.0];
dict[NSFontAttributeName] = [NSFont systemFontOfSize:32.0];
[titledrawInRect:NSMakeRect(width/2-55, height/2-20, 120, 40) withAttributes:dict];
}
- (NSBezierPath *)drawBezierPath:(CGFloat)x :(CGFloat)y :(CGFloat)w :(CGFloat)h
{
NSBezierPath *bezierPath = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(x,y,w,h)];
returnbezierPath;
}
- (void)drawRect:(NSRect)rect
setFill:(NSColor*)fillColor
{
[fillColorsetFill];
[NSBezierPathfillRect:rect];
}
- (void)drawRect:(NSRect)rect
setStroke:(NSColor*)strokeColor
{
[strokeColorsetStroke];
[NSBezierPathstrokeRect:rect];
}
- (void)drawRect:(NSRect)rect
setFill:(NSColor*)fillColor
setStroke:(NSColor*)strokeColor
{
[selfdrawRect:rectsetFill:fillColor];
[selfdrawRect:rectsetStroke:strokeColor];
}
@end
3) 使用DrawLoadingGif类的方法
网友评论