0

I have a UIImageView that can move around, and what I am trying to do is calculate how many pixels the UIImageView gets dragged off of the right of the screen. It's easy on the left because you just have to convert the origin to a positive number. I need the same results as if it was going off of the left.

Jonathan
  • 507
  • 5
  • 15
  • [What have you tried](http://whathaveyoutried.com) so far? It sounds like you have a similar solution already - have you thought about adapting that? – Tim Nov 16 '12 at 21:13
  • 3
    Uhm, `float distRight = [[UIScreen mainScreen] size].width - view.origin.x;`, is that what you're looking for? –  Nov 16 '12 at 21:13
  • 2
    @NSPostWhenIdle: dangerous to say 320, since screens can have different resolutions (or be in landscape mode). – Tim Nov 16 '12 at 21:14
  • 1
    @NSPostWhenIdle also, even if it wasn't dangerous, instead of `The same thing you would do for the left side - 320`, it would be `320 - The same thing you would do for the left side`. –  Nov 16 '12 at 21:16
  • What I do on the left is get the origin of the UIImageView which is a negative and just times it by -1 so I would get the positive. That gave me how many pixels it went off of the screen – Jonathan Nov 16 '12 at 21:27
  • 1
    @Jonathan there's an `abs()` function in the C standard library, you know. –  Nov 16 '12 at 21:31

2 Answers2

2

If you're looking for how far the origin (upper left corner) of the view is from the right side of the screen (assuming that there aren't multiple levels of sub- and superviews which are offset):

float distRight = [[UIScreen mainScreen] size].width - view.origin.x;

If you want the distance of the right side of the view from the right side of the screen, simply subtract the width of the view:

float distRight = [[UIScreen mainScreen] size].width - view.origin.x - view.frame.size.width;

(But come on, it's very basic geometry, even if you can't imagine it, you should be able to draw it and then code it...)

  • I have a UIImageView that can move around, and what I am trying to do is calculate how many pixels the UIImageView gets dragged off of the right of the screen. Sorry I should have been more specific before. – Jonathan Nov 16 '12 at 21:24
  • 1
    That's exactly what his solution does. Take the size of the screen and subtract the `x` position of the view from that. Ex. 320 - 330 means it's ten points off screen. Alter this by the width of the view to change the edge you calculate from. What else do you want? :S – Metabble Nov 16 '12 at 21:29
  • So after much trouble I have found the real issue. My view has the wrong dimensions. I am running a Landscape only but the view is reporting portrait dimensions "View Width = 768.000000 Height = 1024.000000" Any Ideas how to fix that? – Jonathan Nov 16 '12 at 23:00
  • @Jonathan is your view attached to a view controller? –  Nov 16 '12 at 23:02
  • Do you mean - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } – Jonathan Nov 16 '12 at 23:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19661/discussion-between-jonathan-and-h2co3) – Jonathan Nov 16 '12 at 23:27
  • http://stackoverflow.com/questions/13426016/my-self-view-has-the-wrong-dimensions – Jonathan Nov 16 '12 at 23:41
0

This code is untested, but should provide methods to call and you will find out for right or left offsets.

-(float)isOffScreenToLeft{
float distFromLeftEdge = [[UIScreen mainScreen] size].width - imgview.origin.x;

if(distFromLeftEdge < 0){
NSLog("Offscreen to left %f pixels.", fabs(distFromLeftEdge));
return fabs(distFromLeftEdge));
}


-(float)isOffScreenToRight{
float rightEdgePosition = ([[UIScreen mainScreen] size].width - imgview.origin.x) + imgview.frame.size.width;
float distFromRightEdge = [[UIScreen mainScreen] size].width - rightEdgePosition

//If ipad in portrait
if(distFromRightEdge < 0){
NSLog("Offscreen to right %f pixels.", fabs(distFromRightEdge));
return fabs(distFromLeftEdge);
}
propstm
  • 3,461
  • 5
  • 28
  • 41