效果图

具体实现代码如下

ZCWScrollNumView.h文件

  1. #import <UIKit/UIKit.h>
  2.  
  3. typedef enum {
  4. ZCWScrollNumAnimationTypeNone,
  5. ZCWScrollNumAnimationTypeNormal,
  6. ZCWScrollNumAnimationTypeFromLast,
  7. ZCWScrollNumAnimationTypeRand,
  8. ZCWScrollNumAnimationTypeFast
  9. } ZCWScrollNumAnimationType;
  10.  
  11. @interface ZCWScrollDigitView : UIView {
  12. CGFloat _oneDigitHeight;
  13. }
  14.  
  15. @property (retain, nonatomic) UIView *backgroundView;
  16. @property (retain, nonatomic) UILabel *label;
  17. @property (readonly, nonatomic) NSUInteger digit;
  18. @property (retain, nonatomic) UIFont *digitFont;
  19.  
  20. - (void)setDigitAndCommit:(NSUInteger)aDigit;
  21. - (void)setDigitFromLast:(NSUInteger)aDigit;
  22. - (void)setDigit:(NSUInteger)aDigit from:(NSUInteger)last;
  23. - (void)setDigitFast:(NSUInteger)aDigit;
  24. - (void)setRandomScrollDigit:(NSUInteger)aDigit length:(NSUInteger)length;
  25.  
  26. - (void)commitChange;
  27.  
  28. - (void)didConfigFinish;
  29.  
  30. @end
  31.  
  32. @interface ZCWScrollNumView : UIView {
  33. NSMutableArray *_numberViews;
  34. }
  35.  
  36. @property (nonatomic) NSUInteger numberSize;
  37. @property (nonatomic) CGFloat splitSpaceWidth;
  38. @property (nonatomic) CGFloat topAndBottomPadding;
  39. @property (readonly, nonatomic) NSUInteger numberValue;
  40. @property (retain, nonatomic) UIView *backgroundView;
  41. @property (retain, nonatomic) UIView *digitBackgroundView;
  42. @property (retain, nonatomic) UIFont *digitFont;
  43. @property (readonly, nonatomic) NSArray *numberViews;
  44. @property (retain, nonatomic) UIColor *digitColor;
  45. @property (nonatomic) NSUInteger randomLength;
  46. - (void)setNumber:(NSUInteger)number withAnimationType:(ZCWScrollNumAnimationType)type animationTime:(NSTimeInterval)timeSpan;
  47.  
  48. - (void)didConfigFinish;
  49. @end

ZCWScrollNumView.m文件

  1. #import "ZCWScrollNumView.h"
  2.  
  3. #define kRandomLength 10
  4. #define kDefaultDigitFont [UIFont systemFontOfSize:14.0]
  5.  
  6. @implementation ZCWScrollDigitView
  7.  
  8. @synthesize backgroundView;
  9. @synthesize label;
  10. @synthesize digit;
  11. @synthesize digitFont;
  12. - (void)setDigitAndCommit:(NSUInteger)aDigit {
  13. self.label.text = [NSString stringWithFormat:@"%zd", aDigit];
  14. CGRect rect = self.label.frame;
  15. rect.origin.y = ;
  16. rect.size.height = _oneDigitHeight;
  17. self.label.numberOfLines = ;
  18. self.label.frame = rect;
  19. digit = aDigit;
  20. }
  21. - (void)setDigit:(NSUInteger)aDigit from:(NSUInteger)last{
  22. if (aDigit == last) {
  23. [self setDigitAndCommit:aDigit];
  24. return;
  25. }
  26. NSMutableString *str = [NSMutableString stringWithFormat:@"%zd", last];
  27. int count = ;
  28. if (aDigit > last) {
  29. for (int i = (int)last + ; i < aDigit + ; ++i) {
  30. ++count;
  31. [str appendFormat:@"\n%d", i];
  32. }
  33. } else {
  34. for (int i = (int)last + ; i < ; ++i) {
  35. ++count;
  36. [str appendFormat:@"\n%d", i];
  37. }
  38. for (int i = ; i < aDigit + ; ++i) {
  39. ++count;
  40. [str appendFormat:@"\n%d", i];
  41. }
  42. }
  43. self.label.text = str;
  44. self.label.numberOfLines = count;
  45. CGRect rect = self.label.frame;
  46. rect.origin.y = ;
  47. rect.size.height = _oneDigitHeight * count;
  48. self.label.frame = rect;
  49. digit = aDigit;
  50. }
  51. - (void)setDigitFromLast:(NSUInteger)aDigit {
  52. [self setDigit:aDigit from:self.digit];
  53.  
  54. }
  55.  
  56. - (void)setDigitFast:(NSUInteger)aDigit{
  57. self.label.text = [NSString stringWithFormat:@"%zd\n%zd", self.digit, aDigit];
  58. self.label.numberOfLines = ;
  59. CGRect rect = self.label.frame;
  60. rect.origin.y = ;
  61. rect.size.height = _oneDigitHeight * ;
  62. self.label.frame = rect;
  63. digit = aDigit;
  64. }
  65.  
  66. - (void)setRandomScrollDigit:(NSUInteger)aDigit length:(NSUInteger)length{
  67. NSMutableString *str = [NSMutableString stringWithFormat:@"%zd", self.digit];
  68. for (int i = ; i < length - ; ++i) {
  69. [str appendFormat:@"\n%d", rand() % ];
  70. }
  71. [str appendFormat:@"\n%zd", aDigit];
  72. self.label.text = str;
  73. self.label.numberOfLines = length;
  74. CGRect rect = self.label.frame;
  75. rect.origin.y = ;
  76. rect.size.height = _oneDigitHeight * length;
  77. self.label.frame = rect;
  78. digit = aDigit;
  79.  
  80. }
  81.  
  82. - (void)commitChange{
  83.  
  84. CGRect rect = self.label.frame;
  85. rect.origin.y = _oneDigitHeight - rect.size.height;
  86. self.label.frame = rect;
  87. }
  88.  
  89. - (void)didConfigFinish{
  90.  
  91. if (self.backgroundView == nil) {
  92. self.backgroundView = [[UIView alloc] init];
  93. self.backgroundView.backgroundColor = [UIColor grayColor];
  94. }
  95. CGRect backrect = {{, }, self.frame.size};
  96. self.backgroundView.frame = backrect;
  97. [self addSubview:self.backgroundView];
  98.  
  99. CGSize size= [@"" sizeWithFont:self.digitFont];
  100.  
  101. _oneDigitHeight = size.height;
  102.  
  103. CGRect rect = {{(self.frame.size.width - size.width) / , (self.frame.size.height - size.height) / }, size};
  104. UIView *view = [[UIView alloc] initWithFrame:rect];
  105. view.backgroundColor = [UIColor clearColor];
  106. view.clipsToBounds = YES;
  107. rect.origin.x = ;
  108. rect.origin.y = ;
  109. self.label = [[UILabel alloc] initWithFrame:rect];
  110. self.label.font = self.digitFont;
  111. self.label.backgroundColor = [UIColor clearColor];
  112. [view addSubview:self.label];
  113. [self addSubview:view];
  114. [self setDigitAndCommit:self.digit];
  115.  
  116. }
  117.  
  118. @end
  119.  
  120. @implementation ZCWScrollNumView
  121. @synthesize numberSize;
  122. @synthesize numberValue;
  123. @synthesize backgroundView;
  124. @synthesize digitBackgroundView;
  125. @synthesize digitFont;
  126. @synthesize numberViews = _numberViews;
  127. @synthesize splitSpaceWidth;
  128. @synthesize topAndBottomPadding;
  129. - (id)initWithFrame:(CGRect)frame
  130. {
  131. self = [super initWithFrame:frame];
  132. if (self) {
  133. // Initialization code
  134. [self initScrollNumView];
  135. }
  136. return self;
  137. }
  138.  
  139. - (id)initWithCoder:(NSCoder *)aDecoder {
  140. if (self = [super initWithCoder:aDecoder]) {
  141. [self initScrollNumView];
  142. }
  143. return self;
  144. }
  145.  
  146. - (void)initScrollNumView {
  147. self.numberSize = ;
  148. numberValue = ;
  149. self.splitSpaceWidth = 2.0;
  150. self.topAndBottomPadding = 2.0;
  151. self.digitFont = kDefaultDigitFont;
  152. self.randomLength = kRandomLength;
  153. }
  154. /*
  155. // Only override drawRect: if you perform custom drawing.
  156. // An empty implementation adversely affects performance during animation.
  157. - (void)drawRect:(CGRect)rect
  158. {
  159. // Drawing code
  160. }
  161. */
  162.  
  163. - (void)setNumber:(NSUInteger)number withAnimationType:(ZCWScrollNumAnimationType)type animationTime:(NSTimeInterval)timeSpan {
  164. for (int i = ; i < numberSize; ++i) {
  165. ZCWScrollDigitView *digitView = [_numberViews objectAtIndex:i];
  166. NSUInteger digit = [ZCWScrollNumView digitFromNum:number withIndex:i];
  167. if (digit != [self digitIndex:i] || type == ZCWScrollNumAnimationTypeRand)
  168. switch (type) {
  169. case ZCWScrollNumAnimationTypeNone:
  170. [digitView setDigit:digit from:digit];
  171. break;
  172.  
  173. case ZCWScrollNumAnimationTypeNormal:
  174. [digitView setDigit:digit from:];
  175. break;
  176. case ZCWScrollNumAnimationTypeFromLast:
  177. [digitView setDigitFromLast:digit];
  178. break;
  179.  
  180. case ZCWScrollNumAnimationTypeRand:
  181. [digitView setRandomScrollDigit:digit length:self.randomLength];
  182. break;
  183. case ZCWScrollNumAnimationTypeFast:
  184. [digitView setDigitFast:digit];
  185. default:
  186. break;
  187. }
  188. }
  189. [UIView beginAnimations:nil context:nil];
  190. [UIView setAnimationDuration:timeSpan];
  191.  
  192. for (ZCWScrollDigitView *digitView in _numberViews) {
  193. [digitView commitChange];
  194. }
  195. [UIView commitAnimations];
  196. numberValue = number;
  197. }
  198.  
  199. + (NSUInteger)digitFromNum:(NSUInteger)number withIndex:(NSUInteger)index {
  200. NSUInteger num = number;
  201. for (int i = ; i < index; ++i) {
  202. num /= ;
  203. }
  204.  
  205. return num % ;
  206. }
  207.  
  208. - (NSUInteger)digitIndex:(NSUInteger)index {
  209. return [ZCWScrollNumView digitFromNum:self.numberValue withIndex:index];
  210.  
  211. }
  212.  
  213. - (void)didConfigFinish {
  214. CGRect backRect = {{, }, self.frame.size};
  215. self.backgroundView.frame = backRect;
  216. [self addSubview:self.backgroundView];
  217. _numberViews = [[NSMutableArray alloc] initWithCapacity:self.numberSize];
  218. CGFloat allWidth = self.frame.size.width;
  219. CGFloat digitWidth = (allWidth - (self.numberSize + ) * splitSpaceWidth) / self.numberSize;
  220. NSData *digitBackgroundViewData = [NSKeyedArchiver archivedDataWithRootObject:self.digitBackgroundView];
  221. for (int i = ; i < numberSize; ++i) {
  222. CGRect rect = {{allWidth - (digitWidth + self.splitSpaceWidth) * (i + ), self.topAndBottomPadding}, {digitWidth, self.frame.size.height - self.topAndBottomPadding * }};
  223.  
  224. ZCWScrollDigitView *digitView = [[ZCWScrollDigitView alloc] initWithFrame:rect];
  225. digitView.backgroundView = [NSKeyedUnarchiver unarchiveObjectWithData:digitBackgroundViewData];
  226. digitView.digitFont = self.digitFont;
  227. [digitView didConfigFinish];
  228. [digitView setDigitAndCommit:[self digitIndex:i]];
  229. if (self.digitColor != nil) {
  230. digitView.label.textColor = self.digitColor;
  231. }
  232. [_numberViews addObject:digitView];
  233. [self addSubview:digitView];
  234. }
  235. }
  236. @end

控制端代码

  1. #import "TianJiCeSuanViewController.h"
  2. #import "ZCWScrollNumView.h"
  3. #import "HXSrollAnimalView.h"
  4.  
  5. #define kAllFullSuperviewMask UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
  6.  
  7. @interface TianJiCeSuanViewController ()
  8. @property (weak, nonatomic) IBOutlet UIImageView *BgView;
  9. @property (weak, nonatomic) IBOutlet HXSrollAnimalView *shengxiaoView;
  10. @property (weak, nonatomic) IBOutlet ZCWScrollNumView *weishuView;
  11.  
  12. @end
  13.  
  14. @implementation TianJiCeSuanViewController
  15.  
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18.  
  19. // 设置导航栏
  20. [self setNav];
  21.  
  22. [self setscrollNumer];
  23. [self setscrollAnimal];
  24.  
  25. [self.view insertSubview:self.BgView atIndex:];
  26. }
  27.  
  28. -(void)setscrollNumer{
  29.  
  30. CGRect tmp = self.weishuView.bounds;
  31. self.weishuView.numberSize =
  32. ;
  33. UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth: topCapHeight:];
  34. self.weishuView.backgroundView = [[UIImageView alloc] initWithImage:image];
  35. UIView *digitBackView = [[UIView alloc] initWithFrame:tmp];
  36. digitBackView.backgroundColor = [UIColor clearColor];
  37. digitBackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  38. digitBackView.autoresizesSubviews = YES;
  39. image = [[UIImage imageNamed:@"money_bg"] stretchableImageWithLeftCapWidth: topCapHeight:];
  40. UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];
  41. bgImageView.frame = tmp;
  42. bgImageView.autoresizingMask = kAllFullSuperviewMask;
  43. [digitBackView addSubview:bgImageView];
  44. image = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth: topCapHeight:];
  45. UIImageView *bgMaskImageView = [[UIImageView alloc] initWithImage:image];
  46. bgMaskImageView.autoresizingMask = kAllFullSuperviewMask;
  47. bgMaskImageView.frame = tmp;
  48. [digitBackView addSubview:bgMaskImageView];
  49.  
  50. self.weishuView.digitBackgroundView = digitBackView;
  51. self.weishuView.digitColor = [UIColor whiteColor];
  52. self.weishuView.digitFont = [UIFont systemFontOfSize:17.0];
  53. [self.weishuView didConfigFinish];
  54. }
  55. -(void)setscrollAnimal{
  56.  
  57. CGRect tmp = self.shengxiaoView.bounds;
  58. self.shengxiaoView.numberSize =
  59. ;
  60. UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth: topCapHeight:];
  61. self.shengxiaoView.backgroundView = [[UIImageView alloc] initWithImage:image];
  62. UIView *digitBackView = [[UIView alloc] initWithFrame:tmp];
  63. digitBackView.backgroundColor = [UIColor clearColor];
  64. digitBackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  65. digitBackView.autoresizesSubviews = YES;
  66. image = [[UIImage imageNamed:@"money_bg"] stretchableImageWithLeftCapWidth: topCapHeight:];
  67. UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];
  68. bgImageView.frame = tmp;
  69. bgImageView.autoresizingMask = kAllFullSuperviewMask;
  70. [digitBackView addSubview:bgImageView];
  71. image = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth: topCapHeight:];
  72. UIImageView *bgMaskImageView = [[UIImageView alloc] initWithImage:image];
  73. bgMaskImageView.autoresizingMask = kAllFullSuperviewMask;
  74. bgMaskImageView.frame = tmp;
  75. [digitBackView addSubview:bgMaskImageView];
  76.  
  77. self.shengxiaoView.digitBackgroundView = digitBackView;
  78. self.shengxiaoView.digitColor = [UIColor whiteColor];
  79. self.shengxiaoView.digitFont = [UIFont systemFontOfSize:17.0];
  80. [self.shengxiaoView didConfigFinish];
  81. }
  82.  
  83. -(void)setNav{
  84. // 设置导航栏的标题
  85. self.navigationItem.title = @"天机测算";
  86. // 设置字体
  87. [self.navigationController.navigationBar setTitleTextAttributes:
  88. @{NSFontAttributeName:[UIFont systemFontOfSize:],
  89. NSForegroundColorAttributeName:XMGRGBColor(, , )}];
  90. self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"ico_share" highImage:@"ico_share" target:self action:@selector(share)];
  91. }
  92.  
  93. -(void)share{
  94.  
  95. }
  96. - (IBAction)qiuShengXiao {
  97.  
  98. self.shengxiaoView.hidden = NO;
  99. [self.shengxiaoView setNumber:rand() withAnimationType:HXSScrollNumAnimationTypeRand animationTime:];
  100.  
  101. }
  102. - (IBAction)qiuWeiShu {
  103. self.weishuView.hidden = NO;
  104. [self.weishuView setNumber:rand() withAnimationType:ZCWScrollNumAnimationTypeRand animationTime:];
  105. }
  106. @end

iOS 数字滚动 类似于老 - 虎- 机的效果的更多相关文章

  1. 原生javascript实现老.虎机抽奖点名demo源码思路解析

    想着使用原生Javascript做一个随机点名的小应用, 也可以做抽奖使用. html简单化,人名单可以通过js生成并处理. 可以非常随意的添加修改人名字. 应用想带点特效,比如老.虎机转动的特效. ...

  2. IOS中(类似于进度条哪种效果)MBProgressHUD的使用

    1.显示HUD MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = ...

  3. Vue.js大屏数字滚动翻转效果

    ================================ 大屏数字滚动翻转效果来源于最近工作中element后台管理页面一张大屏的UI图,该UI图上有一个模块需要有数字往上翻动的效果,以下是最 ...

  4. 抽奖动画 - lao虎机抽奖

    本文介绍一个lao虎机抽奖动画的实现,lao虎机抽奖在各类商家营销活动中非常常见,这里主要介绍动画的实现过程,其他细节不做详细分析. ps:lao虎机是敏感词,博客园不允许出现,所有老用拼音. 1. ...

  5. 让数字变化炫酷起来,数字滚动Text组件[Unity]

    让数字滚动起来 上周我的策划又提了样需求,当玩家评分发生变动时,屏幕出现人物评分浮层UI,播放评分数字滚动动画.这类数字滚动需求非常常见,我就按一般思路,将startvalue与endvalue每隔一 ...

  6. iOS Sprite Kit教程之真机测试以及场景的添加与展示

    iOS Sprite Kit教程之真机测试以及场景的添加与展示 IOS实现真机测试 在进行真机测试之前,首先需要确保设备已经连在了Mac(或者Mac虚拟机)上,在第1.9.1小节开始,设备就一直连接在 ...

  7. [issue] [iOS 10] 升级后无法真机测试 Could not find Developer Disk Image

    说明:更新了手机的到了iOS 10.0.2.真机调试时候提示"Could not find Developer Disk Image" 并且之前就下载了Xcode8,但是没有安装X ...

  8. 如何让老Mac机支持USB安装Windows

    一些老Mac机的用户想装Windows,却发现自己的系统上的Boot Camp Assistant(以下简称BCA)没有USB安装Windows的选项. 下面以我的MacBook Pro (13-in ...

  9. iOS 开发之 Xcode6 创建真机调试证书

    http://jingyan.baidu.com/article/ff411625b8141312e48237a7.html 1.登录苹果开发者中心 2.登录后的界面如图所示,如果没有最上面的两个选项 ...

随机推荐

  1. [ASP.NET] 如果将缓存“滑动过期时间”设置为1秒会怎样?

    今天编写了一个采用ASP.NET Caching的组件,在为它编写Unit Test的过程中发现了一个有趣的问题,接下来我通过一个简单的实例说明这个问题.我们在一个控制台应用中编写了如下一段程序,这个 ...

  2. MySQL学习笔记四:字符集

    1.字符集就是字符和其编码的集合,查看数据库支持的字符集 show character set 2.查看服务端启动时默认的字符集 mysql> show variables like 'char ...

  3. 窥探Swift之函数与闭包的应用实例

    今天的博客算是比较基础的,还是那句话,基础这东西在什么时候都是最重要的.说到函数,只要是写过程序就肯定知道函数是怎么回事,今天就来讨论一下Swift中的函数的特性以及Swift中的闭包.今天的一些小实 ...

  4. 窥探Swift编程之在Playground上尽情的玩耍

    自从苹果公司发布Swift的时候,Xcode上又多了一样新的东西---"Playground".Playground就像操场一样,可以供我们在代码的世界里尽情的玩耍,在本篇博客中就 ...

  5. pixi.js教程中文版--基础篇

    前言 Pixi.js使用WebGL,是一个超快的HTML5 2D渲染引擎.作为一个Javascript的2D渲染器,Pixi.js的目标是提供一个快速的.轻量级而且是兼任所有设备的2D库.提供无缝 C ...

  6. xcode6 使用MJRefresh,Too many arguments to function call, expected 0, have *

    转载自:  http://blog.csdn.net/wsjshx/article/details/40743291 将XCode升级到6后,报Too many arguments to functi ...

  7. JavaScript 获取HTML中的CSS样式的属性以及值的的方法。

    <body> <div id="wow" style="font-size:10px; float:left"></div> ...

  8. Eclipse与Android源码中ProGuard工具的使用

    由于工作需要,这两天和同事在研究android下面的ProGuard工具的使用,通过查看android官网对该工具的介绍以及网络上其它相关资料,再加上自己的亲手实践,算是有了一个基本了解.下面将自己的 ...

  9. TypeError: invalid 'in' operand obj

    尝试在程序去访问远程的Web API,它在运行时,出现异常: TypeError: invalid 'in' operand obj 由于从服务器返回的数据是json.当我们需要得到这些数据时,还得需 ...

  10. 异步编程系列第05章 Await究竟做了什么?

    p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...