动画设计学习Html 5+css +JavaScript iOS Developer

一个UIView中的两个UICollectionView如何实现

2016-07-28  本文已影响910人  梦醒繁华
@property(nonatomic,assign)NSInteger currentIndex;

2.并在第一个UICollectionView中的代理方法中将页码传给currentIndex。

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    CGFloat offSetX = targetContentOffset->x;
    CGFloat itemWidth = 80;
    NSInteger pageWidth = itemWidth + 10;
    
    NSInteger pageNum = (offSetX + pageWidth/2)/pageWidth;
    
    targetContentOffset->x = pageWidth*pageNum;
    
    self.currentIndex = pageNum;
}

3.在UIView的.m文件中设置KVO的监听者

[smallCollectionV addObserver:self
                   forKeyPath:@"currentIndex" //监听的属性
                      options:NSKeyValueObservingOptionNew
                      context:nil];

4.在到最后面设置监听

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    NSInteger index = [[change objectForKey:@"new"]integerValue];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0 ];
    [largeCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
}

5.在到第二个UICollectionView的
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
代理方法中写以下代码:

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    CGFloat offSet = targetContentOffset->x;
    CGFloat width = KScreenWidth -100;
    
    NSInteger pageWidth = width + 50;
    NSInteger pageNum = (offSet + pageWidth/2)/pageWidth;
    targetContentOffset->x = pageWidth*pageNum;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageNum inSection:0 ];
    
    [smallCollectionV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

}
13.gif
上一篇 下一篇

猜你喜欢

热点阅读