2009-08-26 51 views
0

我想知道如何去動畫UIView到特定的CGPoint。以下是我迄今(不以它的當前工作狀態):動畫UIView到CGPoint

#define MOVE_ANIMATION_DURATION_SECONDS 2 

NSValue *pointValue = [[NSValue valueWithCGPoint:point] retain]; 
[UIView beginAnimations:nil context:pointValue]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
[UIView commitAnimations]; 

回答

1

嘗試:

#define MOVE_ANIMATION_DURATION_SECONDS 2 

[UIView beginAnimations:nil context:pointValue]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

CGRect frame = myUIViewObject.frame; 
frame.origin = point; 
myUIViewObject.frame = frame; 

[UIView commitAnimations]; 
+0

出於某種原因,第一次運行時,動畫視圖似乎在y軸上有點下降。但是,在這之後它工作正常。有任何想法嗎? – PF1 2009-08-26 19:52:16

+0

我會檢查一下每次運行時'point'的值是什麼 - 我的猜測是第一次「點」不是你所期望的。 – fbrereto 2009-08-26 20:22:42

+0

感謝您的回覆fbrereton。我解決了這個問題,現在它工作的很好! – PF1 2009-08-26 21:35:02

1

後開始,但在提交前,更改您的用戶界面中查看值。

0

如果你的目標10.5或以上,你可以使用核心動畫通過動畫代理加入的NSView。嘗試

[[someView animator] setFrame: someNewFrame]; 
+0

這也適用於iPhone OS嗎? – fbrereto 2009-08-26 18:39:26

+0

我不知道我的頭頂(尚未有機會爲iPhone開發)。我會這樣想。 – 2009-08-26 19:04:23

+0

iPhone擁有與Mac不同的視圖體系結構,因此iPhone平臺上不存在動畫製作者代理。 – 2009-08-26 20:58:16