在我的应用中,我有一些动画。例如,我在主菜单中有一个按钮,当你点击它时动画开始(比如移动一些地方等),在动画结束时它会导航到另一个页面。我需要的是在动画期间禁用用户交互。因为在动画期间如果我按下按钮的起点,应该导航的页面打开两次。总而言之,如果我在动画过程中不让任何用户交互,我的问题就会得到解决。我怎么能这样做?
答案 0 :(得分:20)
动画之前:
self.view.userInteractionEnabled = NO;
并在动画完成块中:
self.view.userInteractionEnabled = YES;
答案 1 :(得分:17)
这可能会有所帮助:
// for ignoring event
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
代码如下:
[UIView animateWithDuration:1.0 animations:^{
//some animation
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
}
completion:^(BOOL done){
if (done){
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
}
];
答案 2 :(得分:6)
很简单,您可以在动画开始前将setUserInteractionEnabled
设置为NO
,并在动画完成处理程序中将其设置回YES
。
[myObject setUserInteractionEnabled:NO];
[UIView animateWithDuration:1.0 animations:^{
[myObject setTransform:CGAffineTransformMakeTranslation(100, 100)];//some animation
}completion:^(BOOL done){
if (done){
[myObject setUserInteractionEnabled:YES];
}
}];
答案 3 :(得分:5)
你不必破解完成块 - 有一个动画选项可以做到这一点:
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
// animations here
}
completion:nil];
如果您设置了UIViewAnimationOptionAllowUserInteraction
,那么就允许用户互动。
答案 4 :(得分:2)
yourView.userInteractionEnabled = NO;
[UIView animateWithDuration:1 animations:^
{
//animations here
}
completion:^(BOOL finished)
{
yourView.userInteractionEnabled = YES;
}];
答案 5 :(得分:2)
要在视图中禁用触摸事件,
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
在视图中启用触摸事件
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
答案 6 :(得分:1)
禁用Button的userIntrection。
Btn.userInteractionEnabled = NO;
答案 7 :(得分:0)
我的视图控制器带有打开页面的图标。 如果用户快速点击icon1和icon2,则打开2页。
以防止我在点击事件的开头有这2行 这确保无论发生什么,endIgnoring都会调用
-(void) on_image_tap:(UITapGestureRecognizer * ) tapGesture
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.5f];