Search code examples
c#iosxamarin.iosuicollectionviewicarousel

Linear Carousel with wrap enabled


I want to make a carousel that's linear WITH wrap enabled (so you can scroll indefinitely in either direction and it'll just wrap around) They have a CarouselType.Linear, but it isn't wrap enabled. Anyone know of how to do this with either iCarousel or UICollectionViews? I can't find any good resources on how to make a Custom iCarousel layout... Any good references there?

Alliance Carousel

        this.Carousel = new CarouselView(frame)
        {
            DataSource = new CylindericalDataSource(this),
            Delegate = new CylindericalDelegate(this)
        };
        this.Carousel.CarouselType = CarouselType.Linear;
        this.Carousel.ConfigureView();
        this.View.AddSubview(this.Carousel);

------------EDIT--------------

To answer @Rahul's question

    public override void DidScroll (CarouselView carouselView)
    {
        base.DidScroll (carouselView);

        nint numOfItems = carouselView.NumberOfItems;
        if (this._parentController != null &&
            this._parentController._studioViewController != null &&
            this._parentController._studioViewController.SuccessfullyReceivedProjectAndPageData () &&
            this._parentController._studioViewController.HasFlowCardCarouselFinishedLoading () &&
            numOfItems > 0)
        {
            // Enforce min and max values fro ScrollOffset so the user can't scroll the carousel off screen.  This is required when wrapping is turned off. 
            nfloat maxScrollOffset = ((nfloat)numOfItems) - 1f;
            nfloat minScrollOffset = 0f;
            if (carouselView.ScrollOffset < minScrollOffset)
            {
                carouselView.ScrollOffset = minScrollOffset;
            }
            if (carouselView.ScrollOffset > maxScrollOffset)
            {
                carouselView.ScrollOffset = maxScrollOffset;
            }
        }
    }

Solution

  • Figured it out. Override the ValueForOption method and force the CarouseOption.Wrap to be equal to 1.0f. See below

        /*--------------------------------------------------------------------------------*/
        // Class: CylindericalDelegate
        /*--------------------------------------------------------------------------------*/
    
        public class CylindericalDelegate : CarouselViewDelegate
        {
    
            /*--------------------------------------------------------------------------------*/
            // Constructors
            /*--------------------------------------------------------------------------------*/
    
            public CylindericalDelegate()
            {
            }
    
            /*--------------------------------------------------------------------------------*/
            // CylindericalDelegate Implementation
            /*--------------------------------------------------------------------------------*/
    
            public override nfloat ValueForOption(CarouselView carousel, CarouselOption option, nfloat aValue)
            {
                if (option == CarouselOption.Spacing)
                {
                    return aValue * 1.1f;
                }
                if (option == CarouselOption.Wrap)
                {
                    return 1.0f;
                }
                return aValue;
            }
    
            /*--------------------------------------------------------------------------------*/
    
        }
    
        /*--------------------------------------------------------------------------------*/