[英]how to get absolute x,y of UIButton that's in UITableView cell
In cellForRowAtIndexPath(), I allocate a UIButton in the cell. 在cellForRowAtIndexPath()中,我在单元格中分配了一个UIButton。 When button pressed, delegate is called that needs to allocate a UIImageView at the same absolute location on the screen as the button, in order to begin a drag of the UIImageView to elsewhere on the screen
当按下按钮时,将调用委托,该委托需要在屏幕上与按钮相同的绝对位置分配一个UIImageView,以便开始将UIImageView拖动到屏幕上的其他位置
But, when I read the x,y of delegate's 'sender' (the UIButton), the y is the relative y of the UIButton within the cell, and not the y of the UIButton on the iPad screen. 但是,当我读取委托的“发件人”(UIButton)的x,y时,y是单元格中UIButton的相对y,而不是iPad屏幕上UIButton的y。
From within the delegate, how can I get absolute screen x,y of the UIButton that was allocated within the UITableView's cell? 从委托中,如何获取在UITableView的单元格中分配的UIButton的绝对屏幕x,y?
THE CODE THAT MAKES THE UIBUTTON WITHIN cellForRowAtIndexPath().................. 在cellForRowAtIndexPath()中构成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
printf("%d = invisible_drag_button.tag aaaaaaaaaaaaaaaaa \n", PWR_RX_status_index ); // !!!
printf("x=%d y=%d w=%d h=%d \n", x,y,w,h ); // !!!
[invisible_drag_button addTarget: self
action:@selector( delegate_drag_start: )
forControlEvents: UIControlEventTouchDown ];
[cell.contentView addSubview: invisible_drag_button ];
THE DELEGATE THAT RESPONDS TO BUTTON PRESS................... 响应按钮按下的代表...
-(void)delegate_drag_start: (id)sender
{ int drag_record_index; {int drag_record_index; // drag device's database record index
//拖动设备的数据库记录索引
// Drag record index = database index of device:
UIButton* invisible_drag_button = (UIButton*)sender;
drag_record_index = invisible_drag_button.tag;
printf("delegate_drag_start for drag_record_index %d. \n", drag_record_index );
// Lookup drag device's type from its record:
int device_type = area_device_status[ device_area ][ drag_record_index ].device_type;
// Init UIImageView of drag device image:
// Lookup its UIImage from its type, and allocate UIImageView of drag device image:
self.device_drag_UIImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: device_image_filename( device_type )]];
self.device_drag_UIImageView.userInteractionEnabled = YES;
// Get screen location of touched device icon image:
CGRect drag_button_frame = [invisible_drag_button frame];
//CGPoint drag_button_location = CGPointMake(invisible_drag_button.frame.origin.x, invisible_drag_button.frame.origin.y);
printf("x=%d y=%d w=%d h=%d \n", (int)drag_button_frame.origin.x,
(int)drag_button_frame.origin.y,
(int)drag_button_frame.size.width,
(int)drag_button_frame.size.height ); // !!!
// Start drag image at same location as touched device image:
self.device_drag_UIImageView.frame = CGRectMake( drag_button_frame.origin.x,
drag_button_frame.origin.y,
drag_button_frame.size.width * DRAG_IMAGE_BIGGER_FACTOR,
drag_button_frame.size.height * DRAG_IMAGE_BIGGER_FACTOR );
[self.view addSubview: self.device_drag_UIImageView];
[self.view bringSubviewToFront: self.device_drag_UIImageView];
} }
Try this one: 试试这个:
[view convertPoint:pointToConvert toView:nil]
it converts a point from the receiver's coordinate system to that of the specified view. 它将一个点从接收者的坐标系转换为指定视图的坐标系。 If second parametr is nil, it converts to window coordinate system.
如果第二个参数为nil,它将转换为窗口坐标系。
Here's solution: 解决方法:
CGRect drag_button_CGRect = [invisible_drag_button.superview convertRect:invisible_drag_button.frame toView:self.view];
..which gives me the correct, absolute x,y: drag_button_CGRect.origin.x drag_button_CGRect.origin.y ..这给了我正确的绝对x,y:drag_button_CGRect.origin.x drag_button_CGRect.origin.y
您需要的代码行是:
self.device_drag_UIImageView.center = [self.view convertPoint: invisible_drag_button.center fromView: invisible_drag_button.superview];
对于在那里的Swift人。
let buttonRect = sender.superview?.convertRect(sender.frame, toView: self.tableView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.