Fade edges of UITableView

后端 未结 4 1920
情深已故
情深已故 2020-12-05 09:20

I\'ve made a little research about my problem and unfortunately there was no solution for my problem. The closest was Fade UIImageView as it approaches the edges of a UIScro

4条回答
  •  感情败类
    2020-12-05 09:29

    This is my version of the fading table view by inheriting UITableView. Tested for iOS 7 & 8.

    1. There is no need to implement scrollViewDidScroll, layoutSubviews can be used instead.
    2. By observing for bounds changes, the mask is always properly placed when the rotation changes.
    3. You can use the percent parameter to change how much fading you want at the edges, I find a small value looking better in some cases.

    CEFadingTableView.m

    #import "CEFadingTableView.h"
    
    @interface CEFadingTableView()
    
    @property (nonatomic) float percent; // 1 - 100%
    
    @end
    
    @implementation CEFadingTableView
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
    
        self.percent = 5.0f;
    
        [self addObserver:self forKeyPath:@"bounds" options:0 context:nil];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if(object == self && [keyPath isEqualToString:@"bounds"])
        {
            [self initMask];
        }
    }
    
    - (void)dealloc
    {
        [self removeObserver:self forKeyPath:@"bounds"];
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        [self updateMask];
    }
    
    - (void)initMask
    {
        CAGradientLayer *maskLayer = [CAGradientLayer layer];
    
        maskLayer.locations = @[@(0.0f), @(_percent / 100), @(1 - _percent / 100), @(1.0f)];
    
        maskLayer.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
        maskLayer.anchorPoint = CGPointZero;
    
        self.layer.mask = maskLayer;
    
        [self updateMask];
    }
    
    - (void)updateMask
    {
        UIScrollView *scrollView = self;
    
        CGColorRef outer = [UIColor colorWithWhite:1.0 alpha:0.0].CGColor;
        CGColorRef inner = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
    
        NSArray *colors = @[(__bridge id)outer, (__bridge id)inner, (__bridge id)inner, (__bridge id)outer];
    
        if(scrollView.contentOffset.y <= 0) // top
        {
            colors = @[(__bridge id)inner, (__bridge id)inner, (__bridge id)inner, (__bridge id)outer];
        }
        else if((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) // bottom
        {
            colors = @[(__bridge id)outer, (__bridge id)inner, (__bridge id)inner, (__bridge id)inner];
        }
    
        ((CAGradientLayer *)scrollView.layer.mask).colors = colors;
    
        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        scrollView.layer.mask.position = CGPointMake(0, scrollView.contentOffset.y);
        [CATransaction commit];
    }
    
    @end
    

提交回复
热议问题