码迷,mamicode.com
首页 > 其他好文 > 详细

显示脉冲效果的PulsingView

时间:2014-11-01 00:50:54      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   使用   for   sp   

显示脉冲效果的PulsingView

bubuko.com,布布扣

效果如下:

bubuko.com,布布扣

源码:

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

类的详细细节:

bubuko.com,布布扣

 

显示脉冲效果的PulsingView

标签:style   blog   http   io   color   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/YouXianMing/p/4065197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!