2013-07-09 160 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

方法一旦我只保留相关的验证是轻量级,问题已解决。