/**观察者模式*/
[_headCollection addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:nil];
[_collectionView addObserver:self forKeyPath:@"currentPage" options:NSKeyValueObservingOptionNew context:nil];
/**观察者模式*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//取得滑动的视图变化后的下标
NSInteger selectIndex = [[change objectForKey:@"new"] integerValue]; //0 5
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectIndex inSection:0];
if (object == _headCollection && selectIndex != _collectionView.currentPage) {
//将下边的海报视图滑动到相对象的地方
[_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; // 5
_collectionView.currentPage = selectIndex;
}else if (object == _collectionView && selectIndex != _headCollection.currentPage) {
[_headCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
_headCollection.currentPage = selectIndex;
}
}
观察者模式实现上下一起滑动
最新推荐文章于 2022-11-17 17:32:44 发布