iOS-动画无响应 [英] IOS - animation not responding
问题描述
我想创建一个动画菜单。
- 首先它隐藏在屏幕外
- 当用户点击屏幕时,它将滑入
- 用户可以轻按它并执行某些操作
- 然后3秒钟后它将滑出屏幕
但是问题是当我点击它时没有响应。为什么?
示例代码:所有代码都在我的UIView类中。
显示=否;
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 {
//做某事
NSLog(@ tap box);
}
和:
-(void)touchesBe:(NSSet *)touch withEvent:(UIEvent *)event {
if(!showing){
box.frame = CGRectMake(500,-200,200,200);
[UIView animateWithDuration:0.5f delay:0.3f options:(UIViewAnimationOptionAllowUserInteraction)动画:^ {
box.frame = CGRectMake(500,200,200,200);
}完成:^(布尔完成){
显示=是;
[UIView animateWithDuration:0.5f delay:3.0f options:(UIViewAnimationOptionAllowUserInteraction)动画:^ {
box.frame = CGRectMake(500,-200,200,200);
}完成:^(布尔完成){
显示=否;
}];
}];
}
}
感谢您的帮助,对不起我的英语:)
编辑:更多详细信息
我尝试在屏幕上设置框的原点(例如500,600),它即使我的盒子移动到另一个位置,在点击开始点(500,600)时仍然会始终响应。
更新:
我改变了将盒子移动到屏幕外的方式
[UIView animateWithDuration:0.5f delay:0.3f options:UIViewAnimationOptionAllowUserInteraction动画:^ {
box.frame = CGRectMake(500,200,200,200);
}完成:^(布尔完成){
isShowMenubar = YES;
//使用NSTimer而不是延迟
[NSTimercheduledTimerWithTimeInterval:3.0f目标:自我选择器:@selector(hideBox)userInfo:无重复:NO];
}];
和
-(void)hideBox {
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction动画:^ {
box.frame = CGRectMake(500,-200,200,200);}完成: ^(布尔完成){
isShowMenubar = NO;
}];
}
没有收到触摸与动画的工作方式有关。您的盒子实际上是在屏幕外的,但是在视觉上,它正在通过动画。
即使将延迟设置为3.0,它仍会预先移动盒子(由于动画的缘故)方法仍被调用)。因此,在您当前的代码中,如果您在(500,-200)处单击该框,它将获得触摸效果。
要解决此问题,请使用NSTimer或
NSTimer:
[NSTimercheduledTimerWithTimeInterval :3.0目标:自我行动:@selector(dismissBox)userInfo:无重复:NO];
NSTimer的问题在于,您必须将动画代码放在单独的方法中。 / p>
中央中央调度:
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);
}完成:^(布尔完成){
显示=否;
}];
});
I want to create an animated menu.
- first it's hidden outside screen
- when user taps the screen, it will slide in
- user can tap it and it do something
- then after 3s it will slide outside screen
But problem is it not response when I tap it. Why?
example code: All code is in my UIView class.
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];
and:
-(void) tapMenuHandler: (UIView *)obj {
//Do something
NSLog(@"tap box");
}
and:
-(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;
}];
}];
}
}
Thank for your help and sorry for my English :)
Edit: more detail I tried set start my box's origin in screen (ex. 500,600), It still always response when tap at start point (500,600) even though my box move to another position.
Update: I changed my way to move box outside screen by use NSTimer then it's work!
[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];
}];
and
-(void)hideBox {
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{
box.frame = CGRectMake(500,-200,200,200);} completion:^(BOOL finished) {
isShowMenubar = NO;
}];
}
The reason why the box is not receiving touches has to do with how animation works. Your box is actually offscreen, but visually, it's going through the animation.
Even though you set the delay as 3.0, it still moves the box beforehand (due to the animation method still being called). So in your current code, if you tapped on the box at (500, -200) then it will receive the touch.
To fix this, either use an NSTimer or Grand Central Dispatch to delay.
NSTimer:
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self action:@selector(dismissBox) userInfo:nil repeats:NO];
The problem with NSTimer is that you'll have to put that animation code in a separate method.
Grand Central Dispatch:
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;
}];
});
这篇关于iOS-动画无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!