2011-09-27 128 views
0

大家.. 如何設置按鈕點擊動畫?UIButton上的動畫點擊iPhone

我有兩個按鈕,兩者具有不同的幀..

First CGRectMake (10 , 10 , 100 , 80); It has Black Image, 
Second CGRectMake (200, 10 , 210 , 80); It has White Image, 

當我點擊者的第一個按鈕,它應該以動畫從一幀移動到另一幀..

只有見過像第一的按鈕黑色圖像移動到第二個按鈕的位置。像按鈕的圖像變化..

後點擊按鈕框架不會改變......應該繼續作爲第一和第二..

我都試過,但沒有得到確切的

buttonFirst.frame = buttonSecond.frame; 
buttonFirst.center = buttonSecond.center; 
buttonFirst.transform = CGAffineTransformMakeTranslation(buttonSecond.frame.origin.x - buttonFirst.frame.origin.x , buttonSecond.frame.origin.y - buttonFirst.frame.origin.y); 

如何製作動畫呢?

EDITTING:

我需要從一個的CGRect移動一個棋子到另一種的CGRect。所以,它要顯示像移動棋子,但我不想改變的CGRect,只有按鈕圖像看起來像這樣.. 。

請告訴我.. 謝謝...

回答

0

最後我得到的答案...

我在兩個單獨的視圖中設置了兩個按鈕。設置之後一個定時器,和第一按鈕圖像移動到另一個按鈕的位置.. 下面是一個示例代碼..

步驟1:用定時器設定的視圖

UIView *oldView = [[UIView alloc] init]; 
        oldView = [viewArray objectAtIndex:selectedIndex]; 
        UIView *newView = [[UIView alloc] init]; 
        newView = [viewArray objectAtIndex:[sender tag]]; 
        oldRect = [oldView frame];      
        newRect = [newView frame]; 
        movingTimer = [[NSTimer alloc] init]; 
        movingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movingCheckers:) userInfo:nil repeats:YES]; 

        imageViewMove = [[UIImageView alloc] init]; 
        [imageViewMove setFrame:oldRect]; 
        [imageViewMove setImage:[UIImage imageNamed:blackManName]]; 

        [self.view bringSubviewToFront:imageViewMove]; 
        [self.view addSubview:imageViewMove]; 

步驟2:運動圖像第二位置

CGPoint aPoint = CGPointMake(newRect.origin.x - oldRect.origin.x, oldRect.origin.y - newRect.origin.y);      
imageViewMove.frame = CGRectMake(imageViewMove.frame.origin.x + (aPoint.x/6) , imageViewMove.frame.origin.y - (aPoint.y/6), imageViewMove.frame.size.width, imageViewMove.frame.size.height); 
if (imageViewMove.frame.origin.x >= newRect.origin.x) 
{ 
       [selectButton setBackgroundImage:nil forState:UIControlStateNormal]; 
       [moveButton setBackgroundImage:imageViewMove.image forState:UIControlStateNormal]; 
       [imageViewMove removeFromSuperview]; 
       [movingTimer invalidate]; 
} 

現在其工作.....

0

以及.. 你可以試試這個鏈接

[UIView beginAnimation/endAnimation] bock 
+0

是,還..這裏是一個代碼..的UIImageView * ImageView的= [[ALLOC的UIImageView] INIT]; [imageView setFrame:button.frame]; [imageView setImage:buttonS.currentBackgroundImage]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2.0]; [imageView setFrame:buttonS.frame]; [UIView commitAnimations]; [imageView removeFromSuperview];但沒有工作.. –

0

這是例如,從工作程序:

[self.buttonFirst setUserInteractionEnabled:NO]; 
    [UIView transitionWithView:self.buttonFirst 
        duration:1.0 
        options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveLinear 
       animations:^{ 
        buttonFirst.frame = buttonSecond.frame; 
        buttonFirst.center = buttonSecond.center; 
        } 
       completion:^(BOOL finished){ 
        [self.buttonFirst setUserInteractionEnabled:YES];       
       }]; 
+0

你有沒有檢查你的代碼?它是按照我的問題行事嗎? –

+0

在設置'animations'的部分'buttonFirst.frame = buttonSecond.frame;'它會工作。 – LightNight

+0

沒有夥計..你能詳細解釋我..? –

0
CGRect oldRectFirst = buttonFirst.frame; 
CGRect oldRectSecond = buttonSecond.frame; 
[UIView animateWithDuration:0.2f animations:^{ 
    buttonFirst.frame = oldRectSecond; 
    buttonSecond.frame = oldRectFirst; 
}]; 
+0

不工作它交替幀 –

+0

好吧,我想我明白了。現在檢查它 – kv0

0

很簡單的邏輯

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.20f]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[firstButton setFrame:CGRectMake(5, 50, 30, 30)];//moves to second button place 
[secondButton setFrame:CGRectMake(5, 5, 30, 30)];//moves to first button place 
[UIView commitAnimations]; 

相應地使用邏輯您使用..

+0

不工作它替代框架.. –