心形加载的view

效果:

素材图片:

源码:

StarView.h 与 StarView.m

  1. //
  2. // StarView.h
  3. // Star
  4. //
  5. // Created by XianMingYou on 15/3/13.
  6. // Copyright (c) 2015年 XianMingYou. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface StarView : UIView
  12.  
  13. @property (nonatomic, strong) UIColor *backgroundViewColor;
  14. @property (nonatomic, strong) UIColor *animationViewColor;
  15. @property (nonatomic, assign) NSTimeInterval animationDuration;
  16.  
  17. + (instancetype)createWithFrame:(CGRect)frame
  18. backgroundViewColor:(UIColor *)bgColor
  19. animationViewColor:(UIColor *)anColor;
  20.  
  21. - (void)percent:(CGFloat)percent animated:(BOOL)animated;
  22.  
  23. @end
  1. //
  2. // StarView.m
  3. // Star
  4. //
  5. // Created by XianMingYou on 15/3/13.
  6. // Copyright (c) 2015年 XianMingYou. All rights reserved.
  7. //
  8.  
  9. #import "StarView.h"
  10. #import "UIView+SetRect.h"
  11.  
  12. @interface StarView ()
  13.  
  14. @property (nonatomic, strong) UIImageView *imageView; // 图片
  15. @property (nonatomic, strong) UIView *backgroundView; // 背景色View
  16. @property (nonatomic, strong) UIView *animationView; // 做动画的View
  17.  
  18. @property (nonatomic) CGFloat height;
  19. @property (nonatomic) CGFloat width;
  20.  
  21. @end
  22.  
  23. @implementation StarView
  24.  
  25. - (instancetype)initWithFrame:(CGRect)frame {
  26. self = [super initWithFrame:frame];
  27. if (self) {
  28. self.layer.masksToBounds = YES;
  29. self.height = frame.size.height;
  30. self.width = frame.size.width;
  31.  
  32. [self addSubview:self.backgroundView];
  33.  
  34. [self addSubview:self.animationView];
  35.  
  36. [self initImageView];
  37. }
  38. return self;
  39. }
  40.  
  41. - (void)initImageView {
  42. self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  43. self.imageView.image = [UIImage imageNamed:@"star"];
  44.  
  45. [self addSubview:self.imageView];
  46. }
  47.  
  48. - (void)percent:(CGFloat)percent animated:(BOOL)animated {
  49. // 过滤percent
  50. if (percent <= ) {
  51. percent = ;
  52. } else if (percent >= ) {
  53. percent = ;
  54. }
  55.  
  56. if (animated == NO) {
  57. CGFloat positionY = self.height * ( - percent);
  58. _animationView.y = positionY;
  59. } else {
  60. CGFloat positionY = self.height * ( - percent);
  61. [UIView animateWithDuration:(self.animationDuration <= ? 0.5 : self.animationDuration)
  62. animations:^{
  63. _animationView.y = positionY;
  64. }];
  65. }
  66. }
  67.  
  68. + (instancetype)createWithFrame:(CGRect)frame
  69. backgroundViewColor:(UIColor *)bgColor
  70. animationViewColor:(UIColor *)anColor {
  71. StarView *star = [[StarView alloc] initWithFrame:frame];
  72. star.backgroundViewColor = bgColor;
  73. star.animationViewColor = anColor;
  74.  
  75. return star;
  76. }
  77.  
  78. @synthesize backgroundView = _backgroundView;
  79. - (UIView *)backgroundView {
  80. if (_backgroundView == nil) {
  81. _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
  82. }
  83.  
  84. return _backgroundView;
  85. }
  86.  
  87. @synthesize animationView = _animationView;
  88. - (UIView *)animationView {
  89. if (_animationView == nil) {
  90. _animationView = [[UIView alloc] initWithFrame:CGRectMake(, self.height, self.width, self.height)];
  91. }
  92.  
  93. return _animationView;
  94. }
  95.  
  96. @synthesize backgroundViewColor = _backgroundViewColor;
  97. - (UIColor *)backgroundViewColor {
  98. return _backgroundViewColor;
  99. }
  100. - (void)setBackgroundViewColor:(UIColor *)backgroundViewColor {
  101. _backgroundViewColor = backgroundViewColor;
  102. _backgroundView.backgroundColor = backgroundViewColor;
  103. }
  104.  
  105. @synthesize animationViewColor = _animationViewColor;
  106. - (UIColor *)animationViewColor {
  107. return _animationViewColor;
  108. }
  109. - (void)setAnimationViewColor:(UIColor *)animationViewColor {
  110. _animationViewColor = animationViewColor;
  111. _animationView.backgroundColor = animationViewColor;
  112. }
  113. @end

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

  1. //
  2. // UIView+SetRect.h
  3. // TestPch
  4. //
  5. // Created by YouXianMing on 14-12-26.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface UIView (SetRect)
  12.  
  13. // Frame
  14. @property (nonatomic) CGPoint viewOrigin;
  15. @property (nonatomic) CGSize viewSize;
  16.  
  17. // Frame Origin
  18. @property (nonatomic) CGFloat x;
  19. @property (nonatomic) CGFloat y;
  20.  
  21. // Frame Size
  22. @property (nonatomic) CGFloat width;
  23. @property (nonatomic) CGFloat height;
  24.  
  25. // Frame Borders
  26. @property (nonatomic) CGFloat top;
  27. @property (nonatomic) CGFloat left;
  28. @property (nonatomic) CGFloat bottom;
  29. @property (nonatomic) CGFloat right;
  30.  
  31. // Center Point
  32. #if !IS_IOS_DEVICE
  33. @property (nonatomic) CGPoint center;
  34. #endif
  35. @property (nonatomic) CGFloat centerX;
  36. @property (nonatomic) CGFloat centerY;
  37.  
  38. // Middle Point
  39. @property (nonatomic, readonly) CGPoint middlePoint;
  40. @property (nonatomic, readonly) CGFloat middleX;
  41. @property (nonatomic, readonly) CGFloat middleY;
  42. @property (nonatomic, assign) CGFloat cornerRadius ;
  43. @property (nonatomic ,assign) BOOL round ;
  44. @end
  1. //
  2. // UIView+SetRect.m
  3. // TestPch
  4. //
  5. // Created by YouXianMing on 14-12-26.
  6. // Copyright (c) 2014年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "UIView+SetRect.h"
  10.  
  11. @implementation UIView (SetRect)
  12.  
  13. #pragma mark Frame
  14.  
  15. - (CGPoint)viewOrigin
  16. {
  17. return self.frame.origin;
  18. }
  19.  
  20. - (void)setViewOrigin:(CGPoint)newOrigin
  21. {
  22. CGRect newFrame = self.frame;
  23. newFrame.origin = newOrigin;
  24. self.frame = newFrame;
  25. }
  26.  
  27. - (CGSize)viewSize
  28. {
  29. return self.frame.size;
  30. }
  31.  
  32. - (void)setViewSize:(CGSize)newSize
  33. {
  34. CGRect newFrame = self.frame;
  35. newFrame.size = newSize;
  36. self.frame = newFrame;
  37. }
  38.  
  39. #pragma mark Frame Origin
  40.  
  41. - (CGFloat)x
  42. {
  43. return self.frame.origin.x;
  44. }
  45.  
  46. - (void)setX:(CGFloat)newX
  47. {
  48. CGRect newFrame = self.frame;
  49. newFrame.origin.x = newX;
  50. self.frame = newFrame;
  51. }
  52.  
  53. - (CGFloat)y
  54. {
  55. return self.frame.origin.y;
  56. }
  57.  
  58. - (void)setY:(CGFloat)newY
  59. {
  60. CGRect newFrame = self.frame;
  61. newFrame.origin.y = newY;
  62. self.frame = newFrame;
  63. }
  64.  
  65. #pragma mark Frame Size
  66.  
  67. - (CGFloat)height
  68. {
  69. return self.frame.size.height;
  70. }
  71.  
  72. - (void)setHeight:(CGFloat)newHeight
  73. {
  74. CGRect newFrame = self.frame;
  75. newFrame.size.height = newHeight;
  76. self.frame = newFrame;
  77. }
  78.  
  79. - (CGFloat)width
  80. {
  81. return self.frame.size.width;
  82. }
  83.  
  84. - (void)setWidth:(CGFloat)newWidth
  85. {
  86. CGRect newFrame = self.frame;
  87. newFrame.size.width = newWidth;
  88. self.frame = newFrame;
  89. }
  90.  
  91. #pragma mark Frame Borders
  92.  
  93. - (CGFloat)left
  94. {
  95. return self.x;
  96. }
  97.  
  98. - (void)setLeft:(CGFloat)left
  99. {
  100. self.x = left;
  101. }
  102.  
  103. - (CGFloat)right
  104. {
  105. return self.frame.origin.x + self.frame.size.width;
  106. }
  107.  
  108. - (void)setRight:(CGFloat)right
  109. {
  110. self.x = right - self.width;
  111. }
  112.  
  113. - (CGFloat)top
  114. {
  115. return self.y;
  116. }
  117.  
  118. - (void)setTop:(CGFloat)top
  119. {
  120. self.y = top;
  121. }
  122.  
  123. - (CGFloat)bottom
  124. {
  125. return self.frame.origin.y + self.frame.size.height;
  126. }
  127.  
  128. - (void)setBottom:(CGFloat)bottom
  129. {
  130. self.y = bottom - self.height;
  131. }
  132.  
  133. #pragma mark Center Point
  134.  
  135. #if !IS_IOS_DEVICE
  136. - (CGPoint)center
  137. {
  138. return CGPointMake(self.left + self.middleX, self.top + self.middleY);
  139. }
  140.  
  141. - (void)setCenter:(CGPoint)newCenter
  142. {
  143. self.left = newCenter.x - self.middleX;
  144. self.top = newCenter.y - self.middleY;
  145. }
  146. #endif
  147.  
  148. - (CGFloat)centerX
  149. {
  150. return self.center.x;
  151. }
  152.  
  153. - (void)setCenterX:(CGFloat)newCenterX
  154. {
  155. self.center = CGPointMake(newCenterX, self.center.y);
  156. }
  157.  
  158. - (CGFloat)centerY
  159. {
  160. return self.center.y;
  161. }
  162.  
  163. - (void)setCenterY:(CGFloat)newCenterY
  164. {
  165. self.center = CGPointMake(self.center.x, newCenterY);
  166. }
  167.  
  168. #pragma mark Middle Point
  169.  
  170. - (CGPoint)middlePoint
  171. {
  172. return CGPointMake(self.middleX, self.middleY);
  173. }
  174.  
  175. - (CGFloat)middleX
  176. {
  177. return self.width / ;
  178. }
  179.  
  180. - (CGFloat)middleY
  181. {
  182. return self.height / ;
  183. }
  184.  
  185. - (void)setCornerRadius:(CGFloat)cornerRadius
  186. {
  187. self.layer.masksToBounds = YES ;
  188. self.layer.cornerRadius = cornerRadius ;
  189. }
  190.  
  191. - (void)setRound:(BOOL)round
  192. {
  193. [self setCornerRadius:self.height/];
  194. }
  195.  
  196. - (CGFloat)cornerRadius
  197. {
  198. return self.layer.cornerRadius ;
  199. }
  200.  
  201. - (BOOL)round
  202. {
  203. return NO ;
  204. }
  205.  
  206. @end

使用时候的源码:

  1. //
  2. // ViewController.m
  3. // Star
  4. //
  5. // Created by XianMingYou on 15/3/13.
  6. // Copyright (c) 2015年 XianMingYou. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "StarView.h"
  11.  
  12. @interface ViewController ()
  13.  
  14. @property (nonatomic, strong) StarView *star;
  15. @property (nonatomic, strong) NSTimer *timer;
  16.  
  17. @end
  18.  
  19. @implementation ViewController
  20.  
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23.  
  24. self.star = [StarView createWithFrame:CGRectMake(, , , )
  25. backgroundViewColor:[[UIColor redColor] colorWithAlphaComponent:0.05f]
  26. animationViewColor:[[UIColor redColor] colorWithAlphaComponent:0.5f]];
  27. self.star.animationDuration = .f;
  28. self.star.center = self.view.center;
  29. [self.view addSubview:self.star];
  30.  
  31. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5f
  32. target:self
  33. selector:@selector(timerEvent:)
  34. userInfo:nil
  35. repeats:YES];
  36. }
  37.  
  38. - (void)timerEvent:(id)sender {
  39. CGFloat percent = arc4random() % / .f;
  40. [self.star percent:percent animated:YES];
  41. }
  42.  
  43. @end

[控件] 心形加载的view的更多相关文章

  1. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法

    博客分类: jquery-easyui jQueryAjax框架HTML  现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...

  2. Winform DevExpress控件库(二) 使用SplashScreenManager控件定制程序加载页面

    SplashScreenManager控件:主要作用是显示在进行耗时操作时的等待界面: 位于 工具箱 -> Navigation & Layout(导航栏与布局类控件) 目录下: 在工具 ...

  3. 使用SplashScreenManager控件定制程序加载页面

    需要devexpress版本在12.0及以上才支持 https://www.cnblogs.com/wuhuacong/p/6112461.html 在DevExpress程序中使用SplashScr ...

  4. zTree 树形控件 ajax动态加载数据

    很久没搞过树形控件了 , 再次接触看官网文档有点没懂,于是在网上找了个代码copy上,但数据是写死的,就想这在用ajax异步取出数据替换,下面是js代码 <SCRIPT type="t ...

  5. 扩展easyUI tab控件,添加加载遮罩效果

    项目里要用HighChart显示图表,如果返回的数量量太多,生成图表是一个很耗时的过程.tab控件又没有显示遮罩的设置(至少本菜是没有找到), Google了一下,根据另一个兄台写的方法,拿来改造了一 ...

  6. Webbrowser控件判断网页加载完毕的简单方法 (转)

    摘自:http://blog.csdn.net/cometnet/article/details/5261192 一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,W ...

  7. C#自定义控件、用户控件、动态加载菜单按钮

    一.效果图,动态加载5个菜单按钮: 二.实现方法 1.创建用户控件 2.在用户控件拖入toolStrip 3.进入用户控件的Lood事件,这里自动添加5个选  ToolStripMenuItem,后期 ...

  8. asp.net读取用户控件,自定义加载用户控件

    1.自定义加载用户控件 ceshi.aspx页面 <html> <body> <div id="divControls" runat="se ...

  9. 02、获取 WebView 控件中,加载的 HTML 网页内容

    在开发 app 的时候,WebView 是经常使用的控件.而且有时需要向 WebView 中的 html 内容 注入额外的 js 进行操作.这里记录一下在当前 WebView 控件中,获取 html ...

随机推荐

  1. elasticsearch 导入基础数据并索引之 geo_shape

    我们看到的图形, 实际是由点来完成的, 有2种类型的格子模型可用于地理星座, 默认使用的是geoHash, 还有一种4叉树(quad trees), 也可用于 判断形状与索引的形状关系 1), int ...

  2. 【LeetCode题解】206_反转链表(Reverse-Linked-List)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 git ...

  3. Spring配置Quartz任务调度、及 ThreadPool 线程池

    ONE.除了引入 Spring 相关的 jar 包,还要引入 Quartz 的 jar 包 <dependency> <groupId>org.springframework& ...

  4. Web 后端--PHP 与数据库的交互

    网页要处理数据,数据置于数据库之中.今天看了书,不能让知识遗忘,遂及时记下. 用 PHP  操作 MySQL ,实现数据的交换,还要多练练.... PS: 以下 mysql 字段与mysqli 字段皆 ...

  5. JS中的编码,解码类型及说明

    使用ajax向后台提交的时候 由于参数中含有#  默认会被截断 只保留#之前的字符  json格式的字符串则不会被请求到后台的action 可以使用encodeURIComponent在前台进行编码, ...

  6. CLR 中 线程的 ThreadState 解释

    ThreadState   Aborted 线程已停止 AbortedRequested 线程的 Thread.Abort() 方法已被调用,但线程还未停止. Background 线程在后台执行,与 ...

  7. webfrom后台

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  8. 【JavaScript 从零开始】 原始值和对象引用、类型转换

    JavaScript 中的原始值(undefined.null . 布尔值.数值和字符串)于对象(包括数组和函数)有着根本区别. 原始值是不可更改的:任何方法都无法改变(或“突变”)一个原始值. 对于 ...

  9. C# 进程优先级和线程优先级的方法

    C# 设置进程优先级的方法 this.process1= Process.GetCurrentProcess(); process1.PriorityClass = ProcessPriorityCl ...

  10. eclipse启动项目报错:java.lang.ClassNotFoundException: ContextLoaderListener

    eclipse 启动项目报错:找不到 Spring 监听器类 org.springframework.web.context.ContextLoaderListener 严重: Error confi ...