需要一个按钮可以切换一个元素的位置。
目前我写的代码:
-(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显示出来,因此,应该怎么实现使用按钮切换tableView的隐藏和显示?只要改变X坐标就可以,谢谢。
1.定义一个标志位用来表示当前UITableView的显示状态,声明需要用到的三个成员变量
BOOL isShow;
UITableView *sideMenu;
CGFloat posX;
2.在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的显示只是frame的x坐标的变化,所以你可以根据开关状态来计算这个posX,还有就是你的这个UITableview 不需要在每次点击时来创建.把创建UITableView的代码放到viewDidLoad函数来初始化.见上面的代码.
-(IBAction)menuTouched{
posX=isShow ? 1060 : 960
// Animate
[UIView animateWithDuration:1.25 animations:^{
sideMenu.frame = CGRectMake(posX, 88, 63, 661);
}];
isShow=!isShow; //切换开关状态
}
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.5];
myButton.frame = CGRectMake(0,420,320,120);
[UIView commitAnimations];
希望能帮到你~
在 .h文件中添加:
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);
}];
}
}