Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BarryK88

macrumors member
Original poster
Mar 1, 2011
31
0
I'm making a view with a draggable image inside. However I'd like the image to be moved within a specific region within my view. I implemented an if-statement within the touchesMoved method. The function works for one orientation (landscape), but when I change the orientation from Landscape to Portrait the bounds of the other orientation is still in use. How can I make sure that the region is defined and working for the Landscape and Portrait View individualy.

Code:
-(void) touchesMoved: (NSSet *)touches withEvent: (UIEvent *) event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

    if(deviceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown) {

        if(location.x > 720) {
            location.x = 720;
        }
        if(location.x < 50) {
            location.x = 50;
        }
        if(location.y > 790) {
            location.y = 790;
        }
        if(location.y < 116) {
            location.y = 116;
        }
    }

    if(deviceOrientation == UIInterfaceOrientationLandscapeLeft || UIInterfaceOrientationLandscapeRight){
            if(location.x > 910) {
                location.x = 910;
            }
            if(location.x < 240) {
                location.x = 240;
            }
            if(location.y > 670) {
                location.y = 670;
            }
            if(location.y < 116) {
                location.y = 116;
            }

    }   

    if ([touch view] == image1) {

        image1.center = location;

    }
}
Thanks in advance!
 
Last edited by a moderator:
I figured it out by using an else statement in stead of the entire if statement.

Code:
if(deviceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown) {

if(location.x > 720) {
location.x = 720;
}
if(location.x < 50) {
location.x = 50;
}
if(location.y > 790) {
location.y = 790;
}
if(location.y < 116) {
location.y = 116;
}
}

else {
if(location.x > 910) {
location.x = 910;
}
if(location.x < 240) {
location.x = 240;
}
if(location.y > 670) {
location.y = 670;
}
if(location.y < 116) {
location.y = 116;
}

Pretty simple solution :)
 
Last edited by a moderator:
I figured it out by using an else statement in stead of the entire if statement.

You could also improve your code a bit with a few more elses, since those tests are linked.

Code:
if(deviceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown) {
 if(location.x > 720) location.x = 720;
 else if(location.x < 50) location.x = 50;
 if(location.y > 790) location.y = 790;
 else if(location.y < 116) location.y = 116;
}
else {
 if(location.x > 910) location.x = 910;
 else if(location.x < 240) location.x = 240;
 if(location.y > 670) location.y = 670;
 else if(location.y < 116) location.y = 116;
}

IMHO, this is also a case where the more compact, single line if statements work well.

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.