我们如何改变iCarousel中的淡化色?

时间:2013-01-25 06:46:28

标签: icarousel

假设我想将蓝色应用于淡入和淡出图像,我该如何实现?

1 个答案:

答案 0 :(得分:4)

淡入和淡出由委托方法控制:

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

此方法用于自定义标准轮播类型的参数。通过实施此方法,您可以调整选项,例如:

iCarouselOptionFadeMin
iCarouselOptionFadeMax
iCarouselOptionFadeRange

这三个选项根据轮播项目视图与当前居中项目的偏移量来控制轮播项目视图的淡出。 FadeMin是项目视图在开始淡入之前可以达到的最小负偏移量。 FadeMax是视图开始淡入之前可以达到的最大正偏移量。 FadeRange是淡出发生的距离,以项目宽度的倍数(默认为1.0)来衡量。

因此,例如从0.5f淡出到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;
    }
}

现在 - 将蓝色应用于淡入和淡出图像,您可以将iCarousel子类化。以下示例假设当图像淡出(alpha接近0.0f)时,蓝色覆盖使用余弦函数淡入(alpha接近1.0f)。

#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