【问题标题】:How to let an animation finish before the next userinput is allowed如何在允许下一个用户输入之前让动画完成
【发布时间】:2011-03-27 08:06:06
【问题描述】:

我有一些 UIImageViews,当用户触摸时,会启动一段持续时间的动画。触发器计数 ++,下一次触摸开始播放另一个动画。 但是,如果用户快速触摸或进行双击,第一个动画直到最后一帧才会结束。 我尝试了“sleep()”命令,但它不起作用。

    #pragma mark HenneAnimation
    if([touch view] == ani_Henne){

        //trigger strats with zero
        switch (trigHenne) {
            case 0:
//firstanimation 1 sec
                ani_Henne.animationImages = [NSArray arrayWithObjects:
                                             [UIImage imageNamed:@"ani_Henne01.png"],
                                             [UIImage imageNamed:@"ani_Henne01.png"],
                                             [UIImage imageNamed:@"ani_Henne02.png"],
                                             [UIImage imageNamed:@"ani_Henne01.png"],
                                             [UIImage imageNamed:@"ani_Henne01.png"],nil];


                ani_Henne.animationDuration = 1;
                ani_Henne.animationRepeatCount = 1;
                [ani_Henne startAnimating];
                [self.view addSubview:ani_Henne];

                trigHenne++;
                break;

            case 1:
//second animation 1 sec                
                ani_Henne.animationImages = [NSArray arrayWithObjects:

                                             [UIImage imageNamed:@"ani_Henne03.png"],
                                             [UIImage imageNamed:@"ani_Henne05.png"],
                                             [UIImage imageNamed:@"ani_Henne03.png"],
                                             [UIImage imageNamed:@"ani_Henne03.png"],
                                             [UIImage imageNamed:@"ani_Henne04.png"],
                                             [UIImage imageNamed:@"ani_Henne05.png"],
                                             [UIImage imageNamed:@"ani_Henne03.png"],nil];              
                ani_Henne.animationDuration = 3;
                ani_Henne.animationRepeatCount = 1;
            [ani_Henne startAnimating];
                [self.view addSubview:ani_Henne];

                trigHenne++;

                break;

            case 2:
                [self.view bringSubviewToFront:ani_Henne];

                ani_Henne.animationImages = [NSArray arrayWithObjects:
                                             [UIImage imageNamed:@"ani_Henne06.png"],
                                             [UIImage imageNamed:@"ani_Henne07.png"],
                                             [UIImage imageNamed:@"ani_Henne06.png"],
                                             [UIImage imageNamed:@"ani_Henne06.png"],
                                             [UIImage imageNamed:@"ani_Henne08.png"],
                                             [UIImage imageNamed:@"ani_Henne08.png"],
                                             [UIImage imageNamed:@"ani_Henne09.png"],
                                             [UIImage imageNamed:@"ani_Henne08.png"],
                                             [UIImage imageNamed:@"ani_Henne09.png"],
                                             [UIImage imageNamed:@"ani_Henne08.png"],
                                             [UIImage imageNamed:@"ani_Henne07.png"],
                                             [UIImage imageNamed:@"ani_Henne08.png"],
                                             [UIImage imageNamed:@"ani_Henne09.png"],
                                             [UIImage imageNamed:@"ani_Henne08.png"],nil];

                ani_Henne.animationDuration = 2.75;
            ani_Henne.animationRepeatCount = 1;
                [ani_Henne startAnimating];
                [self.view addSubview:ani_Henne];

                trigHenne++;
                break;
            case 3:
                trigHenne=0;
// etc. animations
                break;
            default:
                break;  
        }       
    }

【问题讨论】:

    标签: iphone animation timer uiimage sleep


    【解决方案1】:

    当您开始设置动画时禁用用户交互,完成后重新启用它,如下所示:

    yourImageView.userInteractionEnabled = NO;

    yourImageView.userInteractionEnabled = YES;

    你也可以延迟启用

    ...
    [self performSelector:@selector(enable) withObject:yourImageView afterDelay:1.0];
    ...
    
    
    -(void)enable
    {
    yourImageView.userInteractionEnabled = YES
    }
    

    【讨论】:

    • 谢谢!我今晚试试。
    • 谢谢,它就像我想要的那样工作!它比 beginIgnoringInteractionEvents 解决方案更好更简单,因为只有选择的 View 被禁用输入!
    【解决方案2】:

    使用 UIApplications beginIgnoringInteractionEvents 和 endIgnoringInteractionEvents 可能会对您有所帮助,当调用 begin 时,来自用户的 UI 交互事件将被忽略,这是一个参考 UIApplication ref

    【讨论】:

    • 非常感谢您的提示!
    • 我已经这样做了:案例0:[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; [UIView beginAnimations:nil context:nil]; { [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; //geschmeidige Bewegeung [UIView setAnimationDuration:10.0f]; [UIView setAnimationDelegate: self]; ani_schwein_gross.transform = CGAffineTransformMakeTranslation (-200, 10); [UIView setAnimationDidStopSelector:@selector(doneSwappingViewsAnimation:finished:context:)]; } [UIView 提交动画];等动画结束 - 任何输入都被禁用。
    • 在 ViewDIdLoad 我插入之前: - (void) doneSwappingViewsAnimation: (NSString*) id finished: (BOOL) finished context: (id) context { [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }
    猜你喜欢
    • 1970-01-01
    • 2014-12-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多