How to fade all items before currentItem with iCarousel?

1.7k views Asked by At

I'm using iCarousel plugin with custom transformations and I try to make a specific fade effect. I want all items before current item faded but current item and all after not faded. Is there a way to do this with fade options ? I tried but didn't succeed. So I tried to make animations on views of the carousel. This is what i got here :

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    UIView *viewToFadeOut = [carousel itemViewAtIndex:self.carousel.currentItemIndex-1];
    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        viewToFadeOut.alpha = 0.6f;
    } completion:^(BOOL finished) {

    }];

    UIView *viewToFadeIn = [carousel itemViewAtIndex:self.carousel.currentItemIndex];
    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        viewToFadeIn.alpha = 1.0f;
    } completion:^(BOOL finished) {

    }];
}

But it doesn't work exactly as I want because the fadeIn animation starts once the current item changed and so animation starts too late.

Maybe is there a way to implement a method like

- (void)carouselCurrentItemIndexWillChange:(iCarousel *)carousel;

and start the fadeOut animation with a delay ?

3

There are 3 answers

3
Nick Lockwood On BEST ANSWER

If I've understood correctly, you simply need to add this to your delegate:

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    if (option == iCarouselOptionFadeMin)
    {
        return 0.0;
    }
    else if (option == iCarouselOptionFadeMinAlpha)
    {
        return 0.6;
    }
    return value;
}
0
Muhammad Abbas On

If you need to need to fade previous and next element then use that code in delegate.

func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
        switch (option) {
        case .fadeMax: return 0
        case .fadeMin: return 0
        case .fadeMinAlpha: return 0.6
        default: return value
        }
    }
0
Alex Morel On

With swift 3 (have a look at https://medium.com/@arb1nsnmgl/icarousel-walkthrough-swift-3-0-887554155242#.bgs3r7n7b if you're having trouble making iCarousel work with Swift 3)

func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
    if (option == .fadeMin)
    {
        return 0;
    } else if (option == .fadeMinAlpha)
    {
        return 0.3;
    } else if (option == .fadeMax)
    {
        return 0.3;
    }
    return value;
}