美文网首页
iOS_经验(12)_UIScrollView仿UITableV

iOS_经验(12)_UIScrollView仿UITableV

作者: 丶纳凉 | 来源:发表于2018-08-08 15:41 被阅读60次

一丶某个View悬停

001.gif

二丶实现

ScrollView

@interface ZBRichScrollView ()

@property (nonatomic, strong) NSArray *oriFrameArray;

@end

@implementation ZBRichScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        __weak typeof(self) weakSelf = self;
        [self.KVOController observe:self keyPath:@"contentOffset" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf handleTargetView];
        }];
    }
    return self;
}

- (void)setTargetViews:(NSArray *)targetViews
{
    _targetViews = targetViews;
    NSMutableArray *oriFrameArray = [[NSMutableArray alloc] init];
    for (UIView *subView in targetViews) {
        [oriFrameArray addObject:[NSValue valueWithCGRect:subView.frame]];
    }
    self.oriFrameArray = oriFrameArray;
}

- (void)handleTargetView
{
    CGFloat offsetY = self.contentOffset.y;
    UIView *selectedView = nil;
    NSUInteger idx = 0;
    for (UIView *targetView in self.targetViews) {
        targetView.frame = [self.oriFrameArray[idx] CGRectValue];
        if (!selectedView) {
            CGRect tmpRect = targetView.frame;
            if (CGRectGetMinY(tmpRect) < offsetY) {
                //需要悬停
                tmpRect.origin.y = offsetY;
                targetView.frame = tmpRect;
                selectedView = targetView;
            }
        } else {
            targetView.frame = [self.oriFrameArray[idx] CGRectValue];
            if (CGRectGetMinY(targetView.frame) < CGRectGetMaxY(selectedView.frame)) {
                //准备悬停,让当前悬停的frameMaxY = 下一个的minY;
                CGRect tmpRect = selectedView.frame;
                tmpRect.origin.y = CGRectGetMinY(targetView.frame) - CGRectGetHeight(selectedView.frame);
                selectedView.frame = tmpRect;
            }
            if (CGRectGetMinY(targetView.frame) <= offsetY) {
                //需要悬停
                CGRect headerRect = targetView.frame;
                headerRect.origin.y = offsetY;
                targetView.frame = headerRect;
                selectedView = targetView;
            }
        }
        idx++;
    }
}

@end

三丶使用

@interface ZBRichViewController ()

@property (nonatomic, strong) ZBRichScrollView *richView;
@end

@implementation ZBRichViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"rich";
    
    self.richView = [[ZBRichScrollView alloc] init];
    self.richView.contentSize = CGSizeMake(0, [UIScreen height] * 3);
    [self.view addSubview:self.richView];
    [self.richView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(44);
        } else {
            make.top.offset(44);
        }
        make.left.right.bottom.offset(0);
    }];
    
    NSMutableArray *subView = @[].mutableCopy;
    
    for (NSInteger idx = 0; idx < 10 ; idx++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 100 * idx, [UIScreen width], 44)];
        view.backgroundColor = [UIColor zb_randomColor];
        [self.richView addSubview:view];
        [subView addObject:view];
    }
    
    self.richView.targetViews = subView;
    
}

@end

代码地址

https://github.com/k373379320/ZBCommonUI

相关文章

网友评论

      本文标题:iOS_经验(12)_UIScrollView仿UITableV

      本文链接:https://www.haomeiwen.com/subject/cokxbftx.html