gpt4 book ai didi

ios - 如何获得 UITableView 单元格中 UIButton 的绝对 x,y

转载 作者:行者123 更新时间:2023-11-28 18:34:14 24 4
gpt4 key购买 nike

在 cellForRowAtIndexPath() 中,我在单元格中分配了一个 UIButton。按下按钮时,调用委托(delegate)需要在屏幕上与按钮相同的绝对位置分配一个 UIImageView,以便开始将 UIImageView 拖动到屏幕上的其他位置

但是,当我读取委托(delegate)的“发送者”(UIButton)的 x,y 时,y 是单元格内 UIButton 的相对 y,而不是 iPad 屏幕上 UIButton 的 y。

在委托(delegate)中,如何获得在 UITableView 的单元格中分配的 UIButton 的绝对屏幕 x、y?

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

响应按钮按下的代表......................

-(void)delegate_drag_start: (id)sender

{ int drag_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];

最佳答案

试试这个:

[view convertPoint:pointToConvert toView:nil]

它将一个点从接收者的坐标系转换到指定 View 的坐标系。如果第二个参数为 nil,则转换为窗口坐标系。

关于ios - 如何获得 UITableView 单元格中 UIButton 的绝对 x,y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22044389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com