手动滚动显示相同内容的两个 UICollectionView [英] Manually scrolling two UICollectionViews showing the same content

查看:19
本文介绍了手动滚动显示相同内容的两个 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我有一个 UIViewController 和两个 UICollectionViews,它们以水平方式显示相同的内容.主要的一次显示一张照片,拇指显示几张.

As the title suggest, I have a UIViewController with two UICollectionViews which are displaying the same content in a horizontal fashion. The main one shows one photo at a time, the thumbs one shows several.

我重写了 UIScrollViewDelegate 方法并添加了一些代码,这样当用户滚动主 CV 时,拇指 CV 也会滚动.但是,我也想启用相反的功能(滚动拇指将快速移动主体).但是我得到了反馈效果.

I have overridden the UIScrollViewDelegate method and added some code so that when the user scrolls the main CV, then the thumbs CV scrolls too. However I would like to enable the opposite as well (scroll the thumbs which will quickly move the main). However i'm getting a feedback effect.

这是我的代码片段:

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView == self.mainCollectionView){
        CGFloat x = self.mainCollectionView.contentOffset.x / self.mainCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.thumbsCollectionView.contentOffset = contentOffset;

    }
    else if(scrollView == self.thumbsCollectionView){
//        CGFloat   x = self.thumbsCollectionView.contentOffset.x / self.thumbsCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
//        CGFloat y = 0;
//        CGPoint contentOffset = CGPointMake(x, y);
//        self.mainCollectionView.contentOffset = contentOffset;

    }
}

我想我可以跟踪着陆/上升事件以掩盖允许发生的事情,但在我尝试之前,我想我会看看是否有其他方法可以做到这一点?我是否忽略了提供的可以帮助我的方法?

I imagine that I can track touch down/up events to mask out what's allowed to happen, but before I attempt that I thought I'd see if there is a different way to do this? I am I overlooking a provided method that will help me out here?

谢谢.

解决方案.有一个 UIScrollViewDelegate 方法提供了我需要跟踪哪个布局被触摸.更新代码:

solution. There was a UIScrollViewDelegate method that provided what I needed to track which layout was being touched. Updated code:

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView == self.mainCollectionView &&
       self.scrollingView == self.mainCollectionView){
        CGFloat x = self.mainCollectionView.contentOffset.x / self.mainCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.thumbsCollectionView.contentOffset = contentOffset;

    }
    else if(scrollView == self.thumbsCollectionView &&
            self.scrollingView == self.thumbsCollectionView){
        CGFloat x = self.thumbsCollectionView.contentOffset.x / SM_IPHONE_THUMB_CONTAINER_SIZE * self.mainCollectionView.frame.size.width; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.mainCollectionView.contentOffset = contentOffset;

    }
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.scrollingView = scrollView;
}

推荐答案

scrollViewWillBeginDragging: 被调用.

scrollViewDidScroll:中,更新不拖拽的滚动视图:

In scrollViewDidScroll:, update the scroll view that is not dragging:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView == self.mainCollectionView 
             && self.mainCollectionView == self.scrollingView){ // new check
        CGFloat x = self.mainCollectionView.contentOffset.x / self.mainCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.thumbsCollectionView.contentOffset = contentOffset;

    }
    else if(scrollView == self.thumbsCollectionView 
           && self.thumbsCollectionView== self.scrollingView){ // new check
        CGFloat   x = self.thumbsCollectionView.contentOffset.x / self.thumbsCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.mainCollectionView.contentOffset = contentOffset;

}

这篇关于手动滚动显示相同内容的两个 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆