How can we change the fade color in iCarousel?

坚强是说给别人听的谎言 提交于 2019-12-06 11:18:00

Fade in and fade out are controlled by the delegate method:

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value;

This method is used to customise the parameters of the standard carousel types. By implementing this method, you can tweak options such as:

iCarouselOptionFadeMin
iCarouselOptionFadeMax
iCarouselOptionFadeRange

These three options control the fading out of carousel item views based on their offset from the currently centered item. FadeMin is the minimum negative offset an item view can reach before it begins to fade. FadeMax is the maximum positive offset a view can reach before if begins to fade. FadeRange is the distance over which the fadeout occurs, measured in multiples of an item width (defaults to 1.0).

So for example fade out from 0.5f to 2.5f:

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
    switch (option) {
        case iCarouselOptionFadeMax:
            return 0.5f;
            break;
        case iCarouselOptionFadeMin:
            return -0.5f;
            break;
        case iCarouselOptionFadeRange:
            return 2.5f;
            break;

        default:
            return value;
            break;
    }
}

Now- to apply the blue color to fade-in and fade-out image you could subclass iCarousel. The following example assumes that as an image fades out (alpha approaches 0.0f), the blue color overlay fades in (alpha approaches 1.0f) using a cosine function.

#import "iCarousel.h"

// Done to suppress compiler warning to super
@interface iCarousel (BlueOverlay)

- (UIView *)containView:(UIView *)view;
- (void)transformItemView:(UIView *)view atIndex:(NSInteger)index;
- (CGFloat)alphaForItemWithOffset:(CGFloat)offset;

@end

@interface BlueCarousel : iCarousel

@end

@implementation BlueCarousel

- (UIView *)containView:(UIView *)view
{    
    //get container frame
    UIView *containerView = [super containView:view];
    CGRect frame = containerView.frame;

    //create the blue overlay layer
    UIView *overlayView = [[UIView alloc] initWithFrame:frame];
    overlayView.backgroundColor = [UIColor blueColor];
    overlayView.alpha = 0.0f;
    [containerView addSubview:overlayView];

    return containerView;
}

- (void)transformItemView:(UIView *)view atIndex:(NSInteger)index
{
    [super transformItemView:view atIndex:index];

    //calculate offset
    CGFloat offset = [self offsetForItemAtIndex:index];

    //update blue overlay alpha e.g. using cosine function
    CGFloat alpha = cosf(M_PI_2*[self alphaForItemWithOffset:offset]);
    [(UIView*)[view.superview.subviews lastObject] setAlpha:alpha];
}

@end 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!