簡體   English   中英

對.xib文件中的UIView進行動畫處理

[英]Animating a UIView in a .xib file

我已經用Xcode編寫了一個告訴時間的程序。 我希望有一個矩形,隨着秒數的增加(時間進度),動畫/生長的時間會更長,並在秒數達到60(然后回到0)時重新開始。 我沒有情節提要,並且已經開始,所以我不想回去再開始。

我應該使用UIView實例並為'frame'屬性設置動畫嗎?

這是我的時鍾代碼:

- (void)viewDidLoad
{
[self updateTime];

[super viewDidLoad];

hourFormatter = [[NSDateFormatter alloc] init];
hourFormatter.dateFormat = @"HH";
minuteFormatter = [[NSDateFormatter alloc] init];
minuteFormatter.dateFormat = @"mm";
secondFormatter = [[NSDateFormatter alloc] init];
secondFormatter.dateFormat = @"ss";    
}

- (void)updateTime {

[updateTimer invalidate];
updateTimer = nil;

currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];

hourLabel.text = [hourFormatter stringFromDate: currentTime];
minuteLabel.text = [minuteFormatter stringFromDate: currentTime];
secondLabel.text = [secondFormatter stringFromDate: currentTime];

updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}

是的,您可以使用UIView並為其框架設置動畫。

不過,您不必一定要使用計時器來執行此操作,只需將動畫設置為在整個持續時間內運行,並更改整個幀的高度,然后使用計時器重新啟動下一個動畫即可。 這將提供最流暢的動畫。

要更新時間顯示,您只需要每秒運行一次計時器即可。

看一下這樣運行動畫:

CGRect frame = self.expandingTimeView.frame;
frame.height = 0;
self.expandingTimeView.frame = frame;

[UIView animateWithDuration:60
                 animations:^{
    frame.height = MAX_FRAME_HEIGHT;
    self.expandingTimeView.frame = frame;
}];

有一個很好的指導這里

這段代碼將放在您的updateTime方法中,我假設它將被計時器每分鍾調用一次。 無論您調用哪種view屬性,都需要更正expandingTimeView的名稱,並且需要將MAX_FRAME_HEIGHT替換為動畫期間視圖應增長到的最大高度。

您可能希望有2種方法,一種包含每分鍾調用一次的視圖動畫( updateTimeView ),另一種包含每秒調用一次的標簽文本更新( updateTimeLabels )的方法。 在這種情況下,應該在viewWillAppear:創建兩個計時器viewWillAppear:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM