我在一个情节提要中设置了一个UIButton,当按下它时,它会显示UIVIew的动画。我希望相同的按钮动画/移动UIView回到它的原始点时,它是第二次按下。
做这件事最好的方法是什么?
感谢您的帮助或指点。
这是我用来动画和移动UIVIew的代码:
我在用一个叫buttonCurrentStatus的BOOL。
-(IBAction)sortDeals:(UIButton*)sender {
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(@"Sort Deals touched button status = yes");
} else {
buttonCurrentStatus = NO;
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(@"Sort Deals touched button status = no");
}发布于 2012-11-19 01:58:08
另一种更通用的方法是使用将执行以下两个方法之一的IBAction。与前面的答案一样,您可以使用int值或BOOl来确定要使用的操作。这是一个更通用的答案,可以用于许多目的。
首先,您需要一个BOOL变量。在本例中,我将Bool变量设置为boolVarible (是的,我知道我把它拼错了)。默认情况下,我将其设置为YES。
bool boolVarible = YES;我把它变成了一个类变量。
-(IBAction)buttonAction:(id)sender {
if (boolVarible == YES)
{
//Execute first action
[self firstAction];
}
else
{
//Execute second action
[self secondAction];
}
}
-(void)firstAction {
//Do something here...
}
-(void)secondAction {
//Do something here...
}希望您能明白这一点。然后,您可以随时交换操作。只需更改BOOl的值,然后当按钮被按下时,它将执行另一个方法。我发现这比在一个动作中做所有这些事情更干净。祝好运。
发布于 2012-11-18 23:17:04
-(IBAction)sortDeals:(UIButton*)sender {
sender.selected = !sender.selected;
int moveby = (sender.selected) ? 30: -30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
menuTop.frame = CGRectMake(menuTop.frame.origin.x, menuTop.frame.origin.y + moveby, menuTop.frame.size.width, menuTop.frame.size.height);
[UIView commitAnimations];
}发布于 2012-11-19 03:00:21
对于您的问题,非常简单的解决方案是,如果您在类中声明了两个布尔变量,则需要实现此method.your代码的位置将如下所示
在.m文件中
BOOL Action1;
BOOL Action2;现在在ViewDidload方法中
(void)viewDidLoad
{
Action1=TRUE;
Action2=FALSE;
[super viewDidLoad];
}现在你的方法
(IBAction)sortDeals:(UIButton*)sender {
if (Action1 == TRUE)
{
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(@"Sort Deals touched button status = yes");
Action1=FALSE;
Action2=TRUE;
}
else
if (Action2 == TRUE)
{
CGRect menuTopFrame = self.menuTop.frame;
menuTopFrame.origin.y = 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.menuTop.frame = menuTopFrame;
[UIView commitAnimations];
NSLog(@"Sort Deals touched button status = no");
Action1=TRUE;
Action2=FALSE;
}希望它能在you.Thanks上运行
https://stackoverflow.com/questions/13439807
复制相似问题