3

苹果初学者在这里。我目前正在学习并尝试 nickLockwood 的 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 或从应用程序目录获取图像,我尝试了不同的代码,但图像没有出现在轮播中。谢谢你的帮助。

4

1 回答 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]]];
}

希望这可以帮助。

于 2012-05-23T08:22:19.310 回答