iOS 數字滾動 類似於老 - 虎- 機的效果

来源:http://www.cnblogs.com/qianLL/archive/2016/06/18/5596472.html
-Advertisement-
Play Games

效果圖 具體實現代碼如下 ZCWScrollNumView.h文件 ZCWScrollNumView.m文件 控制端代碼 ...


效果圖

 

具體實現代碼如下 

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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • “階段一”是指我第一次系統地學習Android開發。這主要是對我的學習過程作個記錄。 最近學到用AsyncTask來處理有關網路的操作。雖然代碼看上去不是很複雜,但仍有很多地方有疑惑。所以研讀了一下API文檔,在這裡把我學到的和練習的代碼展示出來。如有錯誤,歡迎指出! 一、關於AsyncTask的< ...
  • 接著上篇《Android 採用get方式提交數據到伺服器》,本文來實現採用post方式提交數據到伺服器 首先對比一下get方式和post方式: 修改佈局: 添加代碼: ...
  • 【版權所有,轉載請註明出處。出處:http://www.cnblogs.com/joey-hua/p/5596830.html 】 上一篇說到進程調度歸根結底是調用timer_interrupt函數,在system_call.s中: 前面一堆push指令保存當前的寄存器,然後在ret_from_sy ...
  • 首先搭建模擬web 伺服器,新建動態web項目,servlet代碼如下: 再新建一個jsp頁面 新建android項目,頁面佈局: 代碼如下: 添加許可權:android.permission.INTERNET 運行項目後點擊按鈕後提示錯誤: 06-18 21:08:16.237: W/System. ...
  • 文件命名規範: 1. 項目統一使用類首碼ZY。 2. 分類命名+後面統一使用ZYExtension,例:NSDictionary+ZYExtension.h,常用分類定義在內部並寫好文檔註釋。如果功能性分類內部方法較多可以考慮按功能命名。 3. model文件可按伺服器介面名或欄位名命名,view、 ...
  • 【版權所有,轉載請註明出處。出處:http://www.cnblogs.com/joey-hua/p/5596746.html 】 首先看main.c里的初始化函數main函數裡面有個函數是對進程調度的初始化,sched_init()函數,次函數在sched.c中實現: 首先初始化任務0的TTS,F ...
  • 昨晚蘋果在舊金山召開了WWDC,看了WWDC2016直播,我們發現變得謹慎而開放的蘋果在新一版四大平臺系統中展示了很多變化,當然重中之重還是偉大的iOS。通過試用iOS10beta版,除了長大了的更強大的Siri主要感受到iMessage更加如微信般強大到除了一般的文字、圖片、表情、語音消息,還支持 ...
  • 《Android 內容觀察者的原理》中介紹了內容觀察者的一些基本原理,並做了簡單的實戰,本文接著進一步做一個小項目實戰 添加許可權:android.permission.READ_SMS 運行代碼後,只要簡訊發生變化,都會提示,並且簡訊的內容會自動保存在XXX.abc文件中 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...