CALayer inside UICollectionViewCell

I have added a GIF to describe the problem. I have UICollectionView

with a lot of cells and each cell has one CALayer

inside. I have a pinch

gesture in mine UICollectionView

. When it is scaled, it looks like every cell is scaled. See spaces between cells on gif. Is it possible to increase the number of 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;
}

      

+3


source to share


2 answers


The changes you make on this layer are implicitly animated , which means that they are performed with animation, even if you haven't specifically told them to.

Since you are responding to the user's gestures, you do not want the changes to be animated as it makes them lag behind the gestures.



You can turn off implicit animations during layoutSubviews like this:

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

      

+1


source


Cell subclass

@property (nonatomic, strong) CALayer *circularLayer;

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

      



The only problem I see with this code is the lack of [super layoutSubviews]; If it doesn't work please provide more code.

0


source







All Articles