How to make a collectionview respond to pan gestures outside of it's own view

谁都会走 提交于 2019-11-29 23:16:49

问题


I've got a UICollectionView in my UIViewController and I want it to respond to gestures inside AND outside of the UICollectionView. By default the UICollectionView only responds to the gestures inside its own view but how can I make it respond to swipes outside of its view?

Thanks.


回答1:


I wrote a view subclass that accomplishes just this:

#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

In interface builder, set the subview of the containing view to TouchForwardingView, then assign the collection view to the forwardingTarget property.




回答2:


Swift version of Nailer's anwer, this will forward all gestures done on the viewcontroller to the collectionview

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)
}



回答3:


Steven B's answer for with 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)
}


来源:https://stackoverflow.com/questions/39595705/how-to-make-a-collectionview-respond-to-pan-gestures-outside-of-its-own-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!