请试试这个
//在.h文件中声明一个变量
IBOutlet UIButton *btnpreviouslySelected;
CGRect customerButtonFrame,inventoryButtonFrame,templatesButtonFrame;
考虑 btnCustomer、btnInventroy、btntemplate 是您的三个按钮并将它们分别分配为 1、2、3 标记。viewsearch 是您要在两个按钮之间添加的新视图
//在.m
//in view did load method set following
btnpreviouslySelected=nil;
customerButtonFrame=btnCustomer.frame;
inventoryButtonFrame=btnInventory.frame;
templatesButtonFrame=btnTemplates.frame;
-(void) RepositioningFrameMneuTree
{
btnCustomer.frame=customerButtonFrame;
btnInventory.frame=inventoryButtonFrame;
btnTemplates.frame=templatesButtonFrame;
}
-(void) RepositioningMenuTree:(int)iCaseValue
{
if (iCaseValue==1)
{
btnInventory.center=CGPointMake(btnInventory.center.x,btnInventory.center.y+300);
}
if (iCaseValue==2 || iCaseValue==1)
{
btnTemplates.center=CGPointMake(btnTemplates.center.x,btnTemplates.center.y+300);
}
}
-(IBAction) buttonClicked:(UIButton*)btn;
{
if(btnpreviouslySelected==btn)
{
//set to default location
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//here remove search view
[viwSearchView removeFromSuperview];
[self RepositioningFrameMneuTree];
[UIView commitAnimations];
return;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//assign previousbutton to new button
btnpreviouslySelected=btn;
[self RepositioningFrameMneuTree];
viwSearchView.frame=CGRectMake(0,btn.frame.origin.y+45,viwSearchView.frame.size.width,viwSearchView.frame.size.height);
//add search view on your view
[self.buttonsview addSubview:viwSearchView];
//-------------------------------------------
//change the location of button below selected button to show the search button
switch (btn.tag) {
case 1:
[self RepositioningMenuTree:1];
break;
case 2:
[self RepositioningMenuTree:2];
break;
case 3:
//dont do any thing
break;
default:
break;
}
//end animation
[UIView commitAnimations];
}