【问题标题】:CALayer inside UICollectionViewCell pinchUICollectionViewCell 内的 CALayer 捏
【发布时间】:2017-07-24 10:38:26
【问题描述】:

我添加了一个GIF 来描述一个问题。我有一个UICollectionView,里面有很多单元格,每个单元格里面都有一个CALayer。我的UICollectionView 中有一个pinch 手势。当它放大时,看起来每个单元格都在放大。查看 gif 上的单元格之间的空格。是否可以将每个单元格一起放大?提前致谢 代码:

细胞子类

@property (nonatomic, strong) CALayer *circularLayer;

- (void)layoutSubviews
{
    [self updateRoundedCorners];
}

- (void)updateRoundedCorners
{
    CGRect bounds = self.bounds;
    self.circularLayer.bounds = bounds;
    self.circularLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
}

#pragma mark - Property Set

- (void)setCellObject:(VenueLayoutCellObject *)cellObject
{
    self->_cellObject = cellObject;
    self.objectBackgroundColor = cellObject.objectColor;
    self.type = cellObject.type;
}

控制器

- (void)didReceivePinchGesture:(UIPinchGestureRecognizer *)gesture
{
    static CGFloat scaleStart;

    if (gesture.state == UIGestureRecognizerStateBegan) {
        scaleStart = self.venueLayoutZoom;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged) {
        self.venueLayoutZoom = scaleStart * gesture.scale;
        [self.activeCollectionView.collectionViewLayout invalidateLayout];
    }
}

更新:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VenueLayoutCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kVenueLayoutCellReuseIdentifier forIndexPath:indexPath];
    self.activeCollectionViewCellsDictionary[indexPath] = cell;
    if (self.activeCollectionViewObjects.count > indexPath.section) {
        NSArray *rows = self.activeCollectionViewObjects[indexPath.section];
        if (rows.count > indexPath.row) {
            if ([rows[indexPath.row] isKindOfClass:[VenueLayoutCellObject class]]) {
                VenueLayoutCellObject *object = rows[indexPath.row];
                cell.cellObject = object;

            }
        }
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    CGFloat widthAndHeight = [self widthAndHeightForActiveCollectionViewItem];
    return CGSizeMake(widthAndHeight *self.venueLayoutZoom, widthAndHeight * self.venueLayoutZoom);
}

- (CGFloat)widthAndHeightForActiveCollectionViewItem
{
    NSArray *array = self.activeCollectionViewObjects;
    __block NSInteger maxCount = 0;
    [array enumerateObjectsUsingBlock:^(NSArray *subArray, NSUInteger idx, BOOL * _Nonnull stop) {
        if (subArray.count > maxCount) {
            maxCount = subArray.count;
        }
    }];
    CGFloat widthAndHeight = CGRectGetWidth(self.activeCollectionView.bounds) / maxCount;
    return widthAndHeight;
}

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell calayer


    【解决方案1】:

    您对图层所做的更改是implicitly animated,这意味着它们是通过动画执行的,即使您没有明确告诉他们这样做。

    由于您正在响应用户手势,因此您不希望更改为动画,因为这会使它们落后于手势。

    您可以像这样在 layoutSubviews 期间关闭隐式动画:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        [CATransaction begin];
        [CATransaction setDisableActions: YES];
        [self updateRoundedCorners];
        [CATransaction commit];
    }
    

    【讨论】:

      【解决方案2】:

      细胞子类

      @property (nonatomic, strong) CALayer *circularLayer;
      
      - (void)layoutSubviews
      {
          [super layoutSubviews];
          [self updateRoundedCorners];
      }
      

      我在这段代码中看到的唯一重要问题是缺少 [super layoutSubviews]; 如果它不起作用,请提供更多代码。

      【讨论】:

      • 你的 cellForItemAtIndexPath 发生了什么?
      猜你喜欢
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多