10

I would like iCarousel to start appearing from left i.e it should be left aligned. I have found that in case of Linear Carousel it starts from center of the screen.

How can I make the Linear Carousel to start from left?

Varun Mulloli
  • 628
  • 12
  • 28
Zeeshan Jan
  • 197
  • 3
  • 8

7 Answers7

8

In my case, I was helped by the use of the method [self.vwCoverFlow scrollToItemAtIndex:2 animated:YES]; in my viewDidLoad.

Pavel Rudkouski
  • 156
  • 1
  • 4
4

If you want a linear scrolling , you can use swipeView class , it is from the same developer as iCarousel but for linear scrolling only . I have used it and it do support left alignment . Its easy to add and used, same as iCarousel

here is the link for the code https://github.com/nicklockwood/SwipeView

Ankit
  • 1,686
  • 14
  • 14
1

Set the wrap property to YES and return the number of placeholderinCarousel as 2

Rajarshi
  • 381
  • 3
  • 10
1

I had the same problem and do it with following hack ;)

Problem: I need to have view that display only 3 items that are always left aligned.

Resolution: What I have done is to always make at least 3 items. If I have 0, 1 or 2 items I'm always creating 3, but those that do not need to be shown I'm creating as empty UIView. I always have 2 placeholders, but in some cases one or both are empty.

e.g. If we have 2 items to display, I'm actually creating three, but third one is empty UIView. I'm creating two placeholders and one Item.

  • First item is first placeholder at index 0
  • Second item is Item
  • Third item is second placeholder but with empty UIView

If we have 1 items to display, again I'm creating three, but second and third one are empty UIView. Same as in previous example, I'm creating two placeholders and one Item.

  • First item is first placeholder at index 0
  • Second item is Item but with empty UIView
  • Third item is second placeholder but with empty UIView

Because of this logic I'm always cleaning view when reusing ([v removeFromSuperview]), to be sure it is clean and if new one needs to be displayed I'm adding it ([view addSubview...). Same logic for Item and Placeholder

If you need to have more than 3 displayed items you can use same logic, but change value 3 to something else. If I'm wrong update me ;)

Here is a part of mine code that works for me ;)

- (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;
}
zvjerka24
  • 1,702
  • 1
  • 21
  • 27
1

This works for me

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

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

        return [photosArray count];
       }
0

in your case, i would like to suggest using 2 placeholder.

Ex: you have n photos, then you will have (n-2) items and 2 placeholders. - Index of items is from 1 to (n-2). - Index of placeholders is 0 and (n-1). Now we will have some code like this:

#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);
}
nahung89
  • 6,156
  • 3
  • 33
  • 32
0

SWIFT 5:

Solution that worked for me: Making carousel width double the screen width and constraining carousel to XAnchor of the screen, later adding contentOffset that is of 0.5 x size of carousel width. It is also important to present carousel in some containerView: UIView and not directly on viewController This works because carousel's first item is centered to the center of carousel. If you make carousel double the size of your view and set the contentOffset to the left (- 0.5 x size of carousel), the center would be at the left side of your view. Here is some example code:

setting carousel size:

carousel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
carousel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
carousel.translatesAutoresizingMaskIntoConstraints = false
carousel.heightAnchor.constraint(equalToConstant: 200).isActive = true
carousel.widthAnchor.constraint(equalTo: self.containerView.widthAnchor, multiplier: 2).isActive = true
carousel.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
carousel.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true

setting carousel contentOffset:

carousel.contentOffset = CGSize(width: - containerView.frame.size.width, height: 0)

setting property scrollToItemBoundry , so that your carousel items will not go out of your reach when you scroll heavily to right:

carousel.scrollToItemBoundary = true

later on you add your containerView to your viewController.view:

viewController.view.addSubView(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.centerXAnchor.constraint(equalTo: viewController.view.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: viewController.view.centerYAnchor).isActive = true