ios雷达脉冲效果
来源:互联网 发布:自学linux书籍推荐 编辑:程序博客网 时间:2024/04/28 12:49
- <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">具体先看效果图</span>
直接上代码:
- @interface CCRadarView : UIView
- /*当前雷达中心缩略图*/
- @property (nonatomic,strong)UIImage * thumbnailImage;
- -(instancetype)initWithFrame:(CGRect)frame andThumbnail:(NSString *)thumbnailUrl;
- @end
- @interface CCRadarView()
- @property (nonatomic,weak)CALayer * animationLayer;
- @end
- @implementation CCRadarView
- </pre><pre name="code" class="objc"><pre name="code" class="objc">-(instancetype)initWithFrame:(CGRect)frame andThumbnail:(NSString *)thumbnailUrl{
- self = [super initWithFrame:frame];
- if (self) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resume) name:UIApplicationDidBecomeActiveNotification object:nil];
- self.backgroundColor = [UIColor clearColor];
- self.thumbnailImage = [UIImage imageNamed:@"friends_default_portrait.png"];
- }
- return self;
- }
- - (void)drawRect:(CGRect)rect {
- [[UIColor clearColor] setFill];
- UIRectFill(rect);
- NSInteger pulsingCount = 3;//雷达上波纹的条数
- double animationDuration = 2;//一组动画持续的时间,直接决定了动画运行快慢。
- CALayer * animationLayer = [[CALayer alloc]init];
- self.animationLayer = animationLayer;
- for (int i = 0; i < pulsingCount; i++) {
- CALayer * pulsingLayer = [[CALayer alloc]init];
- pulsingLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
- pulsingLayer.backgroundColor = [UIColor colorWithHexRGB:@"d2e5fb"].CGColor;//圈圈背景颜色,不设置则为透明。
- pulsingLayer.borderColor = [UIColor colorWithHexRGB:@"d2e5fb"].CGColor;
- pulsingLayer.borderWidth = 1.0;
- pulsingLayer.cornerRadius = rect.size.height/2;
- 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];
- //雷达圆圈在n个运行阶段的透明度,n为数组长度。
- CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
- //雷达运行四个阶段不同的透明度。
- opacityAnimation.values = @[[NSNumber numberWithDouble:1.0],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.3],[NSNumber numberWithDouble:0.0]];
- //雷达运行的不同的四个阶段,为0.0表示刚运行,0.5表示运行了一半,1.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.layer addSublayer:self.animationLayer];
- //以下部分为雷达中心的用户缩略图。雷达圈圈也是从该图中心发出。
- CALayer * thumbnailLayer = [[CALayer alloc]init];
- thumbnailLayer.backgroundColor = [UIColor whiteColor].CGColor;
- CGRect thumbnailRect = CGRectMake(0, 0, 46, 46);
- 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;
- [self.layer addSublayer:thumbnailLayer];
- }
- <pre name="code" class="objc">// 动画因应用程序进入后台后会停止。故避免在重新激活程序时出现卡死假象。
- - (void)resume{
- if (self.animationLayer) {
- [self.animationLayer removeFromSuperlayer];
- [self setNeedsDisplay];
- }
- }
- -(void)dealloc{
- [[NSNotificationCenter defaultCenter]removeObserver:self];
- }
- </pre><pre name="code" class="objc">其中有个uicolor (category)
- </pre><pre name="code" class="objc"><pre name="code" class="objc">+ (UIColor *)colorWithHexRGB:(NSString *)rbg
- {
- NSString *cString = [[rbg stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
- if (cString.length < 6) {
- return [UIColor clearColor];
- }
- if ([cString hasPrefix:@"0X"]) {
- cString = [cString substringFromIndex:2];
- }
- if ([cString hasPrefix:@"#"]) {
- cString = [cString substringFromIndex:1];
- }
- if (cString.length != 6) {
- return [UIColor clearColor];
- }
- NSRange range = NSMakeRange(0, 2);
- NSString *rString = [cString substringWithRange:range];
- range.location = 2;
- NSString *gString = [cString substringWithRange:range];
- range.location = 4;
- NSString *bString = [cString substringWithRange:range];
- unsigned int red = 0;
- unsigned int green= 0;
- unsigned int blue = 0;
- [[NSScanner scannerWithString:rString] scanHexInt:&red];
- [[NSScanner scannerWithString:gString] scanHexInt:&green];
- [[NSScanner scannerWithString:bString] scanHexInt:&blue];
- return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
- }
0 0
- ios雷达脉冲效果
- iOS 雷达扫描效果 实现
- iOS 雷达动画效果实现
- ios脉冲动画效果实现小结
- Android UI设计之<十四>自定义ViewGroup,实现绚丽的仿支付宝咻一咻雷达脉冲效果
- Android UI设计之<十四>自定义ViewGroup,实现绚丽的仿支付宝咻一咻雷达脉冲效果
- IOS 雷达效果(Layer的动画)笔记
- iOS开发雷达动画效果实现
- 雷达效果,
- CABasicAnimation 脉冲效果
- iOS雷达和类似水波纹效果的实现
- iOS核心动画实现仿支付宝咻咻、雷达效果
- view写雷达效果
- Android 雷达扫描效果
- 雷达波纹效果
- 二相伪随机脉冲编码雷达-信号处理仿真
- iOS雷达图
- iOS雷达扫描demo
- Windows驱动开发入门系列教程
- centos中mysql无法显示中文问题
- mt7688编译mplayer
- 将列表按字母排序如通讯录
- tomcat源代码解读
- ios雷达脉冲效果
- iOS的一些内存注意点
- 手机上播放视频
- 详说Swift 2.0中的错误处理
- Object-C基础(14)——文件
- Android快速开发系列 10个常用工具类
- android--显式跳转和隐式跳转的区别用法
- UIButton常用属性总结
- 数据库增量备份和差异备份的区别