2011-07-26 75 views
0

我的屏幕上有六個座位對象(alpha屬性設置爲0的UIViews)的集合,我的玩家對象基本上放置在他們的頂部。座位上可能有也可能沒有一個玩家。我現在所擁有的是我編寫了玩家的touchesMoved事件,因此當我將玩家拖到座位對象的頂部時,座位的alpha屬性將從0變爲0.6。然後在拖動播放器的同時,如果我將他拖出座位,則alpha屬性將返回0.將鼠標懸停在動畫上的alpha屬性

相反,是否存在內置的UIView動畫,可能會導致alpha屬性出現波動,介於.6和.2之間?一種跳動的效果?這需要核心動畫還是更高級的東西?

我使用的球員的touchesMoved方法如下拖動播放器來檢測,如果它是一個座位上方:

UITouch *aTouch = [touches anyObject]; 
self.center = [aTouch locationInView:[self.superview]]; 
Seat *seat = [controller seatAtPoint:[aTouch locationInView:self.superview]]; 

if (seat) { 
self.hoverSeat = seat; 
    seat.alpha = .6; 
} else { 
    self.hoverSeat.alpha = 0; 
} 

在我的控制器的seatAtPoint方法如下:

- (Seat *) seatAtPoint:(CGPoint)point { 

NSMutableArray seats = [NSMutableArray arrayWithCapacity:6]; 

for (int i = 1; i <= 6; i++) { 
    Seat *aSeat = (Seat*)[self.view viewWithTag:i]; 
    [seats addObject:aSeat]; 
} 

for (Seat *seat in seats) { 
if (CGRectContainsPoint([seat frame], point)) { 
    return seat; 
} 
} 
return nil; 
} 

我使用hoverSeat ivar來保持玩家徘徊的座位。然後,如果返回的座位是零,那麼它將該座位的alpha設置爲0.

我看到的這個代碼中的一個錯誤是,如果我在屏幕上移動屏幕有點太快,有時候alpha屬性不會回到0.任何人都可以想到一個更有效的方法來確保它回到0嗎?

謝謝你的任何建議。

回答

2

我會考慮使用CoreAnimation;這並不難。下面是它可能看起來像一個褪色:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:.5]; 
seat.alpha = 1; 
[UIView commitAnimations]; 

你可以看看,像這樣的使用方法來創建某種脈衝時的條件相結合的動畫(Trigerring other animation after first ending Animation (Objetive-C))的回調,你也許可以將它設置成永遠循環。另外值得注意的是,UIView動畫在運行時會禁用用戶輸入(Infinitely looping animation)。

至於你提到的錯誤,如果它還在發生,你可以設置一個計時器,當它被觸發時,遍歷所有的座位並檢查沒有被正確顯示的計時器。您可能希望將此間隔設置爲非常少,以便它不會中斷或影響其他動畫的性能。