Problem with dragging image within bounds

Discussion in 'iOS Programming' started by BarryK88, Mar 8, 2011.

  1. BarryK88, Mar 8, 2011
    Last edited by a moderator: Mar 10, 2011

    BarryK88 macrumors member

    Joined:
    Mar 1, 2011
    #1
    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!
     
  2. BarryK88, Mar 10, 2011
    Last edited by a moderator: Mar 10, 2011

    BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #2
    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 :)
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    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
     
  4. BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #4
    Thanks for your reply.

    I succeeded implementing your advice!
     

Share This Page