如何使集合视图响应其自身视图之外的平移手势

Ste*_*n.B 5 ios uipangesturerecognizer uicollectionview swift swift4

我有一个UICollectionView在我的UIViewController,我希望它响应UICollectionView.内部和外部的手势。默认情况下,UICollectionView它只响应它自己内部的手势,view但我怎样才能让它响应它外部的滑动view

演示

谢谢。

Nai*_*ler 4

我编写了一个视图子类来完成此任务:

#import <UIKit/UIKit.h>

@interface TouchForwardingView : UIView

@property (nonatomic, weak) IBOutlet UIResponder *forwardingTarget;

- (instancetype)initWithForwardingTarget:(UIResponder *)forwardingTarget;


@end

#import "TouchForwardingView.h"

@implementation TouchForwardingView

- (instancetype)initWithForwardingTarget:(UIResponder *)forwardingTarget
{
    self = [super init];
    if (self)
    {
        self.forwardingTarget = forwardingTarget;
    }

    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.forwardingTarget touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self.forwardingTarget touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    [self.forwardingTarget touchesCancelled:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self.forwardingTarget touchesMoved:touches withEvent:event];
}

@end
Run Code Online (Sandbox Code Playgroud)

在界面生成器中,将包含视图的子视图设置为 TouchForwardingView,然后将集合视图分配给forwardingTarget 属性。