【问题标题】:Pan gesture not fired for a view inside content view of table cell未针对表格单元格内容视图内的视图触发平移手势
【发布时间】:2015-09-26 16:57:16
【问题描述】:

我正在尝试为 UIView 注册一个平移手势,它是 UITableViewCell 的内容视图的子视图。但是没有触发平移手势动作。

以下是平移手势识别器和布局约束的代码。

self.panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pancontentView:)];
self.panGesture.delegate=self;
[self.mycontentView addGestureRecognizer:self.panGesture];
[self.panGesture setCancelsTouchesInView:NO];
[self.mainLabel addGestureRecognizer:self.panGesture];
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.translatesAutoresizingMaskIntoConstraints=NO;
if(self)
{
   //self.mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, self.contentView.frame.size.height)];
    self.mainLabel=[[UILabel alloc]init];
    [self.mainLabel setBackgroundColor:[UIColor greenColor ]];
    self.mainLabel.textColor = [UIColor blackColor];
    self.mainLabel.font = [UIFont fontWithName:@"Arial" size:12.0f];

    self.mycontentView=[[UIView alloc] init];
    self.mycontentView.translatesAutoresizingMaskIntoConstraints=NO;
    self.mainLabel.translatesAutoresizingMaskIntoConstraints=NO;
    //self.contentView.translatesAutoresizingMaskIntoConstraints=NO;
    [self.contentView addSubview:self.mycontentView];
    [self.mycontentView addSubview:self.mainLabel];
    //[self addConstraint:[NSLayoutConstraint constraintWithItem:self.mycontentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];

    [self addConstraints:@[
                           [NSLayoutConstraint constraintWithItem:self.mainLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
                           [NSLayoutConstraint constraintWithItem:self.mainLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
                           [NSLayoutConstraint constraintWithItem:self.mainLabel attribute: NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:45.0],
                           [NSLayoutConstraint constraintWithItem:self.mainLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-45.0],

                           ]];
    [self addConstraints:@[
                           [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
                           [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
                           [NSLayoutConstraint constraintWithItem:self.contentView attribute: NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0],
                           [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.mycontentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0],

                           ]];
}

return self;

这是平移手势的动作。

switch (gestureRecognizer.state) {
    case UIGestureRecognizerStateBegan:
        self.panStartPoint = [gestureRecognizer translationInView:self.contentView];
        NSLog(@"Pan Began at %@", NSStringFromCGPoint(self.panStartPoint));
        break;
    case UIGestureRecognizerStateChanged: {
        CGPoint currentPoint = [gestureRecognizer translationInView:self.contentView];
        CGFloat deltaX = currentPoint.x - self.panStartPoint.x;
        NSLog(@"Pan Moved %f", deltaX);
    }
        break;
    case UIGestureRecognizerStateEnded:
        NSLog(@"Pan Ended");
        break;
    case UIGestureRecognizerStateCancelled:
        NSLog(@"Pan Cancelled");
        break;
    default:
        break;
}

请帮忙。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    这是一个转储问题。 对不起大家。 我试图在初始化之前注册手势。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多