不为表格单元格的内容视图中的视图触发平移手势

时间:2015-09-26 08:01:54

标签: ios objective-c uitableview

我正在尝试为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;
}

请帮忙。

1 个答案:

答案 0 :(得分:0)

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