iOS 联动

连动.gif

实现思路##

在做一个页面之前,一定要先思考好应该如何写,方法用对了是可以省很多事的.对于这样的一个连动的页面.我采取的方法是使用两个UICollectionView和若干个UITableView.顶部头是一个UICollectionView,下面滚动页是一个UICollectionView,滚动页里面的每一个cell都包含一个UITableView.滚动的横条用的是CALayer,因为CALayer改变位置后自带隐式动画,可以省去写动画的代码.

头部视图WYTradeHeaderView.h

/// 滚动到了第几页
- (void)slideToIndex:(NSInteger)index;
/// 滚动到几点几
- (void)slideToRatio:(CGFloat)ratio;

头部视图WYTradeHeaderView.m其中的CELL_WIDTH代表的是头部cell的宽度,具体在项目中由自己设置.

#pragma mark - private
- (void)slideToIndex:(NSInteger)index {
    CGRect slideRect = _slideLayer.frame;
    slideRect.origin.x = index * CELL_WIDTH;
    _slideLayer.frame = slideRect;
    
    self.selectedCell = (WLTradeHeadCell *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}

- (void)slideToRatio:(CGFloat)ratio {
    CGRect slideRect = _slideLayer.frame;
    slideRect.origin.x = ratio * CELL_WIDTH;
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    _slideLayer.frame = slideRect;
    [CATransaction commit];
    
    self.selectedCell = (WLTradeHeadCell *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:ratio + 0.5 inSection:0]];
}

#pragma mark - setter
- (void)setSelectedCell:(WLTradeHeadCell *)selectedCell {
    _selectedCell.nameLabel.textColor = WLColorLightGray_text;
    selectedCell.nameLabel.textColor = WLColorBlue;
    
    _selectedCell = selectedCell;
}

底部UICollectionView的主要操作,监听底部视图的滚动,然后实时更改头部视图,让其实现联动

- (void)viewDidLoad {
    [super viewDidLoad];

    [self prepareHeaderView];
}

- (void)prepareHeaderView {
    __weak typeof(_collectionView) weakCollectionView = _collectionView;
    _headView = [WLTradeHeadView tradeHeadViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45) onClick:^(NSInteger index) {
        [weakCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    }];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [_headView slideToRatio:scrollView.contentOffset.x / SCREEN_WIDTH];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.currentPage = scrollView.contentOffset.x / SCREEN_WIDTH;
}

#pragma mark - setter
- (void)setCurrentPage:(NSInteger)currentPage {
    _currentPage = currentPage;
    
    [_headView slideToIndex:currentPage];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 你看这个世界上有多少形状各异的男人女人。 我走过嘈杂的马路,一个低矮的瘦女人叫住我,指着脚下的摊位说:“哎!买个靠...
    流砂阻水阅读 355评论 2 2
  • 一阳说,最近特郁闷,做什么都很烂,很想死。 我说,你该学习了! 以后的半年时间,我再无他的消息。 再见一阳是在市里...
    荒凉一梦阅读 317评论 0 1
  • 那年的她十七岁,身穿碎花白裙最好的季节。 那年的她自卑且敏感,别人如花的年华,而她却像凋落在泥土里的残花,过得狼狈...
    南凉酱阅读 252评论 1 1
  • 好种子开花 1今天斗胆跟老大沟通可否调整一下政策,可以让公司的招生好、客户好、课程顾问都好,在为客户争取更多的福利...
    西红柿的小姑娘阅读 309评论 0 0