亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

targetContentOffsetForProposedContentOffset:

targetContentOffsetForProposedContentOffset:

汪汪一只貓 2019-12-07 15:44:36
我的應用程序中有一個非常簡單的collectionView(僅一行方形縮略圖)。我想攔截滾動,以便偏移量始終在左側保留完整圖像。此刻,它滾動到任何地方,都將留下剪切的圖像。無論如何,我知道我需要使用該功能- (CGPoint)targetContentOffsetForProposedContentOffset:withScrollingVelocity做到這一點,但我只是使用一個標準UICollectionViewFlowLayout。我不是子類化。沒有子類,有沒有辦法攔截它UICollectionViewFlowLayout?
查看完整描述

3 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

好的,答案是否定的,沒有子類化UICollectionViewFlowLayout,就無法做到這一點。


但是,對于以后閱讀此書的任何人來說,將其分類非常容易。


首先,我設置了子類調用MyCollectionViewFlowLayout,然后在界面構建器中,將集合視圖布局更改為“自定義”,然后選擇了流布局子類。


因為您是用這種方式做的,所以您無法在IB中指定項目大小等,因此在MyCollectionViewFlowLayout.m中,我有這個...


- (void)awakeFromNib

{

    self.itemSize = CGSizeMake(75.0, 75.0);

    self.minimumInteritemSpacing = 10.0;

    self.minimumLineSpacing = 10.0;

    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    self.sectionInset = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);

}

這將為我設置所有尺寸以及滾動方向。


然后 ...


- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity

{

    CGFloat offsetAdjustment = MAXFLOAT;

    CGFloat horizontalOffset = proposedContentOffset.x + 5;


    CGRect targetRect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);


    NSArray *array = [super layoutAttributesForElementsInRect:targetRect];


    for (UICollectionViewLayoutAttributes *layoutAttributes in array) {

        CGFloat itemOffset = layoutAttributes.frame.origin.x;

        if (ABS(itemOffset - horizontalOffset) < ABS(offsetAdjustment)) {

            offsetAdjustment = itemOffset - horizontalOffset;

        }

    }


    return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);

}

這樣可以確保滾動在左側邊緣以5.0的邊距結束。


這就是我要做的。我根本不需要在代碼中設置流布局。


查看完整回答
反對 回復 2019-12-07
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

它不能很好地處理用戶輕彈。用戶快速滑動而滾動沒有移動太多的情況下,會出現動畫故障。


我提出的替代實現與以前提出的分頁相同,但是可以處理頁面之間的用戶滑動。


?#pragma mark - Pagination

?- (CGFloat)pageWidth {

? ? ?return self.itemSize.width + self.minimumLineSpacing;

?}


?- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity

?{? ? ? ? ? ?

? ? ? ? CGFloat rawPageValue = self.collectionView.contentOffset.x / self.pageWidth;

? ? ? ? CGFloat currentPage = (velocity.x > 0.0) ? floor(rawPageValue) : ceil(rawPageValue);

? ? ? ? CGFloat nextPage = (velocity.x > 0.0) ? ceil(rawPageValue) : floor(rawPageValue);


? ? ? ? BOOL pannedLessThanAPage = fabs(1 + currentPage - rawPageValue) > 0.5;

? ? ? ? BOOL flicked = fabs(velocity.x) > [self flickVelocity];

? ? ? ? if (pannedLessThanAPage && flicked) {

? ? ? ? ? ? proposedContentOffset.x = nextPage * self.pageWidth;

? ? ? ? } else {

? ? ? ? ? ? proposedContentOffset.x = round(rawPageValue) * self.pageWidth;

? ? ? ? }


? ? ? ? return proposedContentOffset;

?}


?- (CGFloat)flickVelocity {

? ? ?return 0.3;

?}


查看完整回答
反對 回復 2019-12-07
?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

接受答案的Swift版本。


override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

    var offsetAdjustment = CGFloat.greatestFiniteMagnitude

    let horizontalOffset = proposedContentOffset.x

    let targetRect = CGRect(origin: CGPoint(x: proposedContentOffset.x, y: 0), size: self.collectionView!.bounds.size)


    for layoutAttributes in super.layoutAttributesForElements(in: targetRect)! {

        let itemOffset = layoutAttributes.frame.origin.x

        if (abs(itemOffset - horizontalOffset) < abs(offsetAdjustment)) {

            offsetAdjustment = itemOffset - horizontalOffset

        }

    }


    return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y)

}

對Swift 3有效。


查看完整回答
反對 回復 2019-12-07
  • 3 回答
  • 0 關注
  • 2180 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號