0

我设置了 UITableViewCell,以便您可以向右滑动以显示操作,然后再次向左滑动以隐藏它们。它们被设置在屏幕外,当单元格被滑动时,单元格的原点向左过渡 -120px 以显示控件。

问题是,一旦显示并点击它们,它们就不会触发。这是我的代码。

设置我的手机

        UISwipeGestureRecognizer *completeSwipeGestureShow = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showCompleteControl:)];
        UISwipeGestureRecognizer *completeSwipeGestureHide = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideCompleteControl:)];

        completeSwipeGestureShow.direction = UISwipeGestureRecognizerDirectionRight;
        completeSwipeGestureHide.direction = UISwipeGestureRecognizerDirectionLeft;

        UIView *cellControlDone = [[UIView alloc] initWithFrame:CGRectMake(-60, 0, 60, 127)];
        UIView *cellControlDelete = [[UIView alloc] initWithFrame:CGRectMake(-120, 0, 60, 127)];

        cellControlDone.backgroundColor = [UIColor colorWithRed:(36.0/255.0) green:(195.0/255.0) blue:(28.0/255.0) alpha:1.0];
        cellControlDelete.backgroundColor = [UIColor colorWithRed:(236.0/255.0) green:(1.0/255.0) blue:(1.0/255.0) alpha:1.0];

        UIButton *cellControlDoneIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        UIButton *cellControlDeleteIcon = [UIButton buttonWithType:UIButtonTypeCustom];

        [cellControlDoneIcon setBackgroundImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
        [cellControlDeleteIcon setBackgroundImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];

        cellControlDoneIcon.frame = CGRectMake(((60 - 22) / 2), ((127 - 22) / 2), 22, 22);
        cellControlDeleteIcon.frame = CGRectMake(((60 - 22) / 2), ((127 - 22) / 2), 22, 22);

        [cellControlDoneIcon addTarget:self action:@selector(completeTask) forControlEvents:UIControlEventTouchUpInside];
        [cellControlDeleteIcon addTarget:self action:@selector(cancelTask) forControlEvents:UIControlEventTouchUpInside];

        [cellControlDone addSubview:cellControlDoneIcon];
        [cellControlDelete addSubview:cellControlDeleteIcon];

        [cell addSubview:cellControlDone];
        [cell addSubview:cellControlDelete];

        cell.clipsToBounds = YES;

        [cell addGestureRecognizer:completeSwipeGestureShow];
        [cell addGestureRecognizer:completeSwipeGestureHide];

行动

- (void) showCompleteControl:(UISwipeGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:tableView];
    NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
    UITableViewCell *swipedCell  = [tableView cellForRowAtIndexPath:swipedIndexPath];

    [UIView animateWithDuration:0.25
        delay:0.0
        options: UIViewAnimationCurveEaseOut
        animations:^{
            [swipedCell setBounds:CGRectMake(-120, 0, 320, 127)];
            NSArray* subviews = [swipedCell.contentView subviews];
            for (UIView* subview in subviews) {
                if(subview.tag == 9999)
                {
                    subview.frame = CGRectMake(0, 126, 10, 1);
                    subview.backgroundColor = [UIColor colorWithRed:(224.0/255.0) green:(224.0/255.0) blue:(224.0/255.0) alpha:1.0];
                }
            }
        }
        completion:^(BOOL finished){
            // Do nothing
        }
     ];
}

- (void) hideCompleteControl:(UISwipeGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:tableView];
    NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
    UITableViewCell *swipedCell  = [tableView cellForRowAtIndexPath:swipedIndexPath];

    [UIView animateWithDuration:0.25
        delay:0.0
        options: UIViewAnimationCurveEaseOut
        animations:^{
            [swipedCell setBounds:CGRectMake(0, 0, 320, 127)];
            NSArray* subviews = [swipedCell.contentView subviews];
            for (UIView* subview in subviews) {
                if(subview.tag == 9999)
                {
                    subview.frame = CGRectMake(0, 0, 10, 127);
                    subview.backgroundColor = [UIColor colorWithRed:(124.0/255.0) green:(124.0/255.0) blue:(124.0/255.0) alpha:1.0];
                }
            }
        }
        completion:^(BOOL finished){
            // Do nothing
        }
     ];
}

- (void) completeTask
{
    NSLog(@"Task Complete");
}

- (void) cancelTask
{
    NSLog(@"Task Canceled");
}
4

1 回答 1

0

我不是 100% 确定,但它可能是这个......

由于您通过调整边界来移动单元格,这意味着它的框架仍然在同一个位置,只有内容在移动。所以触摸仍然被显示控件阻止,因为它们仍然在单元格的框架内并且正在处理它们。

于 2013-07-10T23:20:45.923 回答