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 = 0;
    rect.size.height = _oneDigitHeight;
    self.label.numberOfLines = 1;
    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 = 1;
    if (aDigit > last) {
        for (int i = (int)last + 1; i < aDigit + 1; ++i) {
            ++count;
            [str appendFormat:@"\n%d", i];
        }
    } else {
        for (int i = (int)last + 1; i < 10; ++i) {
            ++count;
            [str appendFormat:@"\n%d", i];
        }
        for (int i = 0; i < aDigit + 1; ++i) {
            ++count;
            [str appendFormat:@"\n%d", i];
        }
    }
    self.label.text = str;
    self.label.numberOfLines = count;
    CGRect rect = self.label.frame;
    rect.origin.y = 0;
    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 = 2;
    CGRect rect = self.label.frame;
    rect.origin.y = 0;
    rect.size.height = _oneDigitHeight * 2;
    self.label.frame = rect;
    digit = aDigit;
}

- (void)setRandomScrollDigit:(NSUInteger)aDigit length:(NSUInteger)length{
    NSMutableString *str = [NSMutableString stringWithFormat:@"%zd", self.digit];
    for (int i = 1; i < length - 1; ++i) {
        [str appendFormat:@"\n%d", rand() % 10];
    }
    [str appendFormat:@"\n%zd", aDigit];
    self.label.text = str;
    self.label.numberOfLines = length;
    CGRect rect = self.label.frame;
    rect.origin.y = 0;
    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 = {{0, 0}, self.frame.size};
    self.backgroundView.frame = backrect;
    [self addSubview:self.backgroundView];

    CGSize size= [@"8" sizeWithFont:self.digitFont];

    _oneDigitHeight = size.height;

    CGRect rect = {{(self.frame.size.width - size.width) / 2, (self.frame.size.height - size.height) / 2}, size};
    UIView *view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = [UIColor clearColor];
    view.clipsToBounds = YES;
    rect.origin.x = 0;
    rect.origin.y = 0;
    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 = 1;
    numberValue = 0;
    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 = 0; 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:0];
                    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 = 0; i < index; ++i) {
        num /= 10;
    }

    return num % 10;
}

- (NSUInteger)digitIndex:(NSUInteger)index {
    return [ZCWScrollNumView digitFromNum:self.numberValue withIndex:index];

}

- (void)didConfigFinish {
    CGRect backRect = {{0, 0}, 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 + 1) * splitSpaceWidth) / self.numberSize;
    NSData *digitBackgroundViewData = [NSKeyedArchiver archivedDataWithRootObject:self.digitBackgroundView];
    for (int i = 0; i < numberSize; ++i) {
        CGRect rect = {{allWidth - (digitWidth + self.splitSpaceWidth) * (i + 1), self.topAndBottomPadding}, {digitWidth, self.frame.size.height - self.topAndBottomPadding * 2}};

        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:0];
}

-(void)setscrollNumer{

    CGRect tmp = self.weishuView.bounds;
    self.weishuView.numberSize = 3
    ;
    UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth:10 topCapHeight:14];
    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:12 topCapHeight:12];
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];
    bgImageView.frame = tmp;
    bgImageView.autoresizingMask = kAllFullSuperviewMask;
    [digitBackView addSubview:bgImageView];
    image = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth:12 topCapHeight:12];
    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 = 3
    ;
    UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth:10 topCapHeight:14];
    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:12 topCapHeight:12];
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image];
    bgImageView.frame = tmp;
    bgImageView.autoresizingMask = kAllFullSuperviewMask;
    [digitBackView addSubview:bgImageView];
    image = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth:12 topCapHeight:12];
    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:19],
       NSForegroundColorAttributeName:XMGRGBColor(216, 184, 123)}];
    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:3];

}
- (IBAction)qiuWeiShu {
    self.weishuView.hidden = NO;
    [self.weishuView setNumber:rand() withAnimationType:ZCWScrollNumAnimationTypeRand animationTime:3];
}
@end
时间: 2024-10-14 07:45:51

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

cocos2d实现SlotMachine(老*虎*机)

实现一个四个转子,每个转子有五个花色的老*虎机.转子的转动实现原理很简单,和背景图无限滚动的原理是一样的:排成列的精灵在屏幕上向上滚动,再通过裁剪结点的裁剪就造成了转子滚动的效果. 1 void LayerSlotMachine::on_btn_roll(CCObject* pSender, CCControlEvent event) 2 { 3 if (!flag_act_) 4 { 5 schedule(schedule_selector(XLayerSlotMachine::roll_up

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

iOS Sprite Kit教程之真机测试以及场景的添加与展示 IOS实现真机测试 在进行真机测试之前,首先需要确保设备已经连在了Mac(或者Mac虚拟机)上,在第1.9.1小节开始,设备就一直连接在Mac(或者Mac虚拟机)上,并且此设备就是注册过的.打开创建的项目,在运行按钮一栏中,将程序运行的设备设置为真机的名称.它会自动加载到"选择程序运行的设备"这一项中,如图1.102所示.单击运行按钮,就可以看到应用程序在真机上运行了. 图1.102  设置设备 Sprite Kit的场景

XE6 &amp; IOS开发之免证书真机调试(1):颁发属于自己的App签名证书(有图有真相)

XE6 & IOS开发之免证书真机调试(1):颁发属于自己的App签名证书(有图有真相) 2014-08-25 21:26 网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 1.自己给自己颁发证书,用于给App签名.(如果还没有MAC环境,请参照"XE6移动开发环境搭建之IOS篇"进行配置.)---------------------------------------------------------

XE6 &amp; IOS开发之免证书真机调试(2):连接真机并运行App(有图有真相)

XE6 & IOS开发之免证书真机调试(2):连接真机并运行App(有图有真相) 2014-08-26 21:26 网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 连接真机前,请先确保真机已越狱,并已安装好AppSync.--------------------------------------------------------------- 1.确保iPhone已越狱,可以看到并成功运行cydia为准. 2.并且安

XE6 &amp; IOS开发之免证书真机调试(3):生成iPA文件并安装到其它苹果设备上(有图有真相)

XE6 & IOS开发之免证书真机调试(3):生成iPA文件并安装到其它苹果设备上(有图有真相) 2014-08-27 20:51 网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 1.既然我们已经在真机上运行过App了,那么现在就来生成一下.--------------------------------------------------------------- 1.在虚拟机下,点一下桌面(星空图随便某个地方),在F

那些H5用到的技术(6)——数字滚动特效

前言原理源码使用方式补充CountUp.js 前言 会有这么一种情况,H5页面需要进行数字统计展示,以此来强调产品or工作的成果.如果只是静态显示一个数字,总是感觉生硬.对比如下: 是不是瞬间高大上了呢? 这个效果我是在开源中国上找到的 https://www.oschina.net/code/snippet_2380148_52928 感谢馒头同学. 原理 1.如上图可知,代码将每个数字生成了一竖0-9和小数点的队列.如果需要滚动999,那么就会生成3竖 2.由于有height的限制,所以通过

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

一些老Mac机的用户想装Windows,却发现自己的系统上的Boot Camp Assistant(以下简称BCA)没有USB安装Windows的选项. 下面以我的MacBook Pro (13-inch, Late 2011), OS X EI Capitan为例,让BCA支持USB安装Windows. 0.打开“/Applications/Utilities/System Information.app”,记下“Model Identifier”和“Boot ROM Version”的值(我

关于iOS开发者账号功能总结-真机调试(二)

转载请注明出处:http://blog.csdn.net/dengbin9009/article/details/43970059 在上篇文章中说道如何从开发者网站创建App ID,添加Device,创建并下载开发证书,创建并下载配置文件,详情见:关于iOS开发者账号功能总结-真机调试(一). 在这篇文章中将要介绍的时如何利用下载号的证书cer文件和配置文件mobileprovision. 上篇文章我们已经将证书和配置文件下载到桌面,不过为了Mac的贞洁(笔者处女座),已经把两个文件放到特定的文

IOS UItableView 滚动到底 触发事件

开发过程中,在使用UItableView 总会遇到加载更多的问题,到底是手势响应瀑布流的方法好? 还是添加一个底端cell点击触发加载更多好?我也想有自己的判断.但是我们老板总说了算,没办法,谁叫我给人家打工呢? cell触发式略,这个很简单. 下边讲下怎么响应瀑布流. 主要有上拉刷新 和下提加载 这两种操作.关键是判断何时执行:滚动到最上边,滚动到最下边 给出一个方法,目前我是这么做的,有更好的方法我再备份. - (void)scrollViewDidEndDragging:(UIScroll