1

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 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

CGPoint ... did not work. point was at 85,581 but screen is 1024,655 and table and cell are in upper half. Should it really be "toView:self.view"? This is being run from the cell update delegate, so maybe 'self' is relative to cell?
try self.tableView instead of self.view (or whatever your tableView's name is)
I think the problem is that it should be [cell.contentView convertRect:imageView.frame toView:self.view];' or [cell.contentView convertPoint:imageView.center toView:self.view];`
No. I still can't convert to screen coordinates.
Sorry, I'm not sure what else to try. I used the text method in a recent project and it worked fine. My guess would be to keep trying different combinations of views/superviews in the toView: parameter.
0

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);

2 Comments

No good. I did this, and the 3_fairy_UIImageView.frame.origin.x = 0 and .origin.y = 0:............... CGRect specifiedRect = device_icon_UIImageView.frame; self.e_fairy_UIImageView.frame = CGRectMake(specifiedRect.origin.x, specifiedRect.origin.y, w, h);
if the origin.x and origin.y are 0, then you are setting from bounds; and if you set from frame you will get the actual dimensions
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.