2009-08-31 63 views
1

我想移動一個UIView,它很小。我的for循環只是將UIView向下移動100點。用for循環移動UIView

這是有效的,除非它在循環結束之前不會移動。

我試圖設置

[self.view setNeedsDisplay]和[MyView的setNeedsDisplay],我也把該函數內的NSTimer和nsthread,如下面看到的

[NSThread detachNewThreadSelector:@selector(doStuff) toTarget:self withObject:nil]; 

[NSTimer scheduledTimerWithTimeInterval:1/15 
    target:self selector:@selector(doStuff) 
     userInfo:nil 
    repeats:NO] ; 

但它只是不「動畫」。

在每次移動視圖後,我都會睡覺(1);但仍然沒有。

希望任何人都可以幫助我。

回答

1

您的循環塊你的看法,這發生在runloop,當視圖的drawRect被調用的實際重繪。你有三個選擇(還有更多)。

兩個由@Darren建議。另一種方法是調用performSelector:afterDelay傳遞視圖,將視圖移動100像素(for循環的內部)。在該例程結束時,調用performSelector:afterDelay:來重新調用下一個調用。我更喜歡使用計時器。

+0

謝謝。在我刪除了for循環並且做了一個計時器和布爾(應該移動)之後,一切都按照它應該的那樣工作。之後我無法理解爲什麼我正在嘗試for循環。 謝謝你們 – 2009-08-31 09:28:03

4

您不能使用for循環。您需要設置重複計時器;每當定時器啓動時,將視圖向下移動x像素,並且一旦視圖處於其最終位置,就取消定時器。

但是,你應該考慮使用的Core Animation:

[UIView beginAnimations:@"MyAnimation" context:nil]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:5.0]; // 5 seconds 

CGRect frame = myView.frame; 
frame.origin.y += 100.0; // Move view down 100 pixels 
myView.frame = frame; 

[UIView commitAnimations];