iOS雷达波浪放射效果

.h文件

//
//  RadarAnimationView.h
//  smarthomeAPP
//
//  Created by yu on 2018/11/1.
//  Copyright © 2018年 heysung. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol RadarAnimationViewDelegate <NSObject>
-(void)ClickRadarWIthBtn:(UIButton *)btn;
@end

//雷达效果
@interface RadarAnimationView : UIView

@property (nonatomic, assign) id <RadarAnimationViewDelegate> delegate;

@property (nonatomic,assign) CGFloat Radius;
/**
 *  开始动画
 */
-(void)StopAnimation;
/**
 *  结束动画
 */
-(void)StartAnimation;
/**
 *  圈生成速度 默认80
 */
@property (nonatomic ,assign)float frameInterval;
/**
 *  按钮标题
 */
@property (nonatomic,copy) NSString *btnTitle;
/**
 *  按钮字号
 */
@property (nonatomic,assign) NSInteger font;

/**
 *  按钮图片
 */
@property (nonatomic ,strong) UIImage *image;
/**
 *  按钮选中颜色
 */
@property (nonatomic ,strong) UIImage *selectImage;
/**
 *  波纹颜色
 */
@property (nonatomic,strong) UIColor *raderColor;

/**
 *  按钮状态
 */
@property (nonatomic,assign) BOOL isSelect;
@end

.m文件

//
//  RadarAnimationView.m
//  smarthomeAPP
//
//  Created by yu on 2018/11/1.
//  Copyright © 2018年 heysung. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "RadarAnimationView.h"
#import "SWButton.h"
@interface RadarAnimationView ()<CAAnimationDelegate>
//按钮图片
@property (nonatomic,strong)SWButton *iamgeBtn;
@property (nonatomic,strong)CADisplayLink *disPlayLink;
@end

@implementation RadarAnimationView
{
    CALayer *_layer;
    CAAnimationGroup *_animaTionGroup;
    NSInteger needRemoveCount;
}
- (void)drawRect:(CGRect)rect {

}

-(UIImage *)imageWithColor:(UIColor *)color rect:(CGRect)rect
{
    //根据颜色生成纯色图片
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor=[UIColor clearColor];
        _iamgeBtn = [[SWButton alloc]initWithFrame:self.bounds];
        _iamgeBtn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        //_iamgeBtn.layer.cornerRadius=self.bounds.size.height/2;
        //_iamgeBtn.clipsToBounds=YES;
        _iamgeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        //[_iamgeBtn setTitle:@"标题" forState:UIControlStateNormal];
        //[_iamgeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //_iamgeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
        [_iamgeBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_iamgeBtn];
        self.raderColor = [UIColor colorWithRed:59/255.0 green:212/255.0 blue:137/255.0 alpha:1];

    }
    return self;
}

//按钮点击事件
-(void)imageBtnClick:(UIButton *)btn
{
    if (_delegate && [_delegate respondsToSelector:@selector(ClickRadarWIthBtn:)]) {
        [_delegate ClickRadarWIthBtn:btn];
    }
    btn.selected=!btn.selected;
}

//重写setter方法
-(void)setIsSelect:(BOOL)isSelect
{
    _isSelect=isSelect;
    self.iamgeBtn.selected=isSelect;
}

-(void)setImage:(UIImage *)image
{
    _image = image;
    [self.iamgeBtn setImage:image forState:0];
}

-(void)setSelectImage:(UIImage *)selectImage
{
    _selectImage = selectImage;
    [self.iamgeBtn setImage:selectImage forState:1];
}

-(void)setRaderColor:(UIColor *)raderColor
{
    _raderColor = raderColor;
}
-(void)setBtnTitle:(NSString *)btnTitle
{
    _btnTitle = btnTitle;
    [self.iamgeBtn setTitle:btnTitle forState:UIControlStateNormal];
}

-(void)setFont:(NSInteger)font
{
    _font = font;
    self.iamgeBtn.titleLabel.font=[UIFont systemFontOfSize:font];
}

//-(void)setRadius:(CGFloat)Radius
//{
//    _Radius = Radius;
//    _iamgeBtn.layer.cornerRadius=Radius;
//}


//画雷达圆圈图
-(void)setUp
{
    CGPoint center = CGPointMake(self.bounds.size.height / 2, self.bounds.size.width / 2);
    //使用贝塞尔画圆
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:10 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    shapeLayer.fillColor = self.raderColor.CGColor;
    shapeLayer.opacity = 0.2;
    shapeLayer.path = path.CGPath;

    [self.layer insertSublayer:shapeLayer below:self.iamgeBtn.layer];
    [self addAnimation:shapeLayer];
}

-(void)addAnimation:(CAShapeLayer *)shapeLayer
{
    //雷达圈圈大小的动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    basicAnimation.keyPath = @"path";
    CGPoint center = CGPointMake(self.bounds.size.height / 2, self.bounds.size.width / 2);

    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:10 startAngle:0 endAngle:2 * M_PI clockwise:YES];//path1_radius圈初始大小
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:self.bounds.size.height startAngle:0 endAngle:2 * M_PI clockwise:YES];//path2_radius圈消失大小
    basicAnimation.fromValue = (__bridge id _Nullable)(path1.CGPath);
    basicAnimation.toValue = (__bridge id _Nullable)(path2.CGPath);
    basicAnimation.fillMode = kCAFillModeForwards;

    //雷达圈圈的透明度
    CABasicAnimation *opacityAnimation = [CABasicAnimation animation];
    opacityAnimation.keyPath = @"opacity";
    opacityAnimation.fromValue = @(0.8);//圈初始透明度
    opacityAnimation.toValue = @(0);//消失时透明度
    opacityAnimation.fillMode = kCAFillModeForwards;

    //创建动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[basicAnimation,opacityAnimation];
    //group.duration圈从最小到最大消失的时间
    group.duration = 3;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    group.delegate = self;
    //指定的时间段完成后,动画就自动的从层上移除
    group.removedOnCompletion = YES;
    //添加动画到layer
    [shapeLayer addAnimation:group forKey:nil];

    [self performSelector:@selector(removeLayer:) withObject:shapeLayer afterDelay:3];
}


- (void)removeLayer:(CALayer *)layer
{
    [layer removeFromSuperlayer];
    DMLog(@"removeLayer----在显示圈数量");
}

-(void)StopAnimation
{
    DMLog(@"StopAnimation----在显示圈数量==%lu",self.layer.sublayers.count);
    needRemoveCount =self.layer.sublayers.count-1;
    for (int i=0 ; i<needRemoveCount; i++)
    {
        CAShapeLayer *shaperLayer = (CAShapeLayer *)self.layer.sublayers[0];
        [shaperLayer removeFromSuperlayer];
        shaperLayer = nil;
    }
    [self.layer removeAllAnimations];
    [_disPlayLink invalidate];
    _disPlayLink = nil;
}
-(void)StartAnimation
{
    if (!_disPlayLink)
    {
        _disPlayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(delayAnimation)];
        _disPlayLink.frameInterval =self.frameInterval==0?80:self.frameInterval;
        [_disPlayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }

}
- (void)delayAnimation
{
    [self setUp];
}

-(void)removeFromSuperview
{
    [_disPlayLink invalidate];
    _disPlayLink = nil;
    DMLog(@"_disPlayLink--removeFromSuperview--销毁");
}

-(void)dealloc
{
    DMLog(@"_disPlayLink--dealloc--销毁");
}

@end

相关推荐

微信扫一扫,分享到朋友圈

iOS雷达波浪放射效果
返回顶部

显示

忘记密码?

显示

显示

获取验证码

Close