I want to give my video player a gradient effect at top and bottom of the view. I am using following way for it, but it does not give a smoother effect, i.e. it creates two white horizontal lines at both ending point of the drawing.
Actually, I have created a subclass of UIView
that uses a CAGradientLayer
as its layer. I have done it to make layer follow superview's orientation and it works fine. Check this answer for reference
Here is sub class of UIView
called GradientView
GradientView.h
@interface GradientView : UIView
@property (nonatomic, strong, readonly) CAGradientLayer *layer;
@end
GradientView.m
@implementation GradientView
@dynamic layer;
+ (Class)layerClass {
return [CAGradientLayer class];
}
@end
Instance:
IBOutlet GradientView *viewControls;
Use it:
-(void)applyGradientEffect
{
UIColor *endColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.90];
UIColor *centerColor = [UIColor clearColor];
viewControls.layer.colors = [NSArray arrayWithObjects:
(id)[endColor CGColor],
(id)[centerColor CGColor],
(id)[centerColor CGColor],
(id)[endColor CGColor],
nil];
viewControls.layer.startPoint = CGPointMake(0.5, 1.0);
viewControls.layer.endPoint = CGPointMake(0.5, 0.0);
}
I have also tried with locations
property of CAGradientLayer
instead of startPoint
and endPoint
properties, something like following code snippet. Still it is not smoother enough.
viewControls.layer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.2],
nil];
The gradient effect is smoother only if I use only 2 colors from bottom to top like following code. (It will create gradient effect at bottom edge only, I need such an effect at both top and bottom)
-(void)applyGradientEffect
{
UIColor *endColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.90];
UIColor *centerColor = [UIColor clearColor];
viewControls.layer.colors = [NSArray arrayWithObjects:
(id)[endColor CGColor],
(id)[centerColor CGColor],
nil];
viewControls.layer.startPoint = CGPointMake(0.5, 1.0);
viewControls.layer.endPoint = CGPointMake(0.5, 0.0);
}
What may be the reason? Is it default behavior in this case? Am I doing anything wrong with drawing?