如何获得触摸在UITableView的单元格中为UIImageView移动



我需要在UITableView的单元格中获取touchesStarted和touchesMovefor a UIImageView。但是如果我触摸单元格中的 UIImageView,触摸开始和触摸移动不会触发。touches开始和触摸移动工作,如果我触摸任何其他超出UITableView的UIImageView。

这是从viewDidLayoutSubviews()...............调用的UITableView的分配。 tableFrame = CGRectMake(y-42, x+10 + DEVICE_TABLE_BORDER_WIDTH, height, width);

        device_off_tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
        device_off_tableView.rowHeight = device_off_row_height;
        device_off_tableView.sectionFooterHeight = 0;
        device_off_tableView.sectionHeaderHeight = 0;
        device_off_tableView.scrollEnabled = YES;
        device_off_tableView.bounces = YES;
        device_off_tableView.delegate = self;
        device_off_tableView.dataSource = self;
        device_off_tableView.allowsSelection = NO;
        device_off_tableView.tag = channel;
        device_off_tableView.transform  = CGAffineTransformMakeRotation(-M_PI/2);
        device_off_tableView.autoresizesSubviews = NO;
        // Add device_off_tableView to the view:
            device_off_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
            off_record_index = expected_off_index = 0;
            [device_off_tableView  reloadData];         
            [[self view] addSubview:   device_off_tableView];

这是单元格内UIButton的分配。

                    // Put invisible UIButton on top of device icon, to detect start of drag:                    !!!
                    UIButton* invisible_drag_button = [UIButton buttonWithType:UIButtonTypeCustom];
                    invisible_drag_button.frame = CGRectMake    (x,y, w,h);        // location and size of device off icon
                    invisible_drag_button.tag = cell_record_index;      // store user device's record index
                    [invisible_drag_button addTarget: self 
                                action:@selector( delegate_drag_start: )
                                forControlEvents: UIControlEventTouchDown ];
                    [cell.contentView addSubview: invisible_drag_button ];

以下是触摸代表:........................。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    printf("touchesBegan    n");
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    // Get touch event's details:
    UITouch *touch = [[event allTouches] anyObject];
    // Touching image to be dragged?
    if([touch view] == self.device_drag_UIImageView)
    {
        printf("touchesMoved    device_drag_UIImageView n");
        CGPoint location = [touch locationInView: self.view];
        self.device_drag_UIImageView.center=location;
    }
    else
    {
        printf("touchesMoved    WRONG IMAGE n");
    }
}

假设您在单元格类中具有touchesMoved:withEvent:,这应该可以工作。

在 UITableViewController 上尝试以下操作

self.tableView.canCancelContentTouches = NO;
self.tableView.delaysContentTouches = NO;

图像视图的userInteractionEnabled设置为 NO 。这意味着您将不会获得触摸事件。将其设置为 YES,它可能会解决您的问题。但是,更好的方法是使用 UIPanGestureRecognizer .您仍然需要将userInteractionEnabled设置为 YES但它在表视图中效果更好

最新更新