2014-04-15 9 views
1

내 앱에서 CATransition을 사용하여 배너보기를 슬라이드합니다. 때문에 내 메인 스레드가 걸리면 타이머 기능으로,iOS - 애니메이션 중에 NSTimer가 앱을 중지시킵니다.

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

그래서

의 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 (또는를 보여줍니다 내 코드는 다음과 같은 것입니다 지연). 스레드에서이 애니메이션을 사용하고 싶습니다. NSThreadNSTimer 사이에 혼란 스럽습니다. 어떻게해야합니까?

답변

1

동 기적이며 이전에 이미지를 이미 다운로드하여 다시 사용할 수 있으므로 배너 이미지를 다운로드하는 데 dataWithContentsOfURL:[을 사용하면 안됩니다.

SDWebImage와 같은 비동기 이미지 관리 라이브러리로 변경하면 주 스레드를 차단하지 않고 처리 할 수 ​​있습니다 (타이머 나 애니메이션이 영향을받지 않음).

+0

감사합니다. 내 하루를 저장했습니다. – NSIceCode

+0

@w ... 링크를 제공해주십시오. 비동기 이미지 관리를 위해이 작업 만 수행하는 경우 imgFeaturedEventImage.image = bannerObj.img_url; NSString에서 'UIImage *'에 할당 된 호환되지 않는 포인터 유형을 표시합니다. 죄송합니다. 전 완전히 멍청한 데요. – NSIceCode

+0

https://github.com/rs/SDWebImage는 아마도 가장 많이 사용되는 솔루션 일 것입니다. 예제 코드의 페이지를 아래로 스크롤하십시오. – Wain

0

당신은 시도 할 수 있습니다 : [NSTimer scheduledTimerWithTimeInterval을 : 목표 : 선택기 : 사용자 정보를 : 반복 :]

NSThread : 백그라운드 스레드

NSTimer (주 스레드에서 호출해야합니다)에서 실행 : 메인 스레드에서 실행