简体   繁体   中英

how to get touchesMoved for UIImageView within a cell of UITableView?

I need to get touchesBegan and touchesMoved for a UIImageView within a cell of UITableView. But touchesBegan and touchesMoved don't fire if I touch UIImageView in cell. touchesBegan and touchesMoved work if I touch any other UIImageView that is beyond UITableView.

Here is allocation of UITableView called from viewDidLayoutSubviews()................ 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];

Here is allocation of UIButton within cell...........................

                    // 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 ];

Here are touch delegates:................................

- (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");
    }
}

This should work assuming you have touchesMoved:withEvent: inside the cell class.

Try the following on your UITableViewController

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

Image views have the userInteractionEnabled set to NO . Which means that you wont get the touch events. Set that to YES and it may fix your problem. However the better way to do this is to use a UIPanGestureRecognizer . You would still need to set userInteractionEnabled to YES but it works better with the tableview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM