Search code examples
iosswiftobjective-cxamarin.ios

iCarousel iOS with reduced size views on the sides


I am trying to create a circular carousel. I would like to always show 3 at a time. One view is centralized and the other two (the previous and the next) on a reduced size like the image below:

enter image description here

I am working with Xamarin.iOS but a solution in swift and object-c is welcomed.

This is the closest I could get:

enter image description here

I have this inside the iCarouselDelegate

public override nfloat ValueForOption(iCarousel carousel, iCarouselOption option, nfloat value)
{
        switch (option)
        {
            case iCarouselOption.OffsetMultiplier:
                return 2f;

            case iCarouselOption.Wrap:
                return 1.5f;

            case iCarouselOption.VisibleItems:
                return count;

            case iCarouselOption.Spacing:
                return 2f;

            default:
                return value;
        }
}

and also:

ViewCarousel.Type = iCarouselType.Rotary;


Solution

  • I have managed to solve my issue with the following line of code:

        public override CATransform3D ItemTransformForOffset(iCarousel carousel, nfloat offset, CATransform3D transform)
        {
            // How much smaller the left and right views should be relative to the center view (expressed as pixels)
            float viewSizeDifference = -100;
    
            // The horizontal spacing between the views (expressed as a multiple of the views width)
            float viewDistance = 1.17f;
    
            float transformZ = (float)(Math.Min(1.0f, Math.Abs(offset)) * viewSizeDifference);
    
            return transform.Translate(offset * carousel.ItemWidth * viewDistance, 0f, transformZ);
        }
    

    but for this to work you also have to change the type to custom:

    ViewCarousel.Type = iCarouselType.Custom;