如何使集合视图响应其自身视图之外的平移手势 [英] How to make a collectionview respond to pan gestures outside of it's own view
问题描述
我的 UIViewController
中有一个 UICollectionView
,我希望它响应 UICollectionView
内外的手势.默认情况下,UICollectionView
只响应其自己的 view
内的手势,但我怎样才能让它响应其 view
之外的滑动?
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
?
谢谢.
推荐答案
我写了一个视图子类来完成这个:
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
在界面生成器中,将包含视图的子视图设置为 TouchForwardingView,然后将集合视图分配给 forwardingTarget 属性.
In interface builder, set the subview of the containing view to TouchForwardingView, then assign the collection view to the forwardingTarget property.
这篇关于如何使集合视图响应其自身视图之外的平移手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!