简体   繁体   English

UICollectionViewCell内的CALayer捏

[英]CALayer inside UICollectionViewCell pinch

I added a GIF to describe a problem. 我添加了一个GIF来描述一个问题。 I have a UICollectionView with a lot of cells and each cell has a CALayer inside. 我有一个包含大量单元格的UICollectionView ,每个单元UICollectionView都有一个CALayer I have a pinch gesture in my UICollectionView . 我的UICollectionView有一个pinch手势。 When it zooming in it looks like each cell zooming apart. 当它放大时,看起来每个单元格都会缩小。 See spaces between cells on gif. 查看gif上的单元格之间的空格。 Is it possible to zoom in each cells together? 是否可以将每个单元放大在一起? Thanks in advance Code: 在此先感谢代码:

Cell subclass 细胞亚类

@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];
    }
}

Updated: 更新:

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

The changes you are making to the layer are implicitly animated , meaning that they are performed with an animation, even though you haven't specifically told them to. 您对图层所做的更改是隐式动画的 ,这意味着它们是使用动画执行的,即使您没有明确告诉他们。

Since you're responding to user gestures, you don't want the changes to be animated as this is making them lag behind the gesture. 由于您正在响应用户手势,因此您不希望对更改进行动画处理,因为这会使它们落后于手势。

You can turn off implicit animations during layoutSubviews like this: 您可以在layoutSubviews期间关闭隐式动画,如下所示:

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

Cell subclass 细胞亚类

@property (nonatomic, strong) CALayer *circularLayer;

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

The only problem I see with this code importantly is missing [super layoutSubviews]; 我在这段代码中看到的唯一问题就是缺少[super layoutSubviews]; If it doesn't work do provide more code. 如果它不起作用,请提供更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM