简体   繁体   English

如何将轻击手势添加到从dequeueReusableCellWithReuseIdentifier返回的UICollectionViewCell子视图中

[英]How to add tap gesture to UICollectionViewCell subview returned from dequeueReusableCellWithReuseIdentifier

What's the best method for efficiently adding a tap gesture to a subview of a UICollectionViewCell returned from dequeueReusableCellWithReuseIdentifier that already has a bunch of default gesture recognizers attached to it (such as a UIScrollView ). dequeueReusableCellWithReuseIdentifier返回的UICollectionViewCell的子视图有效添加轻UICollectionViewCell手势的最佳方法是什么,该方法已经附加了许多默认手势识别器(例如UIScrollView )。 Do I need to check and see if my one custom gesture is already attached ( scrollView.gestureRecognizers ) and if not then add it? 我是否需要检查并查看我的一个自定义手势是否已附加( scrollView.gestureRecognizers ),如果没有,则添加它? I need my app's scrolling to be as smooth as possible so performance of the check and efficient reuse of already created resources is key. 我需要我的应用程序的滚动尽可能平滑,因此检查的性能和对已创建资源的有效重用是关键。 This code all takes place inside cellForItemAtIndexPath . 此代码全部发生在cellForItemAtIndexPath内部。 Thanks. 谢谢。

I figured out a way to do it that requires only a single, shared, tap gesture recognizer object and moves the setup code from cellForItemAtIndexPath (which gets called very frequently as a user scrolls) to viewDidLoad (which gets called once when the view is loaded). 我想出了一种方法,该方法只需要一个共享的点击手势识别器对象,并将设置代码从cellForItemAtIndexPath (随着用户滚动被频繁调用)移动到viewDidLoad (加载视图时被调用一次) )。 Here's the code: 这是代码:

- (void)myCollectionViewWasTapped:(UITapGestureRecognizer *)tap
{
    CGPoint tapLocation = [tap locationInView:self.collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tapLocation];
    if (indexPath)
    {
        MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
        CGRect mySubviewRectInCollectionViewCoorSys = [self.collectionView convertRect:cell.mySubview.frame fromView:cell];
        if (CGRectContainsPoint(mySubviewRectInCollectionViewCoorSys, tapLocation))
        {
            // Yay! My subview was tapped!
        }
    }
}

- (void)viewDidLoad
{
    // Invoke super
    [super viewDidLoad];

    // Add tap handler to collection view
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myCollectionViewWasTapped:)];
    [self.collectionView addGestureRecognizer:tap];
}

Here's a rough, very simple outline of a possible design solution: you could subclass UICollectionViewCell and override its initialization methods to add the gesture recognizer to its subviews. 这是一个可能的设计解决方案的粗略,非常简单的概述:您可以将UICollectionViewCell子类UICollectionViewCell并重写其初始化方法,以将手势识别器添加到其子视图中。 Furthermore, if you don't want the cell to "know" about the gesture recognizer, you could create a protocol that the data source object would conform to. 此外,如果您不希望单元“知道”手势识别器,则可以创建数据源对象将遵循的协议。 The cell object would call a "setup" protocol method at the appropriate time. 单元对象将在适当的时间调用“设置”协议方法。

Hope this helps! 希望这可以帮助!

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

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