【问题标题】:Add Gesture to UICollectionViewCell subview with xib使用 xib 将手势添加到 UICollectionViewCell 子视图
【发布时间】:2014-02-16 12:05:12
【问题描述】:

我正在尝试使用 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 方法不为空,我可以在那里添加手势吗?或者我可以在其他地方做吗?

【问题讨论】:

    标签: ios iphone objective-c xib uicollectionviewcell


    【解决方案1】:

    试试这个:

    - (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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 2013-06-25
      • 1970-01-01
      • 2015-05-23
      • 2014-07-31
      • 1970-01-01
      相关资源
      最近更新 更多