2013-07-09 208 views
2

我正在使用iCarousel滾動標籤。一切工作正常在模擬器上,但在iPad/iPhone上滾動不平穩和快速iCarousel不能平滑滾動

這裏是代碼。

CitiesListView.h

#import <UIKit/UIKit.h> 
#import "iCarousel.h" 

@interface CitiesListView : UIView <iCarouselDataSource, iCarouselDelegate> 

@property (nonatomic, retain) iCarousel *carousel; 


@end 

CitiesListView.m

#import "CitiesListView.h" 

@interface CitiesListView() 


@property (nonatomic, retain) NSMutableArray *items; 

@end 

@implementation CitiesListView 

@synthesize carousel; 
@synthesize items; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self setUp]; 
    } 
    return self; 
} 

- (void)setUp 
{ 
    carousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, 0, 300, 200)]; 
     items = [NSMutableArray arrayWithObjects:@"city1", @"city2", @"city3", @"city4", @"city5", @"city6", nil]; 
    [self addSubview:carousel]; 
    carousel.delegate = self; 
    carousel.dataSource = self; 
    carousel.type = iCarouselTypeRotary; 

} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if ((self = [super initWithCoder:aDecoder])) 
    { 
     [self setUp]; 
    } 
    return self; 
} 

#pragma mark iCarousel methods 

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel 
{ 
    return [items count]; 
} 

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    UILabel *label = nil; 

    //create new view if no view is available for recycling 
    if (view == nil) 
    { 
     view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; 
     ((UIImageView *)view).image = [UIImage imageNamed:@"labelBackground.png"]; 
     view.contentMode = UIViewContentModeCenter; 
     label = [[UILabel alloc] initWithFrame:view.bounds]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textAlignment = UITextAlignmentCenter; 
     label.font = [label.font fontWithSize:26]; 
     label.tag = 1; 
     [view addSubview:label]; 
    } else { 
     //get a reference to the label in the recycled view 
     label = (UILabel *)[view viewWithTag:1]; 
    } 

    //set item label 
    //remember to always set any properties of your carousel item 
    //views outside of the `if (view == nil) {...}` check otherwise 
    //you'll get weird issues with carousel item content appearing 
    //in the wrong place in the carousel 
    label.text = [items objectAtIndex:index]; 
    return view; 
} 

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel 
{ 
    //note: placeholder views are only displayed on some carousels if wrapping is disabled 
    return 2; 
} 

- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    UILabel *label = nil; 

    //create new view if no view is available for recycling 
    if (view == nil) 
    { 
     //don't do anything specific to the index within 
     //this `if (view == nil) {...}` statement because the view will be 
     //recycled and used with other index values later 
     view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; 
     ((UIImageView *)view).image = [UIImage imageNamed:@"labelBackground.png"]; 
     view.contentMode = UIViewContentModeCenter; 

     label = [[UILabel alloc] initWithFrame:view.bounds]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textAlignment = UITextAlignmentCenter; 
     label.font = [label.font fontWithSize:50.0f]; 
     label.tag = 1; 
     [view addSubview:label]; 
    } else { 
     //get a reference to the label in the recycled view 
     label = (UILabel *)[view viewWithTag:1]; 
    } 

    //set item label 
    //remember to always set any properties of your carousel item 
    //views outside of the `if (view == nil) {...}` check otherwise 
    //you'll get weird issues with carousel item content appearing 
    //in the wrong place in the carousel 
    label.text = (index == 0)? @"[": @"]"; 

    return view; 
} 

- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform 
{ 
    //implement 'flip3D' style carousel 
    transform = CATransform3DRotate(transform, M_PI/8.0f, 0.0f, 1.0f, 0.0f); 
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth); 
} 

- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value 
{ 
    //customize carousel display 
    switch (option) 
    { 
     case iCarouselOptionSpacing: 
     { 
      //add a bit of spacing between the item views 
      return value * 1.05f; 
     } 
     case iCarouselOptionFadeMax: 
     { 
      if (carousel.type == iCarouselTypeCustom) 
      { 
       //set opacity based on distance from camera 
       return 0.0f; 
      } 
      return value; 
     } 
     default: 
     { 
      return value; 
     } 
    } 
} 

#pragma mark iCarousel taps 

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index 
{ 
    NSNumber *item = [self.items objectAtIndex:index]; 
    NSLog(@"Tapped view number: %@", item); 
} 

@end 

而在我的視圖控制器分配/ initWithFrame CitiesListView並加入到self.view。

我做錯了什麼。我需要標籤像例子一樣平滑滾動。

謝謝。

+0

您可能想要在樂器中剖析您的代碼,以查看是否存在影響應用性能的特定區域。使用Time Profiler工具。有關如何使用樂器的詳細信息,請參閱Apple的文檔:http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Introduction/Introduction.html – stevekohls

+0

當我剖析並使用分配樂器旋轉木馬卷軸光滑。無法找到問題並修復 – Mariam

+0

Allocations會告訴您關於您的內存使用情況,但Time Profiler會讓您知道每種方法執行的時間。這可能有助於找出什麼在減慢你的繪圖。確保你也在設備上進行分析。 – stevekohls

回答

0

裏面你setUp方法,稱之爲:

carousel.centerItemWhenSelected = YES; 

carousel.scrollSpeed也玩(值:0.0〜1.0,默認爲1.0,這意味着最快)來改變滾動速度。

+0

我試過scrollSpeed和centerItemWhenSelected沒有幫助 – Mariam

+0

你在這裏做什麼是可疑的。你是否將標籤添加到imageview?爲什麼不添加一個imageview,然後標籤到UIView本身? Imageview的繪製方式與UIView不同,這可能是繪製滯後的原因。 –

0

使用任一種方法

  • (UIView的*)轉盤:(iCarousel *)轉盤placeholderViewAtIndex:(NSUInteger)指數reusingView:(UIView的*)視圖

(OR)

  • (UIView的*)轉盤:(iCarousel *)轉盤viewForItemAtIndex:(NSUInteger)指數reusingView:(UIView的*)視圖
0

問題根本上在於您試圖一次將太多圖像加載到內存中(視圖數量x每個視圖的動畫幀數)。

您需要找到一種動態加載幀的方法。嘗試使用我的GLView庫(https://github.com/nicklockwood/GLView),其中包含使用可以實時加載的PVR圖像播放動畫的說明。 GLView庫包含一個類似於UIImageView的GLImageView類,但讓我們指定要播放的圖像文件名數組,而不必事先加載所有圖像。

0

這可能會幫助別人。我有類似的問題,其中iCarousel沒有平滑滾動。原來我正在做一些嚴重的驗證(圖像比較)在名爲 - (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel

方法一旦我只保留相關的驗證是輕量級,問題已解決。