iOS 实现类似雷达效果的核心代码

时间:2023-03-09 02:53:36
iOS 实现类似雷达效果的核心代码
 -(void)drawRect:(CGRect)rect
{
[[UIColor clearColor]setFill];
UIRectFill(rect);
NSInteger pulsingCount = ;
double animationDuration = ; CALayer * animationLayer = [[CALayer alloc]init];
self.animationLayer = animationLayer; for (int i = ; i < pulsingCount; i++) {
CALayer * pulsingLayer = [[CALayer alloc]init];
pulsingLayer.frame = CGRectMake(, , rect.size.width, rect.size.height);
pulsingLayer.backgroundColor = [UIColor colorWithRed:92.0/255.0 green:181.0/255.0 blue:217.0/255.0 alpha:1.0].CGColor;
pulsingLayer.borderColor = [UIColor colorWithRed:92.0/255.0 green:181.0/255.0 blue:217.0/255.0 alpha:1.0].CGColor;
pulsingLayer.borderWidth = 1.0;
pulsingLayer.cornerRadius = rect.size.height/; CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; CAAnimationGroup * animationGroup = [[CAAnimationGroup alloc]init];
animationGroup.fillMode = kCAFillModeBoth;
animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration/(double)pulsingCount;
animationGroup.duration = animationDuration; animationGroup.repeatCount = HUGE_VAL;
animationGroup.timingFunction = defaultCurve; CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.autoreverses = NO;
scaleAnimation.fromValue = [NSNumber numberWithDouble:0.2];
scaleAnimation.toValue = [NSNumber numberWithDouble:1.0]; CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.values = @[[NSNumber numberWithDouble:1.0],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.3],[NSNumber numberWithDouble:0.0]];
opacityAnimation.keyTimes = @[[NSNumber numberWithDouble:0.0],[NSNumber numberWithDouble:0.25],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:1.0]];
animationGroup.animations = @[scaleAnimation,opacityAnimation]; [pulsingLayer addAnimation:animationGroup forKey:@"pulsing"];
[animationLayer addSublayer:pulsingLayer];
}
self.animationLayer.zPosition = -;//重新加载时,使动画至底层
[self.layer addSublayer:self.animationLayer]; CALayer * thumbnailLayer = [[CALayer alloc]init];
thumbnailLayer.backgroundColor = [UIColor whiteColor].CGColor;
CGRect thumbnailRect = CGRectMake(, , , );
thumbnailRect.origin.x = (rect.size.width - thumbnailRect.size.width)/2.0;
thumbnailRect.origin.y = (rect.size.height - thumbnailRect.size.height)/2.0;
thumbnailLayer.frame = thumbnailRect;
thumbnailLayer.cornerRadius = 23.0;
thumbnailLayer.borderWidth = 1.0;
thumbnailLayer.masksToBounds = YES;
thumbnailLayer.borderColor = [UIColor whiteColor].CGColor;
UIImage * thumbnail = self.thumbnailImage;
thumbnailLayer.contents = (id)thumbnail.CGImage;
thumbnailLayer.zPosition = -;
[self.layer addSublayer:thumbnailLayer];
}

iOS 实现类似雷达效果的核心代码iOS 实现类似雷达效果的核心代码