2013-06-26 18 views
3

我在新的iOS developement.When我按下秒錶開始按鈕,我想顯示像櫃檯令牌effect.I定時器附上圖片汽油泵儀爲您reference.I已經做顯示秒和分鐘,但我不知道,如何動畫自動滾動效果?我怎樣才能做到這一點?顯示秒錶計時動畫喜歡使用的NSTimer

當計數器在移動它不應該從一個數字跳到另一個數字,而是應該平滑地從一個數字移動到下一個數字,就像汽油泵米。由於 Default state

start 1

enter image description here

enter image description here

+1

您可以使用pickerview進行平滑滾動。 –

+0

@Ravindhiran檢查出 – Warewolf

回答

1

您需要手動滾動tableView而不是scrollToRowAtIndexPath,因爲此動畫使用自己的計時器間隔,而且非常困難,或者我們可以說不可能改變其時間間隔。

所以,我對實施這類問題的API,並做了一個演示應用程序爲您提供平滑滾動,只要你想。

您需要使用外部定時器觸發間隔爲1秒,內部計時器,將觸發每0.03秒是我tablerow的高度爲30我計算內部定時器的時間間隔爲:---

移動30個像素1秒,然後 在內部定時器內移動1像素0.33秒。

內部定時器將在外部定時器中初始化時每1秒失效並觸發一次。

並且內部定時器將執行單元的移動。

dial4是的tableView

看一看我的代碼。

#define rowHeight 30 

-(void)startCounter 
{ 
    outertimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(snapCell) userInfo:nil repeats:YES]; 
} 

-(void)stopCounter 
{ 
    [outertimer invalidate]; 
} 

-(void)snapCell 
{ 
    NSLog(@"Initialize Internal timer %i",secLsb); 
    secLsb++; 

    if (secLsb==10) { 
     [dial4 setContentOffset:CGPointMake(0, 0) animated:NO]; 
     secLsb=0; 
     NSLog(@"dial content offset y is %f",dial4.contentOffset.y); 
    } 

    [internaltimer invalidate]; 
    internaltimer=[NSTimer scheduledTimerWithTimeInterval:0.03 
            target:self 
            selector:@selector(automaticScroll) 
            userInfo:nil 
            repeats:YES]; 
} 

-(void)automaticScroll 
{ 
    NSLog(@"val is & y ======== %f",dial4.contentOffset.y); 

    [dial4 setContentOffset:CGPointMake(dial4.contentOffset.x,dial4.contentOffset.y+1) animated:NO]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return rowHeight; 
} 

看一看這裏Time Counter

+1

它不會使用'UIView'動畫來控制你的表視圖的'contentOffset'而不是實現你自己的計時器? – nielsbot

2

我做了這樣的事情之前 - 這段代碼不一定乾淨,但它的工作。

你想在你的.h文件中創建兩個UILabel S:

IBOutlet UILabel *figureLabel1; 
IBOutlet UILabel *figureLabel2; 

然後你想創建一個BOOL這樣我們就可以分辨出哪UILabel是對用戶可見。

BOOL firstLabel; 

因此,讓想象的初始標籤(示出了編號爲1)是figureLabel1和將來UILabel進行顯示(表示數字2)是figureLabel2。但是,當櫃檯增加一個,那麼figureLabel2上移並採取figureLabel1的地方。然後,當figureLabel1被隱藏時,當figureLabel1可見時,它代替figureLabel2

在這裏看到:

// Check what label is showing 
    if (firstLabel == YES) { 

    // Following code the hide and move the figureLabel1 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate: self]; //or some other object that has necessary method 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

    // Slowing fade out the figureLabel1 
    [figureLabel1 setAlpha:0]; 

    // Move the figureLabel1 up and out of the way 
    [figureLabel1 setFrame:CGRectMake(20, 108, 287, 55)]; 

    [UIView commitAnimations]; 

    // Following code the show and move the figureLabel2 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 

    // Slowing fade in the figureLabel2 
    [figureLabel2 setAlpha:1]; 

    // Move the figureLabel2 up and into position 
    [figureLabel2 setFrame:CGRectMake(20, 141, 287, 55)]; 

    [UIView commitAnimations]; 

    // Update BOOL for next label 
    firstLabel = NO; 

} else { 
    // Exactly the same but opposite 

} 

正如我所說的,這是不漂亮,但它顯示了基本概念。祝一切順利!

+0

基於塊的UIView動畫已被推薦超過begin動畫/ commitAnimations自ios4 – Abizern

0

從圖像可以假設你正在嘗試獲得倒計時的效果。在這裏看看countdown timer。它可以幫助你以某種方式。

享受編程!

+0

只有鏈接的答案是不是很有幫助..請將答案添加到文本.. – Abizern

0

一些好的想法而已,但我會添加另一個。

首先,使用Interface Builder中的Clip Subviews複選框,確保將容器視圖(藍色矩形)設置爲剪輯子項。第二,添加一組子視圖,每個數字的圖像要顯示每個數字(在您的案例中4 * 10 = 40個子視圖)。在IB中使用標籤,以便您可以輕鬆訪問每個標籤。將初始邊界設置爲低於您的底部邊距。

調用UIView animateWithDuration。在動畫塊中,將新數字視圖的框架設置爲出現在剪裁的父視圖的邊界中,並將舊數字視圖的框架設置爲剛好在容器邊界之上。由於兩個視圖的框架更改都在同一個塊中進行動畫,所以這會在舊數字滑出頂部時創建新數字滑動到位的效果。

在完成塊中,將舊數字的框架設回容器視圖下的原始位置。

使用此方法,您可以使用持續時間和時間曲線進行播放,以便您可以模擬在每次轉換之間完全顯示的數字時發生的暫停。

類似的方法也可以用於CALayer或sprites,但UIViews輕量級,性能良好,並且最容易在Interface Builder中組裝。