问题
Suppose, I want to apply blue color to fade-in and fade-out image, how can i achieve this?
回答1:
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
来源:https://stackoverflow.com/questions/14516746/how-can-we-change-the-fade-color-in-icarousel