UIScrollView 嵌套一

打印 上一主题 下一主题

主题 732|帖子 732|积分 2196

关于这个嵌套滑动题目,苹果并不保举嵌套滚动视图,如果直接添加的话,就会出现手势的冲突造成了体验上的悲剧。目前网上大部分的方法都是本身写手势监听,实现起来太过繁琐,近来碰到了同样的题目,写了一个比较简单的方法
通过scrollView的代理方法
- (void)scrollViewDidScrollUIScrollView *)scrollView
来控制内外嵌套scrollView的滚动 以及滚动到目标位置后 把后续的滚动传递给另一个scrollView 制止滚动停止的生硬。
不过这里在目标节点处 拖拽时会存在中断的情况 需要优化
  1. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
复制代码
dome实现代码
@interface ScrollViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong)UIScrollView *outScrollerView;
@property(nonatomic, strong)UIScrollView *inerScrollerView;
@end
@implementation ScrollViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.outScrollerView.frame = CGRectMake(10, 90, 300, 500);
    self.outScrollerView.contentSize = CGSizeMake(300, 1800);
    [self.view addSubview:self.outScrollerView];
    
    self.inerScrollerView.frame = CGRectMake(10, 440, 240, 300);
    self.inerScrollerView.contentSize = CGSizeMake(240, 800);
    [self.outScrollerView addSubview:self.inerScrollerView];
    self.inerScrollerView.scrollEnabled = NO;
    UIView *v1 = [UIView new];
    v1.frame = CGRectMake(90, 0, 90, 90);
    v1.backgroundColor = UIColor.yellowColor;
    [self.inerScrollerView addSubview:v1];
    UIView *v2 = [UIView new];
    v2.frame = CGRectMake(90, 90, 90, 90);
    v2.backgroundColor = UIColor.redColor;
    [self.inerScrollerView addSubview:v2];
    
}
// 目标位置
CGFloat outContentOffsetY = 430;
CGFloat inerContentOffset2Y = 60;
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScrollUIScrollView *)scrollView {
    CGPoint contentOffset = scrollView.contentOffset;
//    NSLog(@"scrollViewDidScroll");
    if (scrollView == self.outScrollerView && contentOffset.y >= outContentOffsetY) {
        self.inerScrollerView.scrollEnabled = YES;
        self.outScrollerView.scrollEnabled = NO;
        
        // 外层滚动到目标位置后 把滚动距离 传给内存 scrollView
        CGFloat offsetY = contentOffset.y - outContentOffsetY;
        CGPoint contentOffset2 = self.inerScrollerView.contentOffset;
        contentOffset2.y += offsetY;
        
        [self.outScrollerView setContentOffset:CGPointMake(0, outContentOffsetY)];
        [self.inerScrollerView setContentOffset:contentOffset2];
        
    }
    if (scrollView == self.inerScrollerView && contentOffset.y < inerContentOffset2Y) {
        self.outScrollerView.scrollEnabled = YES;
        self.inerScrollerView.scrollEnabled = NO;
        // 内外层滚动到目标位置后 把滚动距离 传给外层 scrollView
        CGFloat offsetY = contentOffset.y - inerContentOffset2Y;
        CGPoint contentOffset1 = self.outScrollerView.contentOffset;
        contentOffset1.y += offsetY;
        
        [self.inerScrollerView setContentOffset:CGPointMake(0, inerContentOffset2Y)];
        [self.outScrollerView setContentOffset:contentOffset1];
        
    }
}
- (UIScrollView *)outScrollerView {
    if (!_outScrollerView) {
        _outScrollerView = [[UIScrollView alloc] init];
        _outScrollerView.delegate = self;
        _outScrollerView.backgroundColor = UIColor.blueColor;
    }
    return _outScrollerView;
}

- (UIScrollView *)inerScrollerView {
    if (!_inerScrollerView) {
        _inerScrollerView = [[UIScrollView alloc] init];
        _inerScrollerView.delegate = self;
        _inerScrollerView.backgroundColor = UIColor.grayColor;
    }
    return _inerScrollerView;
}
@end
  1. #import "ScrollViewController.h"
  2. @interface ScrollViewController ()<UIScrollViewDelegate>
  3. @property(nonatomic, strong)UIScrollView *outScrollerView;
  4. @property(nonatomic, strong)UIScrollView *inerScrollerView;
  5. @end
  6. @implementation ScrollViewController
  7. - (void)viewDidLoad {
  8.     [super viewDidLoad];
  9.    
  10.     self.outScrollerView.frame = CGRectMake(10, 90, 300, 500);
  11.     self.outScrollerView.contentSize = CGSizeMake(300, 1800);
  12.     [self.view addSubview:self.outScrollerView];
  13.    
  14.     self.inerScrollerView.frame = CGRectMake(10, 440, 240, 300);
  15.     self.inerScrollerView.contentSize = CGSizeMake(240, 800);
  16.     [self.outScrollerView addSubview:self.inerScrollerView];
  17.     self.inerScrollerView.scrollEnabled = NO;
  18.    
  19.     UIView *v1 = [UIView new];
  20.     v1.frame = CGRectMake(90, 0, 90, 90);
  21.     v1.backgroundColor = UIColor.yellowColor;
  22.     [self.inerScrollerView addSubview:v1];
  23.    
  24.     UIView *v2 = [UIView new];
  25.     v2.frame = CGRectMake(90, 90, 90, 90);
  26.     v2.backgroundColor = UIColor.redColor;
  27.     [self.inerScrollerView addSubview:v2];
  28.    
  29. }
  30. // 目标位置
  31. CGFloat outContentOffsetY = 430;
  32. CGFloat inerContentOffset2Y = 60;
  33. #pragma mark - UIScrollViewDelegate
  34. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  35.     CGPoint contentOffset = scrollView.contentOffset;
  36. //    NSLog(@"scrollViewDidScroll");
  37.     if (scrollView == self.outScrollerView && contentOffset.y >= outContentOffsetY) {
  38.         self.inerScrollerView.scrollEnabled = YES;
  39.         self.outScrollerView.scrollEnabled = NO;
  40.         
  41.         // 外层滚动到目标位置后 把滚动距离 传给内存 scrollView
  42.         CGFloat offsetY = contentOffset.y - outContentOffsetY;
  43.         CGPoint contentOffset2 = self.inerScrollerView.contentOffset;
  44.         contentOffset2.y += offsetY;
  45.         
  46.         [self.outScrollerView setContentOffset:CGPointMake(0, outContentOffsetY)];
  47.         [self.inerScrollerView setContentOffset:contentOffset2];
  48.         
  49.     }
  50.     if (scrollView == self.inerScrollerView && contentOffset.y < inerContentOffset2Y) {
  51.         self.outScrollerView.scrollEnabled = YES;
  52.         self.inerScrollerView.scrollEnabled = NO;
  53.         // 内外层滚动到目标位置后 把滚动距离 传给外层 scrollView
  54.         CGFloat offsetY = contentOffset.y - inerContentOffset2Y;
  55.         CGPoint contentOffset1 = self.outScrollerView.contentOffset;
  56.         contentOffset1.y += offsetY;
  57.         
  58.         [self.inerScrollerView setContentOffset:CGPointMake(0, inerContentOffset2Y)];
  59.         [self.outScrollerView setContentOffset:contentOffset1];
  60.         
  61.     }
  62. }
  63. - (UIScrollView *)outScrollerView {
  64.     if (!_outScrollerView) {
  65.         _outScrollerView = [[UIScrollView alloc] init];
  66.         _outScrollerView.delegate = self;
  67.         _outScrollerView.backgroundColor = UIColor.blueColor;
  68.     }
  69.     return _outScrollerView;
  70. }
  71. - (UIScrollView *)inerScrollerView {
  72.     if (!_inerScrollerView) {
  73.         _inerScrollerView = [[UIScrollView alloc] init];
  74.         _inerScrollerView.delegate = self;
  75.         _inerScrollerView.backgroundColor = UIColor.grayColor;
  76.     }
  77.     return _inerScrollerView;
  78. }
  79. @end
复制代码
实现方式二:ScrollView嵌套二

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

灌篮少年

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表