按钮会在双击而不是单击时触发

时间:2012-01-22 23:26:37

标签: xcode

我在Xcode中完成了可以完美运行的视图动画(使用CGRect点击按钮时滑出屏幕。)不幸的是,我必须单击按钮两次才能执行动画。我做错了什么?我感谢任何反馈

这是我的.H代码

@interface AnimationBlocksViewController : UIViewController{
    IBOutlet UIView *theview;
    IBOutlet UIView *theview2;

    BOOL isAnimated;
    BOOL switchback;
}

-(IBAction)animate:(id)sender;
-(IBAction)change:(id)sender;

这是我的.M代码

-(IBAction)animate:(id)sender;{
    if (isAnimated) {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview setFrame:CGRectMake(0, 0, 320, 460)];
        [theview2 setFrame:CGRectMake(320, 0, 320, 460)];
        [UIView commitAnimations];
        isAnimated=NO;

    }


    else{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview setFrame:CGRectMake(-320, 0, 320, 460)];
        [theview2 setFrame:CGRectMake(0, 0, 320, 460)];
        [UIView commitAnimations];
        isAnimated=YES;

    }
}

-(IBAction)change:(id)sender;{
    if (switchback) {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview2 setFrame:CGRectMake(0, 0, 320, 460)];
        [UIView commitAnimations];
        switchback=NO;

    }


    else{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview2 setFrame:CGRectMake(320, 0, 320, 460)];
        [theview setFrame:CGRectMake(0, 0, 320, 460)];
        [UIView commitAnimations];
        switchback=YES;

    }
}

它有效,但我必须双击才能触发动画,我做错了什么? 谢谢

1 个答案:

答案 0 :(得分:0)

首次双击后,后续的点击是否会正确触发动画?如果是这样,可能是您未明确初始化BOOL个变量,但它们应默认为NO。尝试这样做,看看它是否有帮助。

如果您总是需要进行两次点击来制作动画,可以尝试插入一个对运行循环的调用,以查看它是否仅在您的应用程序线程上运行一段时间之后才处理动画,即它可能只是当它涉及到处理第二次单击事件它实际上是否会获取任何待处理的动画并执行它们。该代码将是(在您设置isAnimated并调用commitAnimations后):

[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate date]];

不确定您是否可以使用nil作为日期,但如果您愿意也可以尝试。