【问题标题】:UIButton actionsUIButton 动作
【发布时间】:2012-11-18 11:46:34
【问题描述】:

我在情节提要中设置了一个 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");

    }

【问题讨论】:

  • CGRect ivar 不够保留最后一帧?
  • 我不明白。你有一个正确设置的例子吗?我真的很想学习如何在第一次触摸时让按钮执行一个动作,然后在第二次触摸时执行不同的动作。谢谢
  • 我的意思是你可以使用一个CGRect类变量来临时存储最后一帧。然后,当您重新点击按钮时,只需将按钮位置重置为使用的最后一帧。否则,如果结束帧始终相同,请检查 sender.frame 并根据您的要求重置帧。 (对不起,我在 iPad 上,如果没有人回答你,我会尽快做)。
  • @hanumanDev 您可以使用 BOOL 来存储当前状态并在每次按下按钮时翻转 bool。
  • 等我回家再给你一个代码示例,如果到时候没人有的话。

标签: iphone objective-c ios ibaction


【解决方案1】:

另一种更通用的方法是使用将执行两种方法之一的 IBAction。与之前的答案一样,您可以使用 BOOl 值或 int 来确定要使用的操作。这是一个更通用的答案,可用于多种用途。

首先,您需要一个 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 值,然后当按下按钮时,它将执行另一个方法。我发现这比一次完成所有操作更干净。祝你好运。

【讨论】:

    【解决方案2】:
    -(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];
    }
    

    【讨论】:

      【解决方案3】:

      您的问题的非常简单的解决方案是,如果您在需要实现此方法的类中声明两个布尔变量。您的代码将如下所示

      在.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;
         }
      

      希望它对你有用。谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        相关资源
        最近更新 更多