显示脉冲效果的PulsingView

简介:

显示脉冲效果的PulsingView

效果如下:

源码:

PulsingView.h 与 PulsingView.m

//
//  PulsingView.h
//  PulsingView
//
//  Created by YouXianMing on 14/10/29.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PulsingView : UIView

/**
 *  startScale与endScale需要设置值
 */
@property (nonatomic, assign) CGFloat  startScale;
@property (nonatomic, assign) CGFloat  endScale;

/**
 *  动画时间
 */
@property (nonatomic, assign) NSTimeInterval duration;

/**
 *  最高程度的alpha
 */
@property (nonatomic, assign) CGFloat  maxAlpha;

/**
 *  用来做动画的view
 */
@property (nonatomic, strong) UIView  *inputView;

/**
 *  做动画
 */
- (void)startAnimation;

@end


//
//  PulsingView.m
//  PulsingView
//
//  Created by YouXianMing on 14/10/29.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "PulsingView.h"

@interface PulsingView ()

@end

@implementation PulsingView

- (void)startAnimation {
    CGFloat tmpStartScale = (_startScale < 0 ? 0.5 : _startScale);
    CGFloat tmpEndScale   = (_endScale < 0 ? 2 : _endScale);
    
    _inputView.transform  = CGAffineTransformMake(tmpStartScale, 0, 0, tmpStartScale, 0, 0);
    _inputView.alpha      = ((_maxAlpha <= 0 || _maxAlpha > 1) ? 1: _maxAlpha);
    
    [UIView animateWithDuration:(_duration <= 0 ? 1.f : _duration)
                          delay:0.f options:UIViewAnimationOptionCurveEaseOut animations:^{
                              _inputView.transform = CGAffineTransformMake(tmpEndScale, 0, 0, tmpEndScale, 0, 0);
                              _inputView.alpha     = 0.f;
                          } completion:nil];
}

@synthesize inputView = _inputView;
- (void)setInputView:(UIView *)inputView {
    _inputView       = inputView;
    inputView.frame  = inputView.bounds; // 重设inputView的frame值
    self.bounds      = inputView.bounds; // 重设view的bounds
    
    // 先删除掉所有的子view
    [[self subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *tmp = (UIView *)obj;
        [tmp removeFromSuperview];
    }];
    
    [self addSubview:inputView];
}
- (UIView *)inputView {
    return _inputView;
}

@end

使用:
//
//  ViewController.m
//  PulsingView
//
//  Created by YouXianMing on 14/10/29.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "PulsingView.h"
#import "YXGCD.h"

@interface ViewController ()

@property (nonatomic, strong) NSTimer     *timer;
@property (nonatomic, strong) PulsingView *pulsingView;
@property (nonatomic, strong) UIView      *circleView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置背景
    self.view.backgroundColor      = [UIColor blackColor];
 
    // 用来显示的view
    _circleView                    = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _circleView.backgroundColor    = [self randomColor];
    _circleView.layer.cornerRadius = 50.f;
    
    // 脉冲view
    _pulsingView            = [PulsingView new];
    _pulsingView.inputView  = _circleView;
    _pulsingView.startScale = 0.1f;
    _pulsingView.duration   = 1.f;
    _pulsingView.center     = self.view.center;
    [self.view addSubview:_pulsingView];
    
    // 定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.2f
                                              target:self
                                            selector:@selector(animationTimerEvent)
                                            userInfo:nil
                                             repeats:YES];
}

- (void)animationTimerEvent {
    _circleView.backgroundColor = [self randomColor];
    _pulsingView.endScale       = arc4random()%200/100.f + 1.f;
    [_pulsingView startAnimation];
}

- (UIColor *)randomColor {
    return [UIColor colorWithRed:arc4random()%255/255.f
                           green:arc4random()%255/255.f
                            blue:arc4random()%255/255.f
                           alpha:1.f];
}

@end

类的详细细节:

目录
相关文章
|
5月前
|
C语言
输出4种波形的函数信号发生器
设计了一款基于MCS-51单片机的函数信号发生器,能生成四种波形(正弦、方、三角、锯齿),频率范围10-100Hz,步进值0.1-10Hz。系统包括5V电源、AT89C51单片机、DAC0832、LM358、LCD1602、键盘和LED电路。通过按键切换波形、设定频率和步进值,LCD实时显示信息,LED指示波形类型。Proteus和Altium仿真验证了设计功能。
93 10
|
5月前
|
存储 算法 异构计算
m基于FPGA的多功能信号发生器verilog实现,包含testbench,可以调整波形类型,幅度,频率,初始相位等
使用Vivado 2019.2仿真的DDS信号发生器展示了正弦、方波、锯齿波和三角波的输出,并能调整幅度和频率。DDS技术基于高速累加器、查找表和DAC,通过频率控制字和初始相位调整产生各种波形。Verilog程序提供了一个TEST模块,包含时钟、复位、信号选择、幅度和频率控制输入,以生成不同波形。
151 18
|
5月前
|
数据采集 网络架构
LabVIEW控制DO通道输出一个精确定时的数字波形
LabVIEW控制DO通道输出一个精确定时的数字波形
78 4
信号与系统概念题1、信号时移只改变信号的相位频谱,不改变信号的幅度频谱2、设两子系统的单位冲击响应分别为h1(t)和h2(t),则由其并联组成的复合系统的单位冲激响应 h(t)=h1(t)+h2(
信号与系统概念题1、信号时移只改变信号的相位频谱,不改变信号的幅度频谱2、设两子系统的单位冲击响应分别为h1(t)和h2(t),则由其并联组成的复合系统的单位冲激响应 h(t)=h1(t)+h2(
|
存储
51单片机--动态数码管显示
51单片机--动态数码管显示
246 0
LC正弦波振荡器【高频电子线路】【Multisim】
LC正弦波振荡器【高频电子线路】【Multisim】
240 0
LC正弦波振荡器【高频电子线路】【Multisim】
|
存储 Linux 程序员
理解--信号
理解--信号
117 0