取消/停止执行方法(选择器) - Objective-C,iOS

时间:2013-07-04 23:00:38

标签: ios objective-c

Hifellows,请考虑以下情况:

我有3种方法,每次创建并添加一些对象,如imageview,activityindicators等。

想象一下:

    - (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;
            }
        }];
    }

所以... anim2:

    - (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时,它会进行验证,然后按照'sequence',调用anim2 ......依此类推...... Anim1 = OK - > Anim2 = OK - > Anim3 - >按钮已启用 - >点击 - >将所有图像视图设置为alpha = 0.70f。

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

基本上,它取消所有选择器,然后从超级视图中删除已创建的所有图像视图。并且,再次调用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)重新开始。

但是,如果我使用pull来调用'cleanAnimViews'来在animX处理期间刷新 (在anim1到anim2之间,或者anim2到anim3之间),我就会搞乱,因为'cleanAnimViews'删除了所有的图像视图,但是'anim1 - > anim2 - > anim3'继续同时处理...所以,我有两个(或三个)activityindicator(例如:来自anim1和anim3),乱用对象控件,因为它在anim2(例如)和新执行旧的'proccess'一对一到anim1同时...

让我更好地解释一下:

viewDidAppear - >

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

开始动画... ...这适用于对象控件,创建ActivityIndi​​cator,验证已完成...创建一个“已检查”的imageview ...

----> 现在我称之为“cleanAnimViews”从拉到刷新。 但anim2已经执行了选择器anim3 ...

因此,它会删除所有的imageview,并在方法结束时再次启动anim1(来自cleanAnimViews),并且由于anim3方法已经启动,它会再次创建imageviews,activityindicator和checked image .. AND anim1再次执行所有进程同一时间..

当我调用'cleanAnimViews'时,有一种'取消/停止'这些animX方法的方法吗? 我问这个是因为当视图消失并再次出现时,一切都很美妙。它会停止所有animX,再次出现时再次调用anim1 ......

我尝试在'cleanAnimViews'上创建一个BOOL标志,然后在animX方法上使用它...但是没有用..

很抱歉很长的帖子,错别字,如果我没那么清楚......但我没有更多的想法。

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

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