I've got a UIScrollView in my app and I have seen in some other apps that when the user scrolls, the top section fades out on scroll rather than just dissapearing out.
I really love this effect and want to achieve it. Any ideas how it can be done?
I've got a UIScrollView in my app and I have seen in some other apps that when the user scrolls, the top section fades out on scroll rather than just dissapearing out.
I really love this effect and want to achieve it. Any ideas how it can be done?
You can use a
CAGradientLayer
byAdding the QuartzCore.framework to your project (see Linking to Library or Framework).
Add
#import
of the QuartzCore headers:And then use
CAGradientLayer
:Note, this
CAGradientLayer
is a gradient from a color with alpha of 0.0 (e.g.clearColor
) to a color to a color with alpha of 1.0 (e.g.whiteColor
), not just from black to white. You can adjust thestartPoint
(the default value is probably fine) and theendPoint
to adjust where you want the gradient to be applied.And generally, when doing this with a
UIScrollView
, unless you want the gradient to scroll with you, you make theUIScrollView
a subview of some otherUIView
and apply this gradient to that container view, not the scroll view itself.You add an alpha mask layer to a view containing your scroll view like this:
where "scrollMask" is a grayscale image defining mask region: white == fully masked, black == not masked at all and gray == partially masked.
To create the effect you're looking for, the mask image would be black with a white gradient at the top like this:
For more details, take a look at the documentation for
CGImageMaskCreate
.We built onto Steph Sharp's code to only fade when necessary. Our code is setup as a static utility method instead of a subclass so that we could reuse the method in our subclasses of UIScrollView and UITableView. Here is our static utility method:
Here is our usage of that method.
FadingScrollView.h
FadingScrollView.m
FadingTableView.h
FadingTableView.m
Thanks to Fattie's answer I created the following UIView extension, in swift, that takes care of the gradient fading and provides more styles (bottom, top, left right, vertical and horizontal)
The Extension:
For any comments/recommendations, please let me know at the gist I've created.
EDIT: I've put this code up on github, see here.
See my answer to a similar question.
My solution is to subclass
UIScrollView
, and create a mask layer in thelayoutSubviews
method.The code above fades the top and bottom of the
UIScrollView
from the background colour to transparent, but this can be easily changed to fade the top only (or fade to any colour you want).Change this line to fade the top only:
EDIT 2:
Or fade the top only by changing these two lines:
Or, fade the bottom only:
Just based on Rob's great answer, here's a category on UIView,
which does this .. seen here on a small UIWebView ...
(To be clear, the web view itself is white; the web view happens to be sitting on a greyish background. So, the fadeTail function basically fades out the "whole" web view, to whatever happens to be underneath.)
Hope it saves someone a little typing, cheers