iOS 数字滚动 类似于老 - 虎- 机的效果
效果图
具体实现代码如下
ZCWScrollNumView.h文件
- #import <UIKit/UIKit.h>
- typedef enum {
- ZCWScrollNumAnimationTypeNone,
- ZCWScrollNumAnimationTypeNormal,
- ZCWScrollNumAnimationTypeFromLast,
- ZCWScrollNumAnimationTypeRand,
- ZCWScrollNumAnimationTypeFast
- } ZCWScrollNumAnimationType;
- @interface ZCWScrollDigitView : UIView {
- CGFloat _oneDigitHeight;
- }
- @property (retain, nonatomic) UIView *backgroundView;
- @property (retain, nonatomic) UILabel *label;
- @property (readonly, nonatomic) NSUInteger digit;
- @property (retain, nonatomic) UIFont *digitFont;
- - (void)setDigitAndCommit:(NSUInteger)aDigit;
- - (void)setDigitFromLast:(NSUInteger)aDigit;
- - (void)setDigit:(NSUInteger)aDigit from:(NSUInteger)last;
- - (void)setDigitFast:(NSUInteger)aDigit;
- - (void)setRandomScrollDigit:(NSUInteger)aDigit length:(NSUInteger)length;
- - (void)commitChange;
- - (void)didConfigFinish;
- @end
- @interface ZCWScrollNumView : UIView {
- NSMutableArray *_numberViews;
- }
- @property (nonatomic) NSUInteger numberSize;
- @property (nonatomic) CGFloat splitSpaceWidth;
- @property (nonatomic) CGFloat topAndBottomPadding;
- @property (readonly, nonatomic) NSUInteger numberValue;
- @property (retain, nonatomic) UIView *backgroundView;
- @property (retain, nonatomic) UIView *digitBackgroundView;
- @property (retain, nonatomic) UIFont *digitFont;
- @property (readonly, nonatomic) NSArray *numberViews;
- @property (retain, nonatomic) UIColor *digitColor;
- @property (nonatomic) NSUInteger randomLength;
- - (void)setNumber:(NSUInteger)number withAnimationType:(ZCWScrollNumAnimationType)type animationTime:(NSTimeInterval)timeSpan;
- - (void)didConfigFinish;
- @end
ZCWScrollNumView.m文件
- #import "ZCWScrollNumView.h"
- #define kRandomLength 10
- #define kDefaultDigitFont [UIFont systemFontOfSize:14.0]
- @implementation ZCWScrollDigitView
- @synthesize backgroundView;
- @synthesize label;
- @synthesize digit;
- @synthesize digitFont;
- - (void)setDigitAndCommit:(NSUInteger)aDigit {
- self.label.text = [NSString stringWithFormat:@"%zd", aDigit];
- CGRect rect = self.label.frame;
- rect.origin.y = ;
- rect.size.height = _oneDigitHeight;
- self.label.numberOfLines = ;
- self.label.frame = rect;
- digit = aDigit;
- }
- - (void)setDigit:(NSUInteger)aDigit from:(NSUInteger)last{
- if (aDigit == last) {
- [self setDigitAndCommit:aDigit];
- return;
- }
- NSMutableString *str = [NSMutableString stringWithFormat:@"%zd", last];
- int count = ;
- if (aDigit > last) {
- for (int i = (int)last + ; i < aDigit + ; ++i) {
- ++count;
- [str appendFormat:@"\n%d", i];
- }
- } else {
- for (int i = (int)last + ; i < ; ++i) {
- ++count;
- [str appendFormat:@"\n%d", i];
- }
- for (int i = ; i < aDigit + ; ++i) {
- ++count;
- [str appendFormat:@"\n%d", i];
- }
- }
- self.label.text = str;
- self.label.numberOfLines = count;
- CGRect rect = self.label.frame;
- rect.origin.y = ;
- rect.size.height = _oneDigitHeight * count;
- self.label.frame = rect;
- digit = aDigit;
- }
- - (void)setDigitFromLast:(NSUInteger)aDigit {
- [self setDigit:aDigit from:self.digit];
- }
- - (void)setDigitFast:(NSUInteger)aDigit{
- self.label.text = [NSString stringWithFormat:@"%zd\n%zd", self.digit, aDigit];
- self.label.numberOfLines = ;
- CGRect rect = self.label.frame;
- rect.origin.y = ;
- rect.size.height = _oneDigitHeight * ;
- self.label.frame = rect;
- digit = aDigit;
- }
- - (void)setRandomScrollDigit:(NSUInteger)aDigit length:(NSUInteger)length{
- NSMutableString *str = [NSMutableString stringWithFormat:@"%zd", self.digit];
- for (int i = ; i < length - ; ++i) {
- [str appendFormat:@"\n%d", rand() % ];
- }
- [str appendFormat:@"\n%zd", aDigit];
- self.label.text = str;
- self.label.numberOfLines = length;
- CGRect rect = self.label.frame;
- rect.origin.y = ;
- rect.size.height = _oneDigitHeight * length;
- self.label.frame = rect;
- digit = aDigit;
- }
- - (void)commitChange{
- CGRect rect = self.label.frame;
- rect.origin.y = _oneDigitHeight - rect.size.height;
- self.label.frame = rect;
- }
- - (void)didConfigFinish{
- if (self.backgroundView == nil) {
- self.backgroundView = [[UIView alloc] init];
- self.backgroundView.backgroundColor = [UIColor grayColor];
- }
- CGRect backrect = {{, }, self.frame.size};
- self.backgroundView.frame = backrect;
- [self addSubview:self.backgroundView];
- CGSize size= [@"" sizeWithFont:self.digitFont];
- _oneDigitHeight = size.height;
- CGRect rect = {{(self.frame.size.width - size.width) / , (self.frame.size.height - size.height) / }, size};
- UIView *view = [[UIView alloc] initWithFrame:rect];
- view.backgroundColor = [UIColor clearColor];
- view.clipsToBounds = YES;
- rect.origin.x = ;
- rect.origin.y = ;
- self.label = [[UILabel alloc] initWithFrame:rect];
- self.label.font = self.digitFont;
- self.label.backgroundColor = [UIColor clearColor];
- [view addSubview:self.label];
- [self addSubview:view];
- [self setDigitAndCommit:self.digit];
- }
- @end
- @implementation ZCWScrollNumView
- @synthesize numberSize;
- @synthesize numberValue;
- @synthesize backgroundView;
- @synthesize digitBackgroundView;
- @synthesize digitFont;
- @synthesize numberViews = _numberViews;
- @synthesize splitSpaceWidth;
- @synthesize topAndBottomPadding;
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- [self initScrollNumView];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- [self initScrollNumView];
- }
- return self;
- }
- - (void)initScrollNumView {
- self.numberSize = ;
- numberValue = ;
- self.splitSpaceWidth = 2.0;
- self.topAndBottomPadding = 2.0;
- self.digitFont = kDefaultDigitFont;
- self.randomLength = kRandomLength;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- - (void)setNumber:(NSUInteger)number withAnimationType:(ZCWScrollNumAnimationType)type animationTime:(NSTimeInterval)timeSpan {
- for (int i = ; i < numberSize; ++i) {
- ZCWScrollDigitView *digitView = [_numberViews objectAtIndex:i];
- NSUInteger digit = [ZCWScrollNumView digitFromNum:number withIndex:i];
- if (digit != [self digitIndex:i] || type == ZCWScrollNumAnimationTypeRand)
- switch (type) {
- case ZCWScrollNumAnimationTypeNone:
- [digitView setDigit:digit from:digit];
- break;
- case ZCWScrollNumAnimationTypeNormal:
- [digitView setDigit:digit from:];
- break;
- case ZCWScrollNumAnimationTypeFromLast:
- [digitView setDigitFromLast:digit];
- break;
- case ZCWScrollNumAnimationTypeRand:
- [digitView setRandomScrollDigit:digit length:self.randomLength];
- break;
- case ZCWScrollNumAnimationTypeFast:
- [digitView setDigitFast:digit];
- default:
- break;
- }
- }
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:timeSpan];
- for (ZCWScrollDigitView *digitView in _numberViews) {
- [digitView commitChange];
- }
- [UIView commitAnimations];
- numberValue = number;
- }
- + (NSUInteger)digitFromNum:(NSUInteger)number withIndex:(NSUInteger)index {
- NSUInteger num = number;
- for (int i = ; i < index; ++i) {
- num /= ;
- }
- return num % ;
- }
- - (NSUInteger)digitIndex:(NSUInteger)index {
- return [ZCWScrollNumView digitFromNum:self.numberValue withIndex:index];
- }
- - (void)didConfigFinish {
- CGRect backRect = {{, }, self.frame.size};
- self.backgroundView.frame = backRect;
- [self addSubview:self.backgroundView];
- _numberViews = [[NSMutableArray alloc] initWithCapacity:self.numberSize];
- CGFloat allWidth = self.frame.size.width;
- CGFloat digitWidth = (allWidth - (self.numberSize + ) * splitSpaceWidth) / self.numberSize;
- NSData *digitBackgroundViewData = [NSKeyedArchiver archivedDataWithRootObject:self.digitBackgroundView];
- for (int i = ; i < numberSize; ++i) {
- CGRect rect = {{allWidth - (digitWidth + self.splitSpaceWidth) * (i + ), self.topAndBottomPadding}, {digitWidth, self.frame.size.height - self.topAndBottomPadding * }};
- ZCWScrollDigitView *digitView = [[ZCWScrollDigitView alloc] initWithFrame:rect];
- digitView.backgroundView = [NSKeyedUnarchiver unarchiveObjectWithData:digitBackgroundViewData];
- digitView.digitFont = self.digitFont;
- [digitView didConfigFinish];
- [digitView setDigitAndCommit:[self digitIndex:i]];
- if (self.digitColor != nil) {
- digitView.label.textColor = self.digitColor;
- }
- [_numberViews addObject:digitView];
- [self addSubview:digitView];
- }
- }
- @end
控制端代码
- #import "TianJiCeSuanViewController.h"
- #import "ZCWScrollNumView.h"
- #import "HXSrollAnimalView.h"
- #define kAllFullSuperviewMask UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
- @interface TianJiCeSuanViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *BgView;
- @property (weak, nonatomic) IBOutlet HXSrollAnimalView *shengxiaoView;
- @property (weak, nonatomic) IBOutlet ZCWScrollNumView *weishuView;
- @end
- @implementation TianJiCeSuanViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 设置导航栏
- [self setNav];
- [self setscrollNumer];
- [self setscrollAnimal];
- [self.view insertSubview:self.BgView atIndex:];
- }
- -(void)setscrollNumer{
- CGRect tmp = self.weishuView.bounds;
- self.weishuView.numberSize =
- ;
- UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth: topCapHeight:];
- self.weishuView.backgroundView = [[UIImageView alloc] initWithImage:image];
- UIView *digitBackView = [[UIView alloc] initWithFrame:tmp];
- digitBackView.backgroundColor = [UIColor clearColor];
- digitBackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- digitBackView.autoresizesSubviews = YES;
- image = [[UIImage imageNamed:@"money_bg"] stretchableImageWithLeftCapWidth: topCapHeight:];
- UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];
- bgImageView.frame = tmp;
- bgImageView.autoresizingMask = kAllFullSuperviewMask;
- [digitBackView addSubview:bgImageView];
- image = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth: topCapHeight:];
- UIImageView *bgMaskImageView = [[UIImageView alloc] initWithImage:image];
- bgMaskImageView.autoresizingMask = kAllFullSuperviewMask;
- bgMaskImageView.frame = tmp;
- [digitBackView addSubview:bgMaskImageView];
- self.weishuView.digitBackgroundView = digitBackView;
- self.weishuView.digitColor = [UIColor whiteColor];
- self.weishuView.digitFont = [UIFont systemFontOfSize:17.0];
- [self.weishuView didConfigFinish];
- }
- -(void)setscrollAnimal{
- CGRect tmp = self.shengxiaoView.bounds;
- self.shengxiaoView.numberSize =
- ;
- UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth: topCapHeight:];
- self.shengxiaoView.backgroundView = [[UIImageView alloc] initWithImage:image];
- UIView *digitBackView = [[UIView alloc] initWithFrame:tmp];
- digitBackView.backgroundColor = [UIColor clearColor];
- digitBackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- digitBackView.autoresizesSubviews = YES;
- image = [[UIImage imageNamed:@"money_bg"] stretchableImageWithLeftCapWidth: topCapHeight:];
- UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];
- bgImageView.frame = tmp;
- bgImageView.autoresizingMask = kAllFullSuperviewMask;
- [digitBackView addSubview:bgImageView];
- image = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth: topCapHeight:];
- UIImageView *bgMaskImageView = [[UIImageView alloc] initWithImage:image];
- bgMaskImageView.autoresizingMask = kAllFullSuperviewMask;
- bgMaskImageView.frame = tmp;
- [digitBackView addSubview:bgMaskImageView];
- self.shengxiaoView.digitBackgroundView = digitBackView;
- self.shengxiaoView.digitColor = [UIColor whiteColor];
- self.shengxiaoView.digitFont = [UIFont systemFontOfSize:17.0];
- [self.shengxiaoView didConfigFinish];
- }
- -(void)setNav{
- // 设置导航栏的标题
- self.navigationItem.title = @"天机测算";
- // 设置字体
- [self.navigationController.navigationBar setTitleTextAttributes:
- @{NSFontAttributeName:[UIFont systemFontOfSize:],
- NSForegroundColorAttributeName:XMGRGBColor(, , )}];
- self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"ico_share" highImage:@"ico_share" target:self action:@selector(share)];
- }
- -(void)share{
- }
- - (IBAction)qiuShengXiao {
- self.shengxiaoView.hidden = NO;
- [self.shengxiaoView setNumber:rand() withAnimationType:HXSScrollNumAnimationTypeRand animationTime:];
- }
- - (IBAction)qiuWeiShu {
- self.weishuView.hidden = NO;
- [self.weishuView setNumber:rand() withAnimationType:ZCWScrollNumAnimationTypeRand animationTime:];
- }
- @end
iOS 数字滚动 类似于老 - 虎- 机的效果的更多相关文章
- 原生javascript实现老.虎机抽奖点名demo源码思路解析
想着使用原生Javascript做一个随机点名的小应用, 也可以做抽奖使用. html简单化,人名单可以通过js生成并处理. 可以非常随意的添加修改人名字. 应用想带点特效,比如老.虎机转动的特效. ...
- IOS中(类似于进度条哪种效果)MBProgressHUD的使用
1.显示HUD MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = ...
- Vue.js大屏数字滚动翻转效果
================================ 大屏数字滚动翻转效果来源于最近工作中element后台管理页面一张大屏的UI图,该UI图上有一个模块需要有数字往上翻动的效果,以下是最 ...
- 抽奖动画 - lao虎机抽奖
本文介绍一个lao虎机抽奖动画的实现,lao虎机抽奖在各类商家营销活动中非常常见,这里主要介绍动画的实现过程,其他细节不做详细分析. ps:lao虎机是敏感词,博客园不允许出现,所有老用拼音. 1. ...
- 让数字变化炫酷起来,数字滚动Text组件[Unity]
让数字滚动起来 上周我的策划又提了样需求,当玩家评分发生变动时,屏幕出现人物评分浮层UI,播放评分数字滚动动画.这类数字滚动需求非常常见,我就按一般思路,将startvalue与endvalue每隔一 ...
- iOS Sprite Kit教程之真机测试以及场景的添加与展示
iOS Sprite Kit教程之真机测试以及场景的添加与展示 IOS实现真机测试 在进行真机测试之前,首先需要确保设备已经连在了Mac(或者Mac虚拟机)上,在第1.9.1小节开始,设备就一直连接在 ...
- [issue] [iOS 10] 升级后无法真机测试 Could not find Developer Disk Image
说明:更新了手机的到了iOS 10.0.2.真机调试时候提示"Could not find Developer Disk Image" 并且之前就下载了Xcode8,但是没有安装X ...
- 如何让老Mac机支持USB安装Windows
一些老Mac机的用户想装Windows,却发现自己的系统上的Boot Camp Assistant(以下简称BCA)没有USB安装Windows的选项. 下面以我的MacBook Pro (13-in ...
- iOS 开发之 Xcode6 创建真机调试证书
http://jingyan.baidu.com/article/ff411625b8141312e48237a7.html 1.登录苹果开发者中心 2.登录后的界面如图所示,如果没有最上面的两个选项 ...
随机推荐
- [ASP.NET] 如果将缓存“滑动过期时间”设置为1秒会怎样?
今天编写了一个采用ASP.NET Caching的组件,在为它编写Unit Test的过程中发现了一个有趣的问题,接下来我通过一个简单的实例说明这个问题.我们在一个控制台应用中编写了如下一段程序,这个 ...
- MySQL学习笔记四:字符集
1.字符集就是字符和其编码的集合,查看数据库支持的字符集 show character set 2.查看服务端启动时默认的字符集 mysql> show variables like 'char ...
- 窥探Swift之函数与闭包的应用实例
今天的博客算是比较基础的,还是那句话,基础这东西在什么时候都是最重要的.说到函数,只要是写过程序就肯定知道函数是怎么回事,今天就来讨论一下Swift中的函数的特性以及Swift中的闭包.今天的一些小实 ...
- 窥探Swift编程之在Playground上尽情的玩耍
自从苹果公司发布Swift的时候,Xcode上又多了一样新的东西---"Playground".Playground就像操场一样,可以供我们在代码的世界里尽情的玩耍,在本篇博客中就 ...
- pixi.js教程中文版--基础篇
前言 Pixi.js使用WebGL,是一个超快的HTML5 2D渲染引擎.作为一个Javascript的2D渲染器,Pixi.js的目标是提供一个快速的.轻量级而且是兼任所有设备的2D库.提供无缝 C ...
- 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 ...
- JavaScript 获取HTML中的CSS样式的属性以及值的的方法。
<body> <div id="wow" style="font-size:10px; float:left"></div> ...
- Eclipse与Android源码中ProGuard工具的使用
由于工作需要,这两天和同事在研究android下面的ProGuard工具的使用,通过查看android官网对该工具的介绍以及网络上其它相关资料,再加上自己的亲手实践,算是有了一个基本了解.下面将自己的 ...
- TypeError: invalid 'in' operand obj
尝试在程序去访问远程的Web API,它在运行时,出现异常: TypeError: invalid 'in' operand obj 由于从服务器返回的数据是json.当我们需要得到这些数据时,还得需 ...
- 异步编程系列第05章 Await究竟做了什么?
p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...