2012-05-14 70 views
3

蘋果初學者在這裏。我正在學習和嘗試尼克洛克伍德的iCarousel。我正計劃將圖像放入iCarousel中。因此,這裏是我的.m和.h代碼:在iCarousel導入圖片時遇到困難

// .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或從應用程序目錄獲取圖像,我試過不同的代碼,但圖像不出現在旋轉木馬上。謝謝你的幫助。

+0

您是否設置了委託和數據源?這是很好,因爲我看到 – adali

+0

是的,我已經設置它。 – Bazinga

+0

所以你有什麼問題,沒有意見或只是空的意見? – adali

回答

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]]]; 
} 

希望這會有所幫助。

+0

如何從主包? – Bazinga

+0

O ya並添加carousel.type = iCarouselTypeCoverFlow2;在你的viewDidLoad – Mou

+0

已經完成。我的問題是從NSBundle或相機膠捲上傳圖像。謝謝 – Bazinga