In iOS 7, the UIPickerView
s have a nice shadow on the text as it gets closer to the edges:
If I'm using a UIScrollView, is it possible to achieve a similar effect where the text at the edges is slightly shadowed/blended into the background?
In iOS 7, the UIPickerView
s have a nice shadow on the text as it gets closer to the edges:
If I'm using a UIScrollView, is it possible to achieve a similar effect where the text at the edges is slightly shadowed/blended into the background?
See here and here for my answers to similar questions.
My solution was to subclass UIScrollView
, and create a mask layer in the layoutSubviews
method.
The code in the above answers is also here on github.
Hope that helps!
I did a similar thing with this class to fade the left and right edges of a scrolling marquee. Much less hassle than using two images as you can resize or recolor it easily:
@interface MarqueeFadeOverlay : UIView
@property(assign, nonatomic) CGGradientRef gradientLayer;
- (id)initWithFrame:(CGRect)frame andEndFadeWidth:(float)fadeWidth andFadeBarColor:(UIColor *)color;
@end
@implementation MarqueeFadeOverlay {
UIColor *fadeBarColor;
float endFadeWidth;
}
- (id)initWithFrame:(CGRect)frame andEndFadeWidth:(float)fadeWidth andFadeBarColor:(UIColor *)color {
self = [super initWithFrame:frame];
if (self) {
endFadeWidth = fadeWidth;
fadeBarColor = color;
[self setOpaque:NO];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLinearGradient(context, self.gradientLayer, CGPointMake(0.0, 0.0),
CGPointMake(self.frame.size.width, 0.0), kCGGradientDrawsBeforeStartLocation);
}
- (CGGradientRef)gradientLayer {
if (_gradientLayer == nil) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat *colorComponents = [self rgbaFrom:fadeBarColor];
if (endFadeWidth == 0) endFadeWidth = 0.1;
CGFloat locations[] = {0.0, endFadeWidth, 1.0 - endFadeWidth, 1.0};
CGFloat colors[] = {colorComponents[0], colorComponents[1], colorComponents[2], 1.00,
colorComponents[0], colorComponents[1], colorComponents[2], 0,
colorComponents[0], colorComponents[1], colorComponents[2], 0,
colorComponents[0], colorComponents[1], colorComponents[2], 1.00};
_gradientLayer = CGGradientCreateWithColorComponents(colorSpace, colors, locations, sizeof(colors) / (sizeof(colors[0]) * 4));
CGColorSpaceRelease(colorSpace);
}
return _gradientLayer;
}
-(CGFloat *)rgbaFrom:(UIColor *)color {
CGColorRef colorRef = [color CGColor];
return (CGFloat *) CGColorGetComponents(colorRef);
}
You just init one with a frame, a fade width between 0 and 1 and a color to match the background, and add to the parent view.
I would place two custom overlay views for this that you place on top of the scroll view. The views could either contain a pre-made image with a gradient from full white to an alpha of 0, or you could draw the image in code, either with Core Graphics or a CAGradientLayer
.
If you set userInteractionEnabled = NO;
on the views, they won't interfere with the touch handling.