Doug Null Doug Null - 2 months ago 9
iOS Question

how to get screen location of UIImageview in a UITableViewCell

I have a

UITableViewCell
that contains a
UIImageView'.
I need to display a 2nd
UIImageView
that is centered on the 1st one.
(this 2nd UIImageView is not in the cell, but to be displayed on the screen coordinate system, so that it can be dragged outside of the
UITableView
that has the
UITableViewCell`.

I can get the
x,y
of the 1st
UIImageView
, but this
x,y
is relative to the cell.

How do I convert this
x,y
to be relative to the screen, to be used to initially position the 2nd
UIImageView
centered over the 1st?

I have tried:
CGPoint screen_point = [device_icon_UIImageView convertPoint: device_icon_UIImageView.center toView:nil];
...but screen_point is the relative x,y within the cell (615,85) and is not the x,y center of the device_icon_UIImageView in the screen (window) coordinate system (should be about 530,280).

THE CODE...................................

.h......................

@interface ataglance_central_controller : UIViewController

{
UIImageView* e_fairy_UIImageView;
}
@property (nonatomic,retain) UIImageView* e_fairy_UIImageView;

.m..............................................
@interface ataglance_central_controller ()
{
}
@end
@synthesize e_fairy_UIImageView;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...


THIS IS THE 1ST UIIMAGEVIEW, IN THE CELL:................
UIImageView* device_icon_UIImageView = (UIImageView*)[cell.contentView viewWithTag: BASE_UIIMAGEVIEW_TAG + ui++ ];
...

self.e_fairy_UIImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"E-fairy.png" ]];
self.e_fairy_UIImageView.center = screen_point;


[self.e_fairy_UIImageView setContentMode: UIViewContentModeScaleAspectFit];
[self.view addSubview: self.e_fairy_UIImageView];

...displays
e_fairy_UIImageView
at bottom of screen, but cell's table is in upper half of screen.

Answer

I fixed it. I gave up trying to convert relative cell x,y into screen x,y. Instead, solution was to simply add relative cell x,y to absolute screen x,y of UITableView.

CGPoint icon_screen_CGPoint;
icon_screen_CGPoint.x = absolute screen x + device_icon_UIImageView.center.x;
icon_screen_CGPoint.y = absolute screen y + device_icon_UIImageView.center.y;

// Create screen UIImageView:
self.e__UIImageView = [ [UIImageView alloc] initWithImage:[UIImage imageNamed: @"E.png" ] ];

// Position it over center of cell icon:
self.e__UIImageView.center = icon_screen_CGPoint;
[self.view addSubview: self.e__UIImageView];

Sweet.