你如何有效地检测UIView和UICollectionViewCells的碰撞?

时间:2015-03-30 21:17:22

标签: ios objective-c uicollectionview

我有一个标记视图(uiview),它永远地反复遍历我想要检测的一堆uicollectionviewcells,并可能改变检测到的那些细胞的颜色,并在标记经过细胞时将其重置。

到目前为止我所拥有的:

View did appear : 
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkHandler)];
            [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

- (void) displayLinkHandler {

    id presentationLayer1 = marker.markerLine.layer.presentationLayer;

    for (NSIndexPath* cellPath in [seqcollectionview indexPathsForVisibleItems]) {

        UICollectionViewCell *cell = [seqcollectionview cellForItemAtIndexPath:cellPath];

        id presentationLayer2 = cell.layer.presentationLayer;

        BOOL nowIntersecting = CGRectIntersectsRect([presentationLayer1 frame], [presentationLayer2 frame]);

        // I'll keep track of whether the two views were intersected in a class property,
        // and therefore only display a message if the intersected state changes.

        if (nowIntersecting != wasIntersected) {

            if (nowIntersecting && cell.selected) {
                NSLog(@"row is: %d section is: %d", [cellPath row], [cellPath section]);

                cell.backgroundColor = [UIColor whiteColor];


            }else{


                    cell.backgroundColor = [UIColor colorWithRed:1 green:0.855 blue:0.741 alpha:1];

            }
            wasIntersected = nowIntersecting;
        }
    }

}

到目前为止,这种方法有效,但颜色变化的延迟时间太长了!

干杯

0 个答案:

没有答案