1
votes

I have a UITableViewCell that contains a UIImageView'. I need to display a 2ndUIImageViewthat 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 theUITableViewthat has theUITableViewCell`.

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.

4

4 Answers

1
votes

Have a look at the UIView methods convertPoint:toView:, convertPoint:fromView: and related methods. These allow conversion between UIView coordinate systems. If you leave the toView or fromView nil, they will also convert to/from window coordinates.

1
votes

I would recommend using UIView's convertRect:toView or convertPoint:toView methods (available in all subclasses). Your implementation would look something like follows:

CGRect rect = [cell convertRect:imageView.frame toView:self.view];

or,

CGPoint point = [cell convertPoint:imageView.center toView:self.view];
0
votes

I think I did something similar if I understood correctly. 1) define the frame of the cell or imageView as CGRect specifiedRect = yourCellOrImageView.frame, 2) set your other UIVIew object with that frame yourObject.frame = CGRectMake(specifiedRect.origin.x, specifiedRect.origin.y, objectWidth, objectHeight);

0
votes

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.