2012-12-24 85 views
-1

我有3個按鈕:按鈕1,按鈕2和按鈕3.當我開始我的應用程序時,按鈕1位於頂部,按鈕2位於中部,按鈕3位於我的屏幕上。如何切換按鈕

當我按下其中一個按鈕時,按鈕必須從位置切換。 因此,例如button1開關button3或button1開關與按鈕2

我應該怎麼做?

回答

3

見您要使用動畫也是這樣一個例子東西...

-(IBAction)button1_Clicked:(id)sender{ 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
     CGRect btnframe = button1.frame; 
     button1.frame = button3.frame; 
     button3.frame = btnframe; 
///Also you can switch the buttons with center points of buttons like... 
/* 
     CGPoint btn1center = button1.center; 
     button1.center = button3.center; 
     button3.center = btn1center; 
*/ 

    //This bellow two lines for your requirement with change the function behind the buttons also... 

     // [button1 addTarget:self action:@selector(button3_Clicked:) forControlEvents:UIControlEventTouchUpInside]; 
     // [button3 addTarget:self action:@selector(button1_Clicked:) forControlEvents:UIControlEventTouchUpInside]; 
     [UIView commitAnimations]; 
    } 

這是該你的想法你的要求的一個例子。

希望這有助於你...

0

這是一個動畫解決方案,使用的,而不是現在老commitAnimations塊:

-(IBAction)clickedButton1:(id)sender{ 

    [UIView animateWithDuration:1 
        animations: ^{ 

      CGPoint center = button1.center; 
      button1.center = button2.center; 
      button2.center = center; 
    }]; 

} 

此代碼切換按鈕1和2的動畫。