Search code examples
iosobjective-cuicollectionviewcore-animationtouch-event

Transforming UICollectionViewCell causes touch events not be captured


Im trying to create a horizontal collection view by subclassing UICollectionViewFlowLayout. which the center cell be scaled a little more to be focused on the view.

see the screen shot:

enter image description here

but UICollectionViewCell doesn't capture touch events. I mean when I tap on the cell the delegate method - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath won't called. but when I remove the scale transform from the cell, the above method called!

#import "CoverFlowLayout.h"

static const CGFloat kMaxDistancePercentage = 0.3f;
static const CGFloat kMaxRotation = (CGFloat)(50.0 * (M_PI / 180.0));
static const CGFloat kMaxZoom = 0.1f;

@implementation CoverFlowLayout

- (id)init {
    if ((self = [super init])) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.minimumLineSpacing = 10000.0f; }
    return self;
}

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {

    // 1
    CGRect visibleRect =
    (CGRect){.origin = self.collectionView.contentOffset,
        .size = self.collectionView.bounds.size};
    CGFloat maxDistance =
    visibleRect.size.width * kMaxDistancePercentage;
    // 2
    NSArray *array =
    [super layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes *attributes in array) {
        // 3
        CGFloat distance =
        CGRectGetMidX(visibleRect) - attributes.center.x;
        // 4
        CGFloat normalizedDistance = distance / maxDistance;
        normalizedDistance = MIN(normalizedDistance, 1.0f);
        normalizedDistance = MAX(normalizedDistance, -1.0f);
        // 5
        CGFloat rotation = normalizedDistance * kMaxRotation;
        CGFloat zoom = 1.0f + ((1.0f - ABS(normalizedDistance)) * kMaxZoom);
        // 6
        CATransform3D transform = CATransform3DIdentity;
        transform.m34 = 1.0 / -1000.0;
        //transform = CATransform3DRotate(transform,
          //                             rotation, 0.0f, 1.0f, 0.0f);

        transform = CATransform3DScale(transform, zoom, zoom, 0.0f);
        attributes.transform3D = transform;
    }
    // 7
    return array;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

- (CGPoint)targetContentOffsetForProposedContentOffset: (CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    // 1
    CGFloat offsetAdjustment = CGFLOAT_MAX;
    CGFloat horizontalCenter = proposedContentOffset.x +
    (CGRectGetWidth(self.collectionView.bounds) / 2.0f);
    // 2
    CGRect targetRect = CGRectMake(proposedContentOffset.x,
                                   0.0f, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    NSArray *array =
    [super layoutAttributesForElementsInRect:targetRect];
    for (UICollectionViewLayoutAttributes* layoutAttributes in array)
    {
        // 3
        CGFloat distanceFromCenter = layoutAttributes.center.x - horizontalCenter;
        if (ABS(distanceFromCenter) < ABS(offsetAdjustment))
        {
            offsetAdjustment = distanceFromCenter;
        }
    }
    // 4
    return CGPointMake(proposedContentOffset.x + offsetAdjustment,
                   proposedContentOffset.y);

}

any idea?


Solution

  • You don't need to scale on z-axis, the problem is caused because you were scaling on z to zero

    The scale on z-axis should be greater than zero; Making it zero causes the button to have "no depth", so the touches are not recognized. (Although you can still see the button)

    Scaling is a computed using a multiplication, so in order to cause "no transformation on z-axis", the value should be 1, rather than 0