2012-03-22 59 views
0

我想创建一个动画菜单。IOS - 动画没有响应

  • 第一它是隐藏的外屏
  • 当用户点击屏幕,它将在
  • 用户滑动可以点击它,它做一些三分球后
  • 那么它会滑动屏幕之外

但问题是,当我点击它时没有反应。为什么?

示例代码:所有代码都在我的UIView类中。

showing = NO; 
box = [[UIView alloc] initWithFrame:CGRectMake(500,-200,200,200)]; 
box.backgroundColor = [UIColor redColor]; 

UITapGestureRecognizer *tapBox = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenuHandler:)] autorelease]; 
[box addGestureRecognizer:tapBox]; 

[self addSubView:box]; 

和:

-(void) tapMenuHandler: (UIView *)obj { 
    //Do something 
    NSLog(@"tap box"); 
} 

和:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
if (!showing) { 

    box.frame = CGRectMake(500, -200, 200, 200); 

    [UIView animateWithDuration:0.5f delay:0.3f options:(UIViewAnimationOptionAllowUserInteraction) animations:^{ 

     box.frame = CGRectMake(500, 200, 200, 200); 

    } completion:^(BOOL finished) { 
     showing = YES; 

        [UIView animateWithDuration:0.5f delay:3.0f options:(UIViewAnimationOptionAllowUserInteraction) animations:^{ 
         box.frame = CGRectMake(500, -200, 200,200); 
        } completion:^(BOOL finished) { 
         showing = NO; 
        }]; 

    }];  
} 

}

感谢您的帮助,我的英语水平:)对不起

编辑:更详细 我三编辑设置开始我的方块在屏幕上的起源(前。 500,600),即使我的箱子移动到另一个位置,仍然总是在起点(500,600)处轻敲。

更新: 我改变了我的方式,通过使用NSTimer移动框外屏,然后它的工作!

[UIView animateWithDuration:0.5f delay:0.3f options:UIViewAnimationOptionAllowUserInteraction animations:^{ 

     box.frame = CGRectMake(500,200,200,200); 

    } completion:^(BOOL finished) { 
     isShowMenubar = YES; 
     //use NSTimer instead of delay 
     [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(hideBox) userInfo:nil repeats:NO]; 

    }]; 

-(void)hideBox { 
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{ 
    box.frame = CGRectMake(500,-200,200,200);} completion:^(BOOL finished) { 
     isShowMenubar = NO; 
    }]; 
} 
+0

你实现tapMenuHandler法您为TapGestureRecognizer设置? – matzino 2012-03-22 15:27:49

+0

感谢matzino :)这是我的错。我编辑我的代码,但它仍然是问题。 – navinking2000 2012-03-22 16:44:24

+0

您的tapMenuHandler-Method有一个错误的签名。它必须像 - (IBAction)tapMenuHandler:(UIGestureRecognizer *)发件人。 IBAction或void是相等的,但发送者参数必须是您的UIGestureRecognizer。解决你的问题? – matzino 2012-03-23 10:05:16

回答

3

为什么箱不接收触摸原因与动画如何工作要做。你的盒子实际上是离屏的,但在视觉上,它正在通过动画。

即使您将延迟设置为3.0,它仍然会事先移动框(由于仍然调用动画方法)。因此,在你当前的代码中,如果你点击(500,-200)的方框,那么它会接收到触摸。

要解决此问题,请使用NSTimer或Grand Central Dispatch来延迟。

的NSTimer:

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self action:@selector(dismissBox) userInfo:nil repeats:NO]; 

用的NSTimer的问题是,你必须把该动画代码在一个单独的方法。

大中央调度:

double delayInSeconds = 3.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [UIView animateWithDuration:0.5f animations:^{ 
     box.frame = CGRectMake(box.frame.origin.x, -200, 200,200); 

    }completion:^(BOOL finished) { 
     showing = NO; 
    }]; 
}); 
+0

嗨凯文,这是我的错误了。我的真实代码与您所说的相同,而且我已经在上面进行了编辑。 – navinking2000 2012-03-23 03:12:50

+0

哦!我以为你的意思是它甚至不提箱子,但你的意思是在箱子上敲击。改变了我的答案。 – 2012-03-23 14:14:36

+0

凯文!感谢您的大力帮助。它解决了我的问题 – navinking2000 2012-03-25 18:20:24

0

我跑的代码,并没有看到红色的看法要么。然后玩了一下,我知道你的观点是动画,但仍然出界,所以你永远不会看到它。尝试将动画块更改为类似的东西(框架的原点位于左下角)

box.frame = CGRectMake(100, 100, 200, 200); 

并希望它会动画。

此外,假设您在视图控制器运行这些,正确的方法应该是

[self.view addSubview:box]; 

希望这有助于

+0

嗨Kaan。感谢您的帮助,但我希望我的盒子在屏幕外开始动画。 – navinking2000 2012-03-23 06:35:12

+0

我明白,但你的盒子开始出界(因为它的左下角有x坐标500),并且结束界限(因为x值在动画中从不改变)。 iPhone屏幕宽度为320像素。所以如果你想以红色方块为中心,你应该在viewDidLoad(当你调用init时)和在动画块中改变它的x值为60,试一试它会工作 – 2012-03-24 19:16:33

+0

我很抱歉,但我的应用程序运行在iPad上(横向)。 – navinking2000 2012-03-25 18:09:08