[英]how to get touchesMoved for UIImageView within a cell of UITableView?
我需要在UITableView的單元格內獲取touchesBegan和touchesMoved以獲取UIImageView。 但是如果我在單元格中觸摸UIImageView,那么touchesBegan和touchesMoved就不會觸發。 touchesBegan和touchesMoved工作,如果我觸摸超出UITableView的任何其他UIImageView。
這里是從viewDidLayoutSubviews()................ tableFrame = CGRectMake(y-42,x + 10 + DEVICE_TABLE_BORDER_WIDTH,高度,寬度)調用的UITableView的分配;
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
但它對tableview更有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.