1

在我的应用程序中,我CATransition用来滑动横幅视图。我的代码是这样的:

ViewDidLoad()

- (void)viewDidLoad
{
    [super viewDidLoad];
    bannerList = [[NSMutableArray alloc] init];
    bannerList = [WebFunctions fetchBannerDataHavingUrl:@"banners/event"];

    NSTimer* timer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

更新标签

- (void)updateLabel {

    BannerDC *bannerObj = [[BannerDC alloc] init];
    if (count == bannerList.count){
        count = 0;
    } else {
        bannerObj = [bannerList objectAtIndex:count];

        lblFeaturedEventTitle.text = bannerObj.line1;
        imgFeaturedEventImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerObj.img_url]]];

        // get the view that's currently showing
        [self.viewBannersBackground removeFromSuperview];
        [self.viewBanners addSubview:self.viewBannersBackground];

        // set up an animation for the transition between the views
        CATransition *animation = [CATransition animation];
        [animation setDuration:1.0];
        [animation setType:kCATransitionPush];
        [animation setSubtype:kCATransitionFromRight];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

        [[self.viewBannersBackground layer] addAnimation:animation forKey:@"SwitchToView1"];

        count++;
    }
}

所以,由于计时器功能,我的主线程在动画期间卡住(或显示延迟)。我想在线程中使用这个动画,我在NSThread和之间感到困惑NSTimer,我应该怎么做?

4

2 回答 2

1

您不应该使用dataWithContentsOfURL:[来下载您的横幅图像,因为它是同步的,并且您之前可能已经下载了该图像并且可以重复使用它。

考虑更改为异步图像管理库,例如 SDWebImage,它将为您处理此问题而不会阻塞主线程(因此您的计时器或动画都不会受到影响)。

于 2014-04-15T07:49:35.903 回答
0

你可以试试:[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]

NSThread : 在后台线程中运行

NSTimer(应该在主线程中调用):在主线程中运行

于 2014-04-15T07:41:33.540 回答