Changing the alpha inside UIScrollView

2019-08-12 19:06发布

问题:

I was trying to achieve the effect of having my text fade in and out to show there is more data in the UIScrollView in this post: UIScrollView, showing that there is more data by fading out text

I could not get the CAGradientLayer to look right since my background isn't a simple black and white background and I couldn't get it to match and make it look like the text is fading. So I thought I'd try setting the alpha property inside the UIScrollView, or the UILabel's alpha inside the UIScrollView. However, it changes all the text's alpha. Is there a way to do it for only a portion of the text, or a portion of the scrollView? Thanks!

回答1:

There are several ways, at least two of which are reasonably easy:

  1. Create a copy of your background with "feathered" edges. Stick this in a view on top of the scroll view.
  2. Set view.layer.maskLayer on an appropriate view (a UIView containing the scroll view should work) to something suitable (e.g. a CALayer with contents/contentsScale/contentsCenter set appropriately). More of a pain, since CALayer does not autosize itself (so your wrapper view will have to do the necessary things in -setFrame:/-setBounds:/-layoutSubviews). You'll need to #include <QuartzCore/CALayer.h>.