2013-01-22 35 views
9

Tôi muốn iCarousel bắt đầu xuất hiện từ bên trái, tức là nó phải được căn trái. Tôi đã thấy rằng trong trường hợp của Carousel tuyến tính nó bắt đầu từ trung tâm của màn hình.Loại tuyến tính iCarousel bắt đầu từ trung tâm

Làm cách nào để làm cho Băng chuyền tuyến tính bắt đầu từ bên trái?

Trả lời

7

Trong trường hợp của tôi, tôi đã nhờ việc sử dụng các phương pháp [self.vwCoverFlow scrollToItemAtIndex:2 animated:YES]; trong viewDidLoad của tôi .

+0

Cảm ơn .. làm việc tốt. – Sudhakar

+0

bạn tiết kiệm người đàn ông ngày của tôi :) Cảm ơn! – Irfan

+0

Nhưng thao tác cuộn băng chuyền này ở chỉ mục thứ 2 không hiển thị mục đầu tiên từ cực bên trái? –

4

Nếu bạn muốn cuộn tuyến tính, bạn có thể sử dụng lớp swipeView, nó là từ cùng một nhà phát triển như iCarousel nhưng chỉ cho phép cuộn tuyến tính. Tôi đã sử dụng nó và nó hỗ trợ căn trái. của nó dễ dàng để thêm và sử dụng, giống như iCarousel

đây là liên kết cho mã https://github.com/nicklockwood/SwipeView

0

trong trường hợp của bạn, tôi muốn đề nghị sử dụng 2 placeholder.

Ví dụ: bạn có n ảnh, sau đó bạn sẽ có (n-2) mục và 2 phần giữ chỗ. - Chỉ mục các mục từ 1 đến (n-2). - Chỉ mục của trình giữ chỗ là 0 và (n-1). Bây giờ chúng ta sẽ có một số mã như thế này:

#pragma mark iCarousel DataSource 

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel 
{ 
    //if we have n photos, then number of items is (n-2) 
    int count = _movieGalleries.count; 
    return count >= 3 ? count - 2 : count; 
} 

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel 
{ 
    //2 placeholder 
    return _movieGalleries.count >= 3 ? 2 : 0; 
} 

#pragma mark iCarousel Delegate 
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    //reuse your view 
    UIView *yourview = ... 

    //get your data 
    //index of item is 1 -> n-2, index of placeholder is 0 and n-1 
    Item *item = [_listItems objectAtIndex:index + 1]; 

    //config your view 
    ... 
    return yourView; 
} 

- (UIView*)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    //index of placeholder is 0 and n-1 
    //Trick: we use viewForItemAtIndex for getting placeholder view 
    index = index == 0 ? - 1 : carousel.numberOfItems; 
    return [self carousel:carousel viewForItemAtIndex:index reusingView:view]; 
} 

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index 
{ 

    //in carousel with placeholder, index of selected item is -1, 0, 1, ..., (n-2). which -1 and (n-2) is index of placeholders. So that we need to increase index by 1 
    NSLog("current index: %i", index + 1); 
} 
1

Thiết lập thuộc tính wrap để YES và trả lại số placeholderinCarousel như 2

1

tôi đã cùng một vấn đề và làm điều đó với sau Hack;)

Sự cố: Tôi cần có chế độ xem chỉ hiển thị 3 mục luôn được căn trái.

Độ phân giải: Điều tôi đã làm là luôn tạo ra ít nhất 3 mục. Nếu tôi có 0, 1 hoặc 2 mục tôi luôn tạo 3, nhưng những thứ không cần hiển thị, tôi đang tạo UIView rỗng. Tôi luôn có 2 trình giữ chỗ, nhưng trong một số trường hợp, một hoặc cả hai đều trống.

ví dụ: Nếu chúng tôi có 2 mục để hiển thị, tôi thực sự đang tạo ba mục, nhưng thứ ba là trống UIView. Tôi đang tạo hai trình giữ chỗ và một Mục.

  • mục đầu tiên là đầu tiên placeholder tại index 0
  • mục thứ hai là mục
  • Thứ ba mục là thứ hai placeholder nhưng với UIView trống

Nếu chúng ta có 1 mặt hàng để hiển thị, một lần nữa tôi đang tạo ba, nhưng thứ hai và thứ ba là rỗng UIView. Giống như trong ví dụ trước, tôi đang tạo hai trình giữ chỗ và một Mục.

  • mục đầu tiên là đầu tiên placeholder tại index 0
  • mục thứ hai là mục nhưng với UIView trống
  • Thứ ba mục là thứ hai placeholder nhưng với UIView trống

Vì của logic này tôi luôn luôn làm sạch xem khi tái sử dụng ([v removeFromSuperview]), để chắc chắn nó là sạch sẽ và nếu mới cần phải được hiển thị tôi thêm ing it ([xem addSubview ...). Cùng logic cho mụcPlaceholder

Nếu bạn cần phải có nhiều hơn 3 hiển thị mục bạn có thể sử dụng cùng một logic, nhưng sự thay đổi giá trị từ 3 đến cái gì khác. Nếu tôi sai cập nhật me;)

Đây là một phần của mã tôi làm việc cho tôi;)

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel 
{ 
    return [[self getRecordings] count] > 3? [[self getRecordings] count] - 2: 1; 
} 

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    if (view == nil) 
    { 
     view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; 
    } 
    else 
    { 
     // If reusing remove content from holder view for fake items 
     for (UIView *v in view.subviews) 
     { 
      [v removeFromSuperview]; 
     } 
    } 

    if ([[self getRecordings] count] >= 2) 
    { 
     [view addSubview:[(RecordingItemViewController*)[_recordingItemViewControllers objectAtIndex:index + 1] view]]; 
    } 

    return view; 
} 

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel 
{ 
    return 2; 
} 

- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    if (view == nil) 
    { 
     view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; 
    } 
    else 
    { 
    // If reusing remove content from holder view for fake items 
     for (UIView *v in view.subviews) 
     { 
      [v removeFromSuperview]; 
     } 
    } 

    if (([[self getRecordings] count] > 0 && [[self getRecordings] count] < 3 && index == 0) || [[self getRecordings]count] >= 3) 
    { 
     [view addSubview:[(RecordingItemViewController*)(index == 0? [_recordingItemViewControllers objectAtIndex:0]: [_recordingItemViewControllers lastObject]) view]]; 
    } 

    return view; 
} 
1

này làm việc cho tôi

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{ 

     [carousel scrollToItemAtIndex:photosArray.count/2 animated:YES]; 

     return [photosArray count]; 
     } 
Các vấn đề liên quan