iCarousel导入图片遇到困难

3

我是一名初学者,正在学习并尝试使用Nick Lockwood的iCarousel。我打算将图片放在iCarousel中。以下是我的.m和.h代码:

//.m

@implementation ViewController

@synthesize images;
...

- (void)awakeFromNib
{    
    if (self) {

        //set up carousel data
        //wrap YES = infinite loop
        wrap = YES;

        self.images = [NSMutableArray arrayWithObjects:
                       @"apple.jpg",@"lemon.jpg",@"orange.jpg",@"melon.jpg",@"mango.jpg",nil];       
            }

}


- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{   
    if (view == nil)
{

        view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:index]]];
}
    return view;

}

// .h

@interface ViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>{

NSMutableArray *images;
...
}
@property (nonatomic,retain) NSMutableArray *images;
…
@end

我打算添加30张图片,因此我希望代码易于维护。 我的问题是我想通过NSBundle或应用程序目录获取图像,我尝试了不同的代码,但图像未出现在旋转木马中。谢谢您的帮助。


你设置了委托和数据源吗?我看到很好。 - adali
是的,我已经设置好了。 - Bazinga
那么,你的问题是什么,视图没有显示或只是空白视图? - adali
@adali,是的,我已经实现了那些。我用 [images count] 返回了值。 - Bazinga
@Kobe.o4 是的,我正在尝试从应用程序目录导入。 - Bazinga
显示剩余25条评论
1个回答

1

嘿,这是我在我的iCarousel应用程序中使用的一些代码。您缺少一些方法。所以这里它们是。

#pragma mark iCarousel methods

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

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return 7;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]];
    return view;

}

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

- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]];
    return view;

}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    //usually this should be slightly wider than the item views
    return ITEM_SPACING;
}

- (CGFloat)carousel:(iCarousel *)carousel itemAlphaForOffset:(CGFloat)offset
{
    //set opacity based on distance from camera
    return 1.0f - fminf(fmaxf(offset, 0.0f), 1.0f);
}

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

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return wrap;
}
- (void)carouselDidEndScrollingAnimation:(iCarousel *)aCarousel
{    
    [labelDescription setText:[NSString stringWithFormat:@"%@", [descriptions objectAtIndex:aCarousel.currentItemIndex]]];
}

希望这能有所帮助。

从主捆绑包中怎么样? - Bazinga
在你的viewDidLoad中添加carousel.type = iCarouselTypeCoverFlow2;。 - Mou
已经完成了。我的问题是从NSBundle或相机胶卷上传图像。谢谢。 - Bazinga
2
你不需要大部分的方法,它们是为了在示例应用程序中实现自定义转换而设计的。唯一必需的方法是numberOfItemsInCarousel:和carousel:viewForItemAtIndex:reusingView:,其他所有方法都是可选的。 - Nick Lockwood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接