0

Hifellows,请考虑以下情况:

我有 3 种方法,每种方法都创建和添加一些对象,如图像视图、活动指示器等。

所以想象一下:

    - (void)anim1 {
    // do some verifications like internet reachability. So create imageview1, imageview2, imageview3, activityindicator then add to subView.

    [UIView animateWithDuration:1 delay:0.3 options:0 animations:^{

            // Animate the alpha value of your imageView from 1.0 to 0.0 here
            //...

        } completion:^(BOOL finished) {
            if(![(AppDelegate*)[[UIApplication sharedApplication] delegate] hasConnectivity])
            {
      NSLog(@"No Internet...");
    return;
            }
            else { 
    // If i got internet connection, i create another imageview (checked image) and add to subview. So play with alpha animations then call the second method.

    [UIView animateWithDuration:0.8 delay:0.8 options:0 animations:^{
                    someobject.alpha = 0.0f;
                    [someobject setAlpha:0.5f];
                    [someobject setAlpha:0.5f];

                } completion:^(BOOL finished) {
                    [self performSelector:@selector(anim2) withObject:nil afterDelay:0.1];
                    NSLog(@"Got Internet...");
                }];

                return;
            }
        }];
    }

所以...动画2:

    - (void)anim2 {
    // do some URL connections (POST FORM) with blocks using AFNetworking framework. So, depending on results of URL connection, i work with imageview2, add an activityindicator (while URL processing) then add to subView. To resume, let consider this:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.somepage.com"]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Response OK..");

    // Here i create another imageview (checked image) and add to subview. So play with alpha animations then call the third method.

    [UIView animateWithDuration:0.8 delay:0.8 options:0 animations:^{
                                            someobject.alpha = 0.0f;
                                            [someobject setAlpha:0.5f];
                                            [someobject setAlpha:0.5f];

                                        } completion:^(BOOL finished) {
                                        [self performSelector:@selector(anim3) withObject:nil afterDelay:0.1];
                                        }];

    }

    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Response error. Aborting.");
     }

    }];
        [operation start];

    }

最后,anim3方法:

    - (void)anim3 {
    // Work with imageview3, another activityindicator, then after animation delay, i create a new imageview (checked image). Then enable a uibutton.
    ...

    }

所以,当我启动 anim1 时,它会进行验证,然后按照“序列”,调用 anim2... 等等... Anim1 = OK -> Anim2 = OK -> Anim3 -> Button enabled -> Clicked -> Set所有图像视图都为 alpha = 0.70f。

一切正常,但我创建了另一种方法来“清理”这些视图,然后重新开始。

基本上,它会取消所有选择器,然后从SuperView 中删除所有已创建的图像视图。并且,再次调用 anim1。

请考虑:

    -(void)cleanAnimViews
    {

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(anim1)         object:nil];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(anim2) object:nil];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(anim3) object:nil];

    NSArray* subviews = [[NSArray alloc] initWithArray: scrollview.subviews];
        for (UIView* view in subviews) {
            if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
                [view removeFromSuperview];
            }
        }
        for (UIView* view in subviews) {
            if ([view isKindOfClass:[UIImageView class]] && view.tag == 200) {
                [view removeFromSuperview];
            }
            if ([view isKindOfClass:[UIImageView class]] && view.tag == 201) {
                [view removeFromSuperview];
            }
            if ([view isKindOfClass:[UIImageView class]] && view.tag == 202) {
                [view removeFromSuperview];
            }
            if ([view isKindOfClass:[UIImageView class]] && view.tag == 203) {
                [view removeFromSuperview];
            }
            if ([view isKindOfClass:[UIActivityIndicatorView class]] && view.tag == 204) {
                [view removeFromSuperview];
            }
            if ([view isKindOfClass:[UIImageView class]] && view.tag == 250) {
                [view removeFromSuperview];
            }
        }
        [self performSelector:@selector(anim1) withObject:nil afterDelay:0.8];

    }

我调用viewWillAppear上的 ' cleanAnimViews ' ,并使用RefreshControl 对象我的问题: 当所有任务(认为 anim1、anim2 和 anim3)都已经完成时,然后我使用 pull 调用“cleanAnimViews”进行刷新,它会漂亮地删除所有图像视图,然后从第一个方法(anim1)重新开始。

但是,如果我在 animX 处理期间(在 anim1 到 anim2 或 anim2 到 anim3 之间)调用“cleanAnimViews”来刷新,我会因为“cleanAnimViews”删除所有图像视图而变得一团糟,但“anim1 -> anim2 -> anim3”继续同时处理...所以,我得到了两个(或三个)活动指示器(例如:来自 anim1 和 anim3),弄乱了对象控件,因为它在 anim2(例如)上执行了一个较旧的“进程”,而新的anim1 在同一时间...

让我更好地解释一下:

viewDidAppear ->

开始 anim1: ... 这创建了 3 个图像... 创建 ActivityIndi​​cator ... 验证已完成... 创建一个“已检查”的图像视图 ... 所以它执行选择器 anim2

开始了 anim2... ... 这适用于对象控件,创建 ActivityIndi​​cator,验证已完成... 创建一个“已检查”的图像视图...

---->现在我从拉动到刷新调用“cleanAnimViews” 。但是 anim2 已经执行了选择器 anim3 ......

因此,它删除了所有 imageview 并在方法结束时再次启动 anim1(从 cleanAnimViews),并且由于 anim3 方法已经启动,它再次创建 imageview、activityindicator 和检查图像。并且 anim1 在同一时间再次执行所有过程。 .

当我调用“cleanAnimViews”时,有办法“取消/停止”这些 animX 方法吗?我问这个是因为当视图消失并再次出现时,一切都很漂亮。它停止所有 animX,当再次出现时再次调用 anim1...

我尝试在“cleanAnimViews”上创建一个 BOOL 标志,然后在 animX 方法上使用它..但不起作用..

对不起,很长的帖子,错别字,如果我不太清楚......但我不知道了。

提前致谢。

4

1 回答 1

0

是的,有一种方法可以取消您的所有动画,someObject因此您必须调用[someObject.layer removeAllAnimations];.

PS不要忘记添加QuartzCore,以便您可以访问layer.

于 2013-07-04T23:36:06.650 回答