nicklockwood / iCarousel

A simple, highly customisable, data-driven 3D carousel for iOS and Mac OS
http://www.charcoaldesign.co.uk/source/cocoa#icarousel
Other
12k stars 2.58k forks source link

Autoplay #837

Open jcamilorm902 opened 7 years ago

jcamilorm902 commented 7 years ago

Hi!

Is there any setting or something like that to set autoplay in icarousel?

Thanks!

Noomie commented 7 years ago

do you mean auto scrolling?

jcamilorm902 commented 7 years ago

I am thinking about auto scrolling automatically, but every 3 seconds, 5 seconds, etc. without clicking, sliding or user interaction. Something like this: grabilla jl5325

Noomie commented 7 years ago

when you create the iCarousel, there should be an autoscroll property you can set. Like this:

_carousel = new iCarousel { Autoscroll = 1.0f, };

1.0f means it scrolls up 1 item per second. Hope this helps!

PS: You should also make sure that you have wrapping enabled. Linear doesn't have wrapping enabled as a default, so you should override the ValueForOption method in the iCarouseldelegate

jcamilorm902 commented 7 years ago

@Noomie Thank you! it is, but i found a couple of things, it does not work when carousel type is iCarouselTypeCoverFlow or iCarouselTypeLinear. And it does not stop in carousel item when autoscrolling, do you know why?

Noomie commented 7 years ago

@jcamilorm902 As I mentioned, you need to enable wrapping for these carouseltypes. You need to create a customdelegate class for this to add to your carousel.

Here's an example:

public class CustomDelegate : iCarouselDelegate
{
        private const float ShouldWrap = 1.0f;
       public override nfloat ValueForOption(iCarousel carousel, iCarouselOption option, nfloat value)
        {
            if (option == iCarouselOption.Wrap)
            {
                return ShouldWrap;
            }
 }

Then add this to your carousel:

_carousel = new iCarousel
{
Delegate = new CustomDelegate(),
Autoscroll = 1.0f,
};

I know that it's possible to make it stop at each item (there's a pagingEnabled property for this when you create the carousel aswell), but I don't know if it's possible to automatically do this. Maybe try and go through the documentation to see if there's an option for this.

jcamilorm902 commented 7 years ago

@Noomie Thank you!

maodd commented 6 years ago

I found an easier solution, instead using autoscroll which can't work with paging together:

 __weak MyViewController * weakSelf = self;
    [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf.carousel scrollByNumberOfItems:1 duration:0.3];
    }];
hpzerozero commented 6 years ago

Inspired by @maodd , I improved this method, which can automatically slide in the opposite direction when sliding to the end.

    __weak MyViewController * weakSelf = self;
    int index = 1
    [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
            if (weakSelf.currentItemIndex == weakSelf.numberOfItems-1) {
                  index = -1;
            } else if (weakSelf.currentItemIndex == 0)  {
                  index = 1;
            }
             [weakSelf scrollByNumberOfItems:index duration:0.3];
    }];

or slide to first item

__weak MyViewController * weakSelf = self;
    [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
            if (weakSelf.currentItemIndex == weakSelf.numberOfItems-1) {
                [weakSelf scrollToItemAtIndex:0 duration:0.1];
            } else {
                 [weakSelf scrollByNumberOfItems:1 duration:0.3];
            }
    }];
Senyor-He commented 6 years ago

https://github.com/khoren93/iCarousel