首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iCarousel滚动不流畅

iCarousel滚动不流畅
EN

Stack Overflow用户
提问于 2013-07-10 00:51:12
回答 4查看 2.6K关注 0票数 2

我正在使用iCarousel滚动标签。在Simulator上一切正常,但在iPad/iPhone上滚动并不流畅和快速

以下是代码。

CitiesListView.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import "iCarousel.h"

@interface CitiesListView : UIView <iCarouselDataSource, iCarouselDelegate>

@property (nonatomic, retain)  iCarousel *carousel;


@end

CitiesListView.m

代码语言:javascript
复制
#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

并在我的视图控制器alloc/initWithFrame CitiesListView中添加到self.view。

我做错了什么。我需要标签来滚动平滑的例子。

谢谢。

EN

回答 4

Stack Overflow用户

发布于 2013-07-10 01:13:34

在您的setUp方法中,调用以下代码:

代码语言:javascript
复制
carousel.centerItemWhenSelected = YES;

也可以使用carousel.scrollSpeed (值: 0.0到1.0,默认值为1.0,表示最快)来更改滚动速度。

票数 0
EN

Stack Overflow用户

发布于 2013-12-16 19:04:19

使用任何一种方法

索引(UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)索引reusingView:(UIView *)查看

(或)

索引(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)索引reusingView:(UIView *)查看

票数 0
EN

Stack Overflow用户

发布于 2013-12-16 19:17:54

从根本上说,问题在于您试图一次将太多图像加载到内存中(视图数量x每个视图的动画帧数)。

您需要找到一种动态加载框架的方法。试试我的GLView库(https://github.com/nicklockwood/GLView),它包含使用可以实时加载的PVR图像播放动画的说明。GLView库包含一个GLImageView类,它的工作方式与UIImageView类似,但是让我们指定一个要播放的图像文件名数组,而不必预先加载所有图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17553854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档