2013-05-13 44 views
0

我想要一個按鈕來切換點擊時元素的位置。iOS/Xcode - 切換按鈕位置

我當前的代碼:

-(IBAction)menuTouched{ 

// Push menu 
UITableView * sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)]; 
sideMenu.backgroundColor = [UIColor redColor]; 

[self.view addSubview:sideMenu]; 

// Animate 
[UIView animateWithDuration:1.25 animations:^{ 
    sideMenu.frame = CGRectMake(960, 88, 63, 661); 
}]; 
} 

當點擊它的動畫中的tableview就像我想的按鈕。但是現在我想通過按下完全相同的按鈕來動畫化tableview。所以簡而言之,我希望通過更改X位置來使按鈕切入和切出一個表格視圖。什麼是最好的/最簡單的方法?

在此先感謝。

+0

@tolylon:檢查我的答案,它會幫助你。 – 2013-05-13 12:39:53

回答

0

.h file裏面添加

UITableView * sideMenu; 
BOOL checkInOut; 

.m文件裏添加

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    checkInOut=False; 
    // Push menu 
    sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)]; 
    sideMenu.backgroundColor = [UIColor redColor]; 

    [self.view addSubview:sideMenu]; 
} 


-(IBAction)menuTouched 
{ 
checkInOut=!checkInOut 
if(checkInOut) 
    { 
    // Animate 
    [UIView animateWithDuration:1.25 animations:^{ 
    sideMenu.frame = CGRectMake(960, 88, 63, 661); 
    }]; 
} 
else 
    { 
    [UIView animateWithDuration:1.25 animations:^{ 
    sideMenu.frame = CGRectMake(1060, 88, 63, 661); 
    }]; 
    } 
} 
+0

感謝您的回覆。我不確定你想讓我把什麼放在視圖中。另外我還不熟悉使用viewWillAppear的概念。 – Torylon 2013-05-13 13:22:01

+0

@Torylon:檢查編輯請...對不起,簡要回答... – BhushanVU 2013-05-13 13:39:49

+0

令人驚歎!像魅力一樣工作! – Torylon 2013-05-13 13:44:07

0

您將需要一個布爾值來存儲你所在的國家,但你可以只使用button.selected狀態的..類似的東西:

- (IBAction)menuTouched:(id)sender 
{ 
    sender.selected = !sender.selected; 

    if (sender.selected) 
    { 
     // toggle on stuff 
    } 
    else 
    { 
     // toggle off stuff 
    } 
} 
0

試試吧....

[UIView beginAnimations: nil context: nil]; 
[UIView setAnimationBeginsFromCurrentState: YES]; 
[UIView setAnimationDuration: 0.5]; 
myButton.frame = CGRectMake(0,420,320,120); 
[UIView commitAnimations]; 

希望我幫了忙。

0

你與檢查先前

sideMenu.frame = CGRectMake(1060, 88, 63, 661); 

設置爲看到有你她穿上表或縮小以執行相應的操作就可以執行與不同幀中的相同動畫。

希望這會有所幫助。

0

你必須接受作爲參考的UIButton而不是ID

- (IBAction)menuTouched:(UIButton *)sender 
    { 
     sender.selected = !sender.selected; 

     if (sender.selected) 
     { 
      [UIView animateWithDuration:1.25 animations:^ 
    { 
     UITableView * sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)]; 
     sideMenu.backgroundColor = [UIColor redColor]; 
     [self.view addSubview:sideMenu]; 
     sideMenu.frame = CGRectMake(960, 88, 63, 661); 
     }]; 
     } 
     else 
     { 
      [UIView animateWithDuration:1.25 animations:^{ 
     [sideMenu removeForSuperView]; 
     }]; 
     } 
    } 
0

1.設置一個標誌位來表示的UITableView目前state.The聲明需要3個變量。在viewDidLoad

-(void)viewDidLoad { 
    isShow=NO; 
    //to do something... 
    sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)]; 
    sideMenu.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:sideMenu]; 
} 

3.什麼的UITableView顯示

BOOL isShow; 
UITableView *sideMenu; 
CGFloat posX; 

2.initial設置是隻改變X座標的frame.so你可以計算出POSX取決於開關「state.and通知唐每次在viewDidLoad函數中輸入UITableView創建代碼時,都不會創建UITableView。

-(IBAction)menuTouched{ 
    posX=isShow ? 1060 : 960  
    // Animate 
    [UIView animateWithDuration:1.25 animations:^{ 
     sideMenu.frame = CGRectMake(posX, 88, 63, 661); 
    }]; 
    isShow=!isShow; //boggle button 
}