显示脉冲效果的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
类的详细细节: