首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIButton操作

UIButton操作
EN

Stack Overflow用户
提问于 2012-11-18 19:46:35
回答 3查看 388关注 0票数 1

我在一个情节提要中设置了一个UIButton,当按下它时,它会显示UIVIew的动画。我希望相同的按钮动画/移动UIView回到它的原始点时,它是第二次按下。

做这件事最好的方法是什么?

感谢您的帮助或指点。

这是我用来动画和移动UIVIew的代码:

我在用一个叫buttonCurrentStatus的BOOL。

代码语言:javascript
复制
-(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");

    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-19 01:58:08

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

首先,您需要一个BOOL变量。在本例中,我将Bool变量设置为boolVarible (是的,我知道我把它拼错了)。默认情况下,我将其设置为YES。

代码语言:javascript
复制
bool boolVarible = YES;

我把它变成了一个类变量。

代码语言:javascript
复制
-(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的值,然后当按钮被按下时,它将执行另一个方法。我发现这比在一个动作中做所有这些事情更干净。祝好运。

票数 1
EN

Stack Overflow用户

发布于 2012-11-18 23:17:04

代码语言:javascript
复制
-(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];
}
票数 1
EN

Stack Overflow用户

发布于 2012-11-19 03:00:21

对于您的问题,非常简单的解决方案是,如果您在类中声明了两个布尔变量,则需要实现此method.your代码的位置将如下所示

在.m文件中

代码语言:javascript
复制
BOOL Action1;
BOOL Action2;

现在在ViewDidload方法中

代码语言:javascript
复制
 (void)viewDidLoad 
 {
   Action1=TRUE;
   Action2=FALSE;
   [super viewDidLoad];

  }

现在你的方法

代码语言:javascript
复制
  (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上运行

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13439807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档