如何使集合视图响应它自己视图之外的平移手势 [英] How to make a collectionview respond to pan gestures outside of it's own view
问题描述
我的 UIViewController
中有一个 UICollectionView
,我希望它能响应内部和外部的手势 UICollectionView
。默认情况下, UICollectionView
仅响应其自己的视图
内的手势,但是如何让它响应于它的视图
?
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屋!