上學期原本想要在project上面加上等待的動畫,可是當時不太會用NSTimer,看了一下書總算有點瞭解
1. 首先動畫的基本想法就是: 初始畫面 -> 轉變的duration -> 最終畫面,所以依照此想法,NSTimer就是負責"轉變的duration"這個部分。如果要NSTimer處理需要使用以下的method
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
//其中上面的function return為一個NSTimer的object,可以利用此object來中斷動畫
2. 制作動畫分為兩步驟:
a. 先把要改變的狀態先寫好
b. call上述的method即可完成
3. sample code:
a. 想要改變的狀態
- (void)animateLabel{
if(count==0){
self.label.text = @"waiting.";
count++;
}
else if(count ==1){
self.label.text = @"waiting..";
count++;
}
else if(count ==2){
self.label.text = @"waiting...";
count++;
}
else{
self.label.text = @"waiting";
count=0;
}
}
b. call + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
- (void)viewDidLoad
{
[super viewDidLoad];
self.label.text = @"waiting";
count=0;
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(animateLabel)
userInfo:nil
repeats:YES];
}
4. myCode on GitHub
沒有留言:
張貼留言