微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有contentOffset 问题代码

如何解决具有contentOffset 问题代码

问题

我创建了一个UICollectionViewFlowLayout,它使用顶部的粘性行和左侧的粘性行。使用这种布局,我还向collectionView添加了捏手势以启用缩放。

当collectionView没有一直滚动到右边或底部时,缩放效果最佳。如果为真,则contentOffset.xcontentOffset.y的行为异常。

有人可以发现问题吗?我已经尝试调试此问题已有一段时间了。 这是一个gif表示形式:

enter image description here

代码

网格

- (void)actionPinchGesture:(UIPinchGestureRecognizer *)gesture {
   
   MyCustomLayout *layout = (MyCustomLayout *)self.gridView.collectionViewLayout;
   
   if (gesture.state == UIGestureRecognizerStateBegan) {
      [layout setPrevIoUsPinchValue:gesture.scale];
   }
 
   if (gesture.state == UIGestureRecognizerStateChanged) {
      [layout setPinchValue:gesture.scale];
      [layout updatePinchValues];
   }
   
}

MyCustomLayout.h

@property (nonatomic,assign) prevIoUsPinchValue;
@property (nonatomic,assign) pinchValue;
- (void)updateGridScale;

MyCustomLayout.m

@interface MyCustomLayout() {

    CGFloat zoomLevel;              //  stored zoom level.
    CGFloat cachedSectionWidth;     //  stored original section width.
    CGFloat cachedColumnWidth;      //  stored original column width.
    CGFloat cachedRowHeight;        //  stored row height.

    // these values above are cached when the grid is setup for the first time

}
@end

@implementation MyCustomLayout

- (void)updateGridScale {
   
    float scaleDeltaFactor     =   self.pinchValue / self.prevIoUsPinchValue;
    float currentZoom          =   self->zoomLevel;
    float newZoom              =   currentZoom * scaleDeltaFactor;
    float kMaxZoom             =   2.0;
    float kMinZoom             =   0.47;

    newZoom                    =   MAX(kMinZoom,MIN(newZoom,kMaxZoom));

    self->zoomLevel            =   newZoom;
    self.prevIoUsPinchValue    =   self.pinchValue;

    CGFloat sectionWidth       =   self->cachedSectionWidth;
    CGFloat columnWidth        =   self->cachedColumnWidth;
    CGFloat rowHeight          =   self->cachedRowHeight;

    sectionWidth = sectionWidth * newZoom;
    columnWidth = columnWidth * newZoom;
    rowHeight = rowHeight * newZoom;
    
    if (newZoom == 1 || newZoom == 0) {
        sectionWidth = self->cachedSectionWidth;
        columnWidth = self->cachedColumnWidth;
    }
    
    self.topColumnWidth = columnWidth;
    self.sectionHeight = rowHeight;
    [self invalidateLayoutCache];
    [self.collectionView reloadData];

}

- (void)prepareLayout {
    
    [super prepareLayout];
    [self prepareAttributesForRows:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,self.collectionView.numberOfSections)]];
    [self.allAttributes addobjectsFromArray:self.sectionAttributes];
    
}

- (void)prepareAttributesForRows:(NSIndexSet *)sectionIndexes {
    
    NSIndexPath *sectionIndexPath           =   [NSIndexPath indexPathForItem:0 inSection:section];
    CGFloat sectionMinY                     =   self.hourHeaderHeight + self.contentMargin.top;
    CGFloat sectionMinX                     =   (self.collectionView.contentOffset.x - 0.5) - self.collectionView.contentInset.left;
    CGFloat sectionY                        =   sectionMinY + ((self.sectionHeight + self.sectionGap) * section);
    CGFloat height                          =   self.sectionHeight + 1;
    
    UICollectionViewLayoutAttributes *atts  =   [self layoutAttributesForSupplementaryViewAtIndexPath:sectionIndexPath ofKind:INSEPGLayoutElementKindSectionHeader withItemCache:self.sectionHeaderAttributes];
    atts.frame                              =   CGRectMake(sectionMinX,sectionY,self.sectionHeaderWidth + 1.5,height);
    atts.zIndex                             =   [self zIndexForElementKind:INSEPGLayoutElementKindSectionHeader floating:YES];
    
    Nsstring *indexPathString               =   [Nsstring stringWithFormat:@"%li",sectionIndexPath.section];
    NSNumber *y_value                       =   [NSNumber numberWithFloat:round(sectionY)];

    if (![[self.cachedSectionInfo allKeys] containsObject:indexPathString]) {
        [self.cachedSectionInfo setobject:y_value forKey:indexPathString];
    }
    
}

@end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。