iOS倒計時動畫

2021-10-01 19:46:20 字數 2720 閱讀 4504

效果圖

製作成gif之後,幀率變快了,看起來很彆扭,實際上是每秒執行一次的

.h 檔案

@inte***ce countdownview : uiview

@property (nonatomic, assign) long long time;

@property (nonatomic, assign, readonly) long long remaintime;

@end

@inte***ce countdownlabel : uiview

@property (nonatomic, assign) catransitionsubtype subtype;

@property (nonatomic, copy) nsstring *text;

@end

.m 檔案
#import "countdownview.h"

@inte***ce countdownview ()

@property (nonatomic, strong) countdownlabel *hourlabel1;

@property (nonatomic, strong) countdownlabel *hourlabel2;

@property (nonatomic, strong) uiimageview *dot1;

@property (nonatomic, strong) countdownlabel *minutelabel1;

@property (nonatomic, strong) countdownlabel *minutelabel2;

@property (nonatomic, strong) uiimageview *dot2;

@property (nonatomic, strong) countdownlabel *secondlabel1;

@property (nonatomic, strong) countdownlabel *secondlabel2;

@property (nonatomic, assign) bool stop;

@property (nonatomic, assign) bool running;

@end

@implementation countdownview

- (instancetype)init

- (instancetype)initwithframe:(cgrect)frame

return self;

}- (void)setupsubviews

- (void)settime:(long long)timeelse

}- (void)timeout:(long long)timeelse

self.hourlabel1.text = [nsstring stringwithformat:@"%ld",hour/10];

self.hourlabel2.text = [nsstring stringwithformat:@"%ld",hour%10];

self.minutelabel1.text = [nsstring stringwithformat:@"%ld",min/10];

self.minutelabel2.text = [nsstring stringwithformat:@"%ld",min%10];

self.secondlabel1.text = [nsstring stringwithformat:@"%ld",sec/10];

self.secondlabel2.text = [nsstring stringwithformat:@"%ld",sec%10];

dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(1 * nsec_per_sec)), dispatch_get_main_queue(), ^

});}@end

@inte***ce countdownlabel ()

@property (nonatomic, strong) cagradientlayer *gradient;

@property (nonatomic, strong) uilabel *textlabel;

@end

@implementation countdownlabel

- (instancetype)init

- (instancetype)initwithframe:(cgrect)frame

return self;

}- (void)settext:(nsstring *)text

_text = text;

self.textlabel.text = text;

}- (void)layoutsubviews

// 動畫

- (void)fadelayer

@end

說明

這個方法每次執行的時間並不是嚴格的1秒鐘,列印時間戳上有細微的誤差,不過很難感覺出來,影響不大

dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(1 * nsec_per_sec)), dispatch_get_main_queue(), ^);

Unity倒計時動畫

最近在做乙個小遊戲開始的時候需要用到倒計時的效果,具體效果為每秒顯示3,2,1,go,然後字型由大到小,透明度由淺到深。下面介紹三種方法。1.最直接的方法,自己寫倒計時的函式,開協程改變字型大小和透明度,這種方法較為簡單,下面是我寫的 其中state為需要改變的text的text元件 public ...

iOS 仿直播321倒計時動畫

今天碰到乙個需求,仿全民直播的321倒計時動畫,類似下圖的效果,其實仔細想想並不難,但在網上找了下,有的做的還是有點複雜了,在這裡分享下我的做法。無非就是乙個倒計時,這裡nstimer實現,然後乙個label,並且對label裡的數字實施動畫,產生如圖的效果。這裡動畫採用的是cakeyframean...

iOS 倒計時按鈕

1 新建乙個類,取名為 timerbtn,繼承uibutton 2 在timer.h中新增如下 import inte ce timerbtn uibutton 建立倒計時按鈕 param frame 位置大小 param title 標題 param ntimer 倒計時時間 return 倒計時...