ios - UICollectionViewCell 捏合中的 CALayer

标签 ios objective-c uicollectionview uicollectionviewcell calayer

我添加了一个 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;
}

Controller

- (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;
}

最佳答案

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

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

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

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

关于ios - UICollectionViewCell 捏合中的 CALayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45278399/

相关文章:

swift - XLPagerTabStrip 中的 Collectionview

ios - Facebook Scores API 不发布开放图表故事

android - 适用于移动设备的简单 HTML5 应用程序

ios - 如何检查 UIImageView 是否与任何其他 View 发生冲突?

iOS - 硬编码字体/字体大小不好吗?

ios - 廉价鲨鱼 API : reading NSDictionary without a key

iphone - 存储 NSAttributedString 核心数据

ios - 如何在 animateWithDuration block 期间利用 UIView 的变化值?

ios - 如何在 UICollectionView 中居中行?

ios - Swift:如何将单元格 View 设计为两个三角形?