Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView transparent gradient

Given an arbitrary UIView on iOS, is there a way using Core Graphics (CAGradientLayer comes to mind) to apply a "foreground-transparent" gradient to it?

I can't use a standard CAGradientLayer because the background is more complex than a UIColor. I also can't overlay a PNG because the background will change as my subview is scrolled along its parent vertical scrollview (see image).

I have a non-elegant fallback: have my uiview clip its subviews and move a pre-rendered gradient png of the background as the parent scrollview is scrolled.

transparent uiview gradient problem

like image 241
jpsim Avatar asked Jun 01 '12 14:06

jpsim


1 Answers

This was an embarrassingly easy fix: apply a CAGradientLayer as my subview's mask.

CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = _fileTypeScrollView.bounds; gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil]; gradientLayer.startPoint = CGPointMake(0.8f, 1.0f); gradientLayer.endPoint = CGPointMake(1.0f, 1.0f); _fileTypeScrollView.layer.mask = gradientLayer; 

Thanks to Cocoanetics for pointing me in the right direction!

like image 118
jpsim Avatar answered Sep 22 '22 09:09

jpsim