我想將圖像從屏幕的一側移動到另一側。iOS中的基本雪碧/ UIImage運動?
我有兩個UIImageViews,ball1和ball2。一個在左邊,一個在右邊。設備處於橫向模式。 當用戶點擊一個按鈕時,我希望它們切換邊,就像補間動畫一樣。我曾嘗試使用CGPoint,但我無法弄清楚。
我想將圖像從屏幕的一側移動到另一側。iOS中的基本雪碧/ UIImage運動?
我有兩個UIImageViews,ball1和ball2。一個在左邊,一個在右邊。設備處於橫向模式。 當用戶點擊一個按鈕時,我希望它們切換邊,就像補間動畫一樣。我曾嘗試使用CGPoint,但我無法弄清楚。
創建兩個CGPoints和一個NSTimer
CGPoint pos1;
CGPoint pos2;
NSTimer *timer;
在移動圖像之前的任何其他方法,設置您的CGPoints將是我們的編輯爲圖像的速度。由於您正在水平移動,CGPoint分別請求x和y參數。在此示例中,分別使用(2.0,0.0);
和(-2.0,0.0);
分別爲右和左。
pos = CGPointMake(2.0,0.0);
pos2 = CGPointMake(-2.0,0.0);
,然後設置定時器火了一個名爲「移動」
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector (move) userInfo:nil repeats:YES];
創建不是全局聲明使用了一個名爲stopBalls
方法停止移動球另一個計時器方法。 (假設它需要兩秒鐘的時間做到這一點。試驗及工程的任何更換2.0
)
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector (stopBall) userInfo:nil repeats:YES];
創建一個使用CGPoints和pos
變量移動圖像-(void)move
方法。假設圖像被命名爲ball1
和ball2
。
-(void)move
{
ball1.center = CGPointMake(ball1.center.x+pos1.x,ball1.center.y +pos1.y);
if (ball1.center.x > 2024 || ball1.center.x < 0)
pos1.x = -pos1.x;
if (ball1.center.y > 768 || ball1.center.y < 0)
pos1.y = -pos1.y;
ball2.center = CGPointMake(ball2.center.x+pos2.x,ball2.center.y +pos2.y);
if (ball2.center.x > 2024 || ball2.center.x < 0)
pos2.x = -pos2.x;
if (ball2.center.y > 768 || ball2.center.y < 0)
pos2.y = -pos2.y;
}
然後創建stopBalls
方法來阻止球移動。這會重置CGPoint並停止timer
。
-(void)stopBalls
{
pos = CGPointMake(0.0,0.0);
pos2 = CGPointMake(0.0,0.0);
[timer invalidate];
}
只需重新運行啓動移動的方法再次執行該操作即可。
同時不要在球移動時多次運行該方法,因爲這會使速度加倍,因爲另一個定時器(2.0,0.0);
和(-2.0,0.0);
被觸發。
CGPoint firstCenter = ball1.center;
CGPoint secondCenter = ball2.center;
[UIView animateWithDuration:1.0 animations:^{
ball1.center = secondCenter;
ball2.center = firstCenter;
}];
很好,謝謝。 – user1536836 2012-07-19 05:48:57
以及雪碧運動試試這個LINK
關於你的問題,你可以使用核心動畫,改變球 的立場是應該做的我猜
比我想找的要多一點,但它給了我一個編輯和玩的片段。謝謝! – user1536836 2012-07-19 05:49:25