Fade Out Scrolling Uitextview Over Image

Fade out scrolling UITextView over image?

Oh man, I use this a lot. Have it saved as a snippet:

CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.frame = self.textView.superview.bounds;
gradient.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor];
gradient.locations = @[@0.0, @0.03, @0.97, @1.0];

self.textView.superview.layer.mask = gradient;

This solution requires that your text view be embedded in a view of its own. This applies a 3% fade to the top and bottom of the text view. Modify gradient.locations to your needs.

fade a UITextView while scrolling uiscrollview

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offset = scrollView.contentOffset.x; // here you will get the offset value
CGFloat value = offset / totalcontentsize;
// use 'value' for setting the textview alpha value
}

I just add logic how to approach.

Or you can use a category. Make a category on UIView called UIView+Looks

@interface UIView (Looks)
-(void)fadeTail;
@end

@implementation UIView (Looks)
-(void)fadeTail
{
// it's used to fade away the bottom,
// perhaps of a slab of text, web view or similar

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = @[
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor clearColor] CGColor]
];
gradient.startPoint = CGPointMake(0.5, 0.93);
gradient.endPoint = CGPointMake(0.5, 1);

[self.layer setMask:gradient];
}
@end

example usage (in this case on a web view

@property (strong) IBOutlet UIWebView *dossierBlock;
-(void)_fillMainArea
{
[self _loadBookImage];

NSString *longHtml = CLOUD.caMeeting[@"yourJsonTag"];
nullsafe(longHtml);

[self.dossierBlock longHtml baseURL:nil];
[self.dossierBlock fadeTail];
}

You can trivially make a "fadeTop" in the same way.

Fade UIImageView as it approaches the edges of a UIScrollView

You can definitely implement something along the lines of #2. It'd be something similar to what the tutorial describes. The alpha transition however won't be as smooth as using the gradient layer mentioned in the tutorial or using an image since the entire icon would have the same alpha. How much discernible the difference is depends on the size of your icons. Smaller icons, very few will be able to tell the difference. Larger icons the difference would be quite clear.

You'd have to implement the

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView

method in your scroll view's delegate class. This method will get called every time the scroll view changes the location of its content. In this method you can call its subviews and adjust their alphas as required. To optimize it a bit instead of calling the alpha adjustments on all the elements you can just update the subviews which are still partially/completely visible.

EDIT: to figure out which views to adjust you'll use the contentOffset property of the scrollView that gets passed as a parameter in the above method.

Gradient effect in UITextView iPhone?

If you apply CAGRradient Layer on UITextView and set [txtView.layer setMask:gradient] this will create problem while scrolling means the gradient layer will scroll.
To overcome from this problem you must use a UiView working as a container view for your textView. first of all add your container view and then add your text view as subview in your container view. and then use this code which save my life.

 CAGradientLayer  gradient = [CAGradientLayer layer];
gradient.frame = view_bottomMenu.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[[UIColor blackColor colorWithAlphaComponent:0.2] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor], (id)[[[UIColor clearColor] colorWithAlphaComponent:1.0] CGColor], (id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],(id)[[[UIColor blackColor] colorWithAlphaComponent:0.3] CGColor],
(id)[[[UIColor blackColor] colorWithAlphaComponent:0.2] CGColor], nil];

gradient.startPoint = CGPointMake(0.0f,0.0f);
gradient.endPoint = CGPointMake(0.0f, 1.0f);

[view_bottomMenu.layer setMask:gradient];

where view_bottomMenu is UIView working as container view.here is my output.
Sample Image

iOS fade to black at top of UIImageView

In your gradient.colors you need to have array of CGColor, so:

gradient.colors = [UIColor.blackColor().CGColor, UIColor.clearColor().CGColor]

Make last few lines of UILabel or UITextView fade out in swift. Please check the attached image

Here is what I have done:

  • I created a UIView with a height of my choice, trailing, leading and bottom constraints to the textView. This is my gradientView which I also use to add additional controls like the 'Read more' button, for example.
  • In viewDidLayoutSubviews() I create a gradient layer for the gradientView. (Note that you need to either remove the layer before creating it or ensure that it is added only once, otherwise your gradient will get darker every time viewDidLayoutSubviews is called)
  • When my text change, I determine if the gradientView should be displayed or not by examining textView.contentSize.height > textView.frame.height

Herewith a Playground example

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {

var gradientView: UIView?
var gradient:CAGradientLayer?

override func loadView() {
let view = UIView()
view.backgroundColor = .gray

let textView = UITextView()
textView.frame = CGRect(x: 20, y: 20, width: 200, height: 300)
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu gravida ligula, vitae venenatis felis. Suspendisse volutpat posuere pretium. Pellentesque quis quam ac velit tincidunt egestas. Phasellus sed scelerisque augue, interdum luctus eros. Praesent non augue eu enim dignissim convallis. Nunc commodo eros quis quam euismod, a malesuada ipsum mattis. Sed sit amet ipsum in justo dictum rutrum a sit amet sem. Sed sit amet mi vel nulla ornare congue. Nam elit ante, aliquam id consequat ac, pretium vel augue. Vivamus hendrerit commodo lectus, vel feugiat mi tempus non. Donec porta, ipsum id porttitor sodales, tortor lectus porta lacus, quis blandit turpis enim at libero. Donec ante est, rutrum quis malesuada a, accumsan at tortor. Nam molestie commodo nulla non suscipit. Nullam pellentesque nunc quam, vitae tempus turpis sollicitudin id. Integer vel varius urna, eleifend eleifend diam."
textView.textColor = .black
view.addSubview(textView)

let gradientViewFrame = CGRect(x:textView.frame.origin.x, y: textView.frame.origin.y + textView.frame.height - 100, width: textView.frame.width, height: 100)
gradientView = UIView(frame:gradientViewFrame)
view.addSubview(gradientView!)
self.view = view
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gradient?.removeFromSuperlayer()
gradient = gradientView?.gradientBackground(from:.white, to: UIColor.white.withAlphaComponent(0), direction: .bottomToTop)
}
}

enum GradientDirection {
case leftToRight
case rightToLeft
case topToBottom
case bottomToTop
}

extension UIView {
func gradientBackground(from color1: UIColor, to color2: UIColor, direction: GradientDirection) -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [color1.cgColor, color2.cgColor]

switch direction {
case .leftToRight:
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
case .rightToLeft:
gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
case .bottomToTop:
gradient.startPoint = CGPoint(x: 0.5, y: 1.0)
gradient.endPoint = CGPoint(x: 0.5, y: 0.0)
case .topToBottom:
gradient.startPoint = CGPoint(x: 0.5, y: 0.0)
gradient.endPoint = CGPoint(x: 0.5, y: 1.0)
default:
break
}

self.layer.insertSublayer(gradient, at: 0)
return gradient
}
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

Fade Effect when Scrolling

You have the code Can you adding the below code and try whether the animation works..

-(void) animateSubViews
{
int buffer = 10;
CGRect frame = CGRectMake((self.pageControl.currentPage * self.scrollView.frame.size.width)-buffer, 0, self.scrollView.frame.size.width + buffer, self.scrollView.frame.size.height);
[UIView animateWithDuration:0.4
animations:^{
for (UIView *view in self.scrollView.subviews)
{
if (CGRectContainsRect(frame, view.frame))
{
[view setAlpha:1.0];
}
else
{
[view setAlpha:0.0];
}
}
}];
}

Try calling this method from ScrollView did scroll and change page.

- (IBAction)changePage:(id)sender {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];

//Call to animate
[self animateSubViews];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;

//Call to animate
[self animateSubViews];
}


Related Topics



Leave a reply



Submit