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

そしてupdateLabel

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

そのため、タイマー機能により、アニメーション中にメインスレッドが動かなくなります (または遅延が発生します)。このアニメーションをスレッドで使用したいのですが、 と の間で混乱しNSThreadNSTimerいます。どうすればよいですか?

4

2 に答える 2

1

を使用してバナー画像をダウンロードするべきではありませんdataWithContentsOfURL:[。これは同期的であり、以前に画像をダウンロードして再利用できる可能性があるためです。

メインスレッドをブロックせずにこれを処理する SDWebImage のような非同期画像管理ライブラリに変更することを検討してください (したがって、タイマーやアニメーションは影響を受けません)。

于 2014-04-15T07:49:35.903 に答える