心形加载的view

效果:

素材图片:

源码:

StarView.h 与 StarView.m

//
// StarView.h
// Star
//
// Created by XianMingYou on 15/3/13.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h> @interface StarView : UIView @property (nonatomic, strong) UIColor *backgroundViewColor;
@property (nonatomic, strong) UIColor *animationViewColor;
@property (nonatomic, assign) NSTimeInterval animationDuration; + (instancetype)createWithFrame:(CGRect)frame
backgroundViewColor:(UIColor *)bgColor
animationViewColor:(UIColor *)anColor; - (void)percent:(CGFloat)percent animated:(BOOL)animated; @end
//
// StarView.m
// Star
//
// Created by XianMingYou on 15/3/13.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "StarView.h"
#import "UIView+SetRect.h" @interface StarView () @property (nonatomic, strong) UIImageView *imageView; // 图片
@property (nonatomic, strong) UIView *backgroundView; // 背景色View
@property (nonatomic, strong) UIView *animationView; // 做动画的View @property (nonatomic) CGFloat height;
@property (nonatomic) CGFloat width; @end @implementation StarView - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.layer.masksToBounds = YES;
self.height = frame.size.height;
self.width = frame.size.width; [self addSubview:self.backgroundView]; [self addSubview:self.animationView]; [self initImageView];
}
return self;
} - (void)initImageView {
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
self.imageView.image = [UIImage imageNamed:@"star"]; [self addSubview:self.imageView];
} - (void)percent:(CGFloat)percent animated:(BOOL)animated {
// 过滤percent
if (percent <= ) {
percent = ;
} else if (percent >= ) {
percent = ;
} if (animated == NO) {
CGFloat positionY = self.height * ( - percent);
_animationView.y = positionY;
} else {
CGFloat positionY = self.height * ( - percent);
[UIView animateWithDuration:(self.animationDuration <= ? 0.5 : self.animationDuration)
animations:^{
_animationView.y = positionY;
}];
}
} + (instancetype)createWithFrame:(CGRect)frame
backgroundViewColor:(UIColor *)bgColor
animationViewColor:(UIColor *)anColor {
StarView *star = [[StarView alloc] initWithFrame:frame];
star.backgroundViewColor = bgColor;
star.animationViewColor = anColor; return star;
} @synthesize backgroundView = _backgroundView;
- (UIView *)backgroundView {
if (_backgroundView == nil) {
_backgroundView = [[UIView alloc] initWithFrame:self.bounds];
} return _backgroundView;
} @synthesize animationView = _animationView;
- (UIView *)animationView {
if (_animationView == nil) {
_animationView = [[UIView alloc] initWithFrame:CGRectMake(, self.height, self.width, self.height)];
} return _animationView;
} @synthesize backgroundViewColor = _backgroundViewColor;
- (UIColor *)backgroundViewColor {
return _backgroundViewColor;
}
- (void)setBackgroundViewColor:(UIColor *)backgroundViewColor {
_backgroundViewColor = backgroundViewColor;
_backgroundView.backgroundColor = backgroundViewColor;
} @synthesize animationViewColor = _animationViewColor;
- (UIColor *)animationViewColor {
return _animationViewColor;
}
- (void)setAnimationViewColor:(UIColor *)animationViewColor {
_animationViewColor = animationViewColor;
_animationView.backgroundColor = animationViewColor;
}
@end

辅助文件 UIView+SetRect.h 与 UIView+SetRect.m

//
// UIView+SetRect.h
// TestPch
//
// Created by YouXianMing on 14-12-26.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (SetRect) // Frame
@property (nonatomic) CGPoint viewOrigin;
@property (nonatomic) CGSize viewSize; // Frame Origin
@property (nonatomic) CGFloat x;
@property (nonatomic) CGFloat y; // Frame Size
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height; // Frame Borders
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat right; // Center Point
#if !IS_IOS_DEVICE
@property (nonatomic) CGPoint center;
#endif
@property (nonatomic) CGFloat centerX;
@property (nonatomic) CGFloat centerY; // Middle Point
@property (nonatomic, readonly) CGPoint middlePoint;
@property (nonatomic, readonly) CGFloat middleX;
@property (nonatomic, readonly) CGFloat middleY;
@property (nonatomic, assign) CGFloat cornerRadius ;
@property (nonatomic ,assign) BOOL round ;
@end
//
// UIView+SetRect.m
// TestPch
//
// Created by YouXianMing on 14-12-26.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "UIView+SetRect.h" @implementation UIView (SetRect) #pragma mark Frame - (CGPoint)viewOrigin
{
return self.frame.origin;
} - (void)setViewOrigin:(CGPoint)newOrigin
{
CGRect newFrame = self.frame;
newFrame.origin = newOrigin;
self.frame = newFrame;
} - (CGSize)viewSize
{
return self.frame.size;
} - (void)setViewSize:(CGSize)newSize
{
CGRect newFrame = self.frame;
newFrame.size = newSize;
self.frame = newFrame;
} #pragma mark Frame Origin - (CGFloat)x
{
return self.frame.origin.x;
} - (void)setX:(CGFloat)newX
{
CGRect newFrame = self.frame;
newFrame.origin.x = newX;
self.frame = newFrame;
} - (CGFloat)y
{
return self.frame.origin.y;
} - (void)setY:(CGFloat)newY
{
CGRect newFrame = self.frame;
newFrame.origin.y = newY;
self.frame = newFrame;
} #pragma mark Frame Size - (CGFloat)height
{
return self.frame.size.height;
} - (void)setHeight:(CGFloat)newHeight
{
CGRect newFrame = self.frame;
newFrame.size.height = newHeight;
self.frame = newFrame;
} - (CGFloat)width
{
return self.frame.size.width;
} - (void)setWidth:(CGFloat)newWidth
{
CGRect newFrame = self.frame;
newFrame.size.width = newWidth;
self.frame = newFrame;
} #pragma mark Frame Borders - (CGFloat)left
{
return self.x;
} - (void)setLeft:(CGFloat)left
{
self.x = left;
} - (CGFloat)right
{
return self.frame.origin.x + self.frame.size.width;
} - (void)setRight:(CGFloat)right
{
self.x = right - self.width;
} - (CGFloat)top
{
return self.y;
} - (void)setTop:(CGFloat)top
{
self.y = top;
} - (CGFloat)bottom
{
return self.frame.origin.y + self.frame.size.height;
} - (void)setBottom:(CGFloat)bottom
{
self.y = bottom - self.height;
} #pragma mark Center Point #if !IS_IOS_DEVICE
- (CGPoint)center
{
return CGPointMake(self.left + self.middleX, self.top + self.middleY);
} - (void)setCenter:(CGPoint)newCenter
{
self.left = newCenter.x - self.middleX;
self.top = newCenter.y - self.middleY;
}
#endif - (CGFloat)centerX
{
return self.center.x;
} - (void)setCenterX:(CGFloat)newCenterX
{
self.center = CGPointMake(newCenterX, self.center.y);
} - (CGFloat)centerY
{
return self.center.y;
} - (void)setCenterY:(CGFloat)newCenterY
{
self.center = CGPointMake(self.center.x, newCenterY);
} #pragma mark Middle Point - (CGPoint)middlePoint
{
return CGPointMake(self.middleX, self.middleY);
} - (CGFloat)middleX
{
return self.width / ;
} - (CGFloat)middleY
{
return self.height / ;
} - (void)setCornerRadius:(CGFloat)cornerRadius
{
self.layer.masksToBounds = YES ;
self.layer.cornerRadius = cornerRadius ;
} - (void)setRound:(BOOL)round
{
[self setCornerRadius:self.height/];
} - (CGFloat)cornerRadius
{
return self.layer.cornerRadius ;
} - (BOOL)round
{
return NO ;
} @end

使用时候的源码:

//
// ViewController.m
// Star
//
// Created by XianMingYou on 15/3/13.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "ViewController.h"
#import "StarView.h" @interface ViewController () @property (nonatomic, strong) StarView *star;
@property (nonatomic, strong) NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.star = [StarView createWithFrame:CGRectMake(, , , )
backgroundViewColor:[[UIColor redColor] colorWithAlphaComponent:0.05f]
animationViewColor:[[UIColor redColor] colorWithAlphaComponent:0.5f]];
self.star.animationDuration = .f;
self.star.center = self.view.center;
[self.view addSubview:self.star]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5f
target:self
selector:@selector(timerEvent:)
userInfo:nil
repeats:YES];
} - (void)timerEvent:(id)sender {
CGFloat percent = arc4random() % / .f;
[self.star percent:percent animated:YES];
} @end

最新文章

  1. 前端制作动画的几种方式(css3,js)
  2. Chrome 开发工具之Console
  3. IOS-Foundation框架结构
  4. python中深复制与浅复制
  5. 运维工作中常用到的几个rsync同步命令
  6. ubuntu14_gtk 安装
  7. CodeViz产生函数调用图
  8. 基于NPOI开源框架写的ExcelHelper【转载】
  9. python高级编程之访问超类中的方法:super()
  10. Python练习2
  11. VBA 中窗体模式切换,一次设计2种表现
  12. java与数据库
  13. pandas数据的分组与分列
  14. HDU 1575(裸矩阵快速幂)
  15. js 画布与图片的相互转化(canvas与img)
  16. nested exception is org.apache.ibatis.reflection.ReflectionExceptio
  17. 使用嵌入式jetty实现文件服务器
  18. 【Hive学习之三】Hive 函数
  19. 【NOIP 2016】Day2 T3 愤怒的小鸟
  20. uva-10905-贪心

热门文章

  1. Golang gRPC 和 gRPC-gateway 结合使用
  2. 9-lvs-lvs集群-及keepalived健康检查
  3. logstash-jdbc-input与mysql数据库同步
  4. Java 国际化
  5. sparkthriftserver启动及调优
  6. mysql备份与恢复数据
  7. Ruby中Time的常用函数
  8. RabbitMQ上手记录–part 3-发送消息
  9. IOS应用图标尺寸
  10. GetHashCode方法学习