私の教育を受けた同輩ともっと同意できませんでしたdahn。
あなたは、細胞は、独自のを追跡するかもしれないことを行うには
...あなたはそれが中央に当たったとき、実際にスクロール位置だけではなく、「変更」とは、細胞に応じて何かをアニメーション化するかもしれない、と述べました位置:
// MyCollectionView's DataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourIdentifier" forIndexPath:indexPath];
cell.observedScrollView = theScrollViewYouWantToTrack;
[cell startObservingPosition];
return cell;
}
は、細胞サイクル
を保持避けるために、弱い
observedScrollView
を保持していることを確認してください:
がインスタンス化のセルにscrollviewを渡します
次に、あなたのセルの.mファイルにKVOオブザーバを登録します。
// MyCollectionViewCell.m
- (void)startObservingPosition {
if (self.observedScrollView) {
[self.observedScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:NULL];
}
}
は...セル割り当てを解除する前にKVOオブザーバを削除するか、他にクラッシュするようにしてください! enhanced KVO observer自分自身を自動的に解放するように構築しました
KVO Observerが起動するたびに(scrollViewがスクロールするとき)、位置を把握してそれに応じて反応させることができます。
この例では、セルが中央にあるときを検出する方法を示していますが、また、あなたはそんなに多くを行うことができます方法を示しています。私のCollectionViewで
// MyCollectionViewCell.m
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"]) {
if (self.observedScrollView) {
CGPoint offset = self.observedScrollView.contentOffset;
CGRect contentViewRect = [self.contentView convertRect:self.contentView.bounds toView:self.observedScrollView];
CGFloat contentViewCenterY = contentViewRect.origin.y + contentView.size.height * 0.5 - offset.y;
if (contentViewCenterY == self.observedScrollView.bounds.height * 0.5) {
// the contentView is in the center of the viewable feed. do your animations...
}
}
}
}
scrollViewDidScrollはどうですか? – danh