5

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

演示

谢谢。

4

3 回答 3

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

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

于 2016-09-20T13:53:38.810 回答
2

Nailer 的 Swift 版本,这会将在视图控制器上完成的所有手势转发到集合视图

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    collectionView.touchesBegan(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    collectionView.touchesEnded(touches, withEvent: event)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    collectionView.touchesCancelled(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    collectionView.touchesMoved(touches, withEvent: event)
}
于 2016-09-22T09:40:27.100 回答
-1

史蒂文 B 对 Swift 4 的回答 :)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    collectionView.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    collectionView.touchesEnded(touches, with: event)
}
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
    collectionView.touchesCancelled(touches!, with: event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    collectionView.touchesMoved(touches, with: event)
}
于 2018-01-13T02:56:03.770 回答