第一步:創建一個View,將這個View添加到當前的控制器
如:
CGFloat timeW = self.view.bounds.size.width; timeAnimation * timean = [[timeAnimation alloc]initWithFrame:CGRectMake(0,0,timeW,timeW)]; timean.center = CGPointMake(self.view.bounds.size.width*0.5, self.view.bounds.size.height *0.5); [self.view addSubview:timean];
第二步:在View的.m文件中添加如下代碼
#define PI 3.14159265358979323846
#define kradius self.bounds.size.width*0.1
-(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.frame = frame; self.backgroundColor = [UIColor clearColor]; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); [self greenRound:context]; self.alpha = 0.5; [self test]; } -(void)test { // 2.創建縮放動畫對象 CABasicAnimation *scale = [CABasicAnimation animation]; scale.keyPath = @"transform.scale"; scale.fromValue =[NSNumber numberWithFloat:0.0]; scale.toValue =[NSNumber numberWithFloat:1.0]; CABasicAnimation *scale1 = [CABasicAnimation animation]; scale1.keyPath = @"opacity"; scale1.fromValue =[NSNumber numberWithFloat:1.0]; scale1.toValue =[NSNumber numberWithFloat:0.0]; // 4.將所有的動畫添加到動畫組中 CAAnimationGroup *group = [CAAnimationGroup animation]; group.animations = @[scale,scale1]; group.duration =.6; group.repeatCount = HUGE_VALF; group.removedOnCompletion = NO; group.fillMode = kCAFillModeForwards; [self.layer addAnimation:group forKey:nil]; } /**畫綠色的圓*/ -(void)greenRound:(CGContextRef)context { CGContextSetRGBStrokeColor(context, 33/255.0, 177/255.0, 75/255.0, 1);//畫筆線的顏色 CGContextSetLineWidth(context, 4.0);//線的寬度 // x,y為圓點坐標,radius半徑,startAngle為開始的弧度,endAngle為 結束的弧度,clockwise 0為順時針,1為逆時針。 CGContextAddArc(context, self.bounds.size.width *0.5,self.bounds.size.height*0.5,self.bounds.size.width*0.21, 0, 2*PI, 0); //添加一個圓 CGContextDrawPath(context, kCGPathStroke); //繪制路徑 }