![](/img/trans.png)
[英]How to add tap gesture to UICollectionViewCell subview returned from dequeueReusableCellWithReuseIdentifier
[英]Add Gesture to UICollectionViewCell subview with xib
我试图将手势添加到带有xib的UICollectionViewCell
的子视图中,我正在这样做:
。H
@interface MyCell : UICollectionViewCell <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIView *containerButton;
@end
.M
@implementation MyCell
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[panGestureRecognizer setDelegate:self];
if (self.containerButton) {
NSLog(@"ok"); //not enter here
[self.containerButton addGestureRecognizer:panGestureRecognizer];
}
}
-(void)prepareForReuse {
[super prepareForReuse];
if (self.containerButton) {
NSLog(@"ok 2");
}
}
我创建了与xib文件连接的UICollectionViewCell子类,在其中创建了容器按钮视图,如果我尝试在initialize方法中添加手势,containerButton为nil,因此无效,但是在prepareForReuse
方法中是不为空,我可以在其中添加手势吗? 还是我可以在其他地方做到?
尝试这个:
- (void)awakeFromNib
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[panGestureRecognizer setDelegate:self];
if (self.containerButton) {
NSLog(@"ok"); //not enter here
[self.containerButton addGestureRecognizer:panGestureRecognizer];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.