Stopping UIImageView at point

Discussion in 'iOS Programming' started by x12, May 20, 2009.

  1. x12 macrumors newbie

    Joined:
    May 20, 2009
    #1
    Can anyone plz help me, i have an UIImageView moving along the y axis with touchesMoved, does anyone no how i can get to move only between 2 points on the axis, thank you
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How exactly are you "moving" it? Can you give us a code snippet of your touchesMoved method?
     
  3. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #3
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    CGPoint touchLocation = [touch locationInView:self.view];


    if ([touch view] == image1) {

    CGPoint newLocation = CGPointMake(touchLocation.x,280);
    image1.center = newLocation;


    }


    else if ([touch view] == image2) {

    CGPoint newLocation1 = CGPointMake(222, touchLocation.y);
    image2.center = newLocation1;
    }

    else if ([touch view] == image3) {

    CGPoint newLocation2 = CGPointMake(263, touchLocation.y);
    image3.center = newLocation2;


    }

    }

    thats the code for the 3 images i am moving i want to be able to move image1 between the 2 points
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    So, as soon as image1 starts to move you want it to "snap" to the other point? Or do you just want it's movement restricted to the y-axis between the two endpoints? If the latter, just add some logic to only recenter the image if the x location falls between the two endpoints.
     
  5. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #5
    i want image1 movement to be restricted between the 2 points how can it be done i have been stuckon this for sometime now and cant find anything online
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Then add logic that if image1 is at one point, recenter at the other point. And vice-versa. Something like this perhaps:
    Code:
    if ([touch view] == image1) {
        CGPoint newLocation;
        if (image1.center.x == x1) {
            newLocation = CGPointMake(x2,280);
        } else {
            newLocation = CGPointMake(x1,280);
        }
        image1.center = newLocation;
    }
    EDIT: Wait. I'm still not sure what you're looking for. Is image1 restricted to two exact points or is it restricted to a line between those points?
     
  7. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #7
    if ([touch view] == image1) {
    CGPoint newLocation;
    if (image1.center.x == 140) {
    newLocation = CGPointMake(340,280);
    } else {
    newLocation = CGPointMake(140,280);
    }
    image1.center = newLocation;
    }

    using this code the image will jump between the points rather than move between them, any ideas, so close thanks for your help
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Code:
    if ([touch view] == image1) {
        CGPoint newLocation;
        if (image1.center.x >= x1 && image1.center.x <= x2) {
            ...
            image1.center = newLocation;
        }
    }
    I think you're smart enough to figure out what goes in the "..." :D
     
  9. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #9
    it would seem that i am not smart enough, confused actually.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Confused? Why? Help us to understand what's confusing you.
     
  11. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #11
    when i applied the code you gave me i tried a few different lines of code where you had written ... the image would move but when it hit one of the points it would jump to the corner of the screen
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Let's see your latest touchesMoved code then.
     
  13. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #13
    if ([touch view] == image1) {
    CGPoint newLocation;

    if (image1.center.x >= 140 && image1.center.x <= 340) {

    CGPoint newLocation = CGPointMake(touchLocation.x, 280);

    image1.center = newLocation;
    }
    }

    this allows me to move it to one of the points but when it hits a point it is stuck there.

    i also get a warning that newLocation is a unused variable
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Oops, what I meant was:
    Code:
    if (touchLocation.x >= 140 && touchLocation.x <= 340)
    You've declared newLocation twice.

    P.S. Please use
    HTML:
    [CODE]...[/CODE]
    tags around your code to make it more legible. The # icon in the toolbar does the tagging.
     
  15. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    You're welcome. You understand what it's doing, right?
     
  17. x12 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #17
    Yeah, i think so, been through all sorts of tutorials for touches so i kind of got my head round it all, thanks again
     

Share This Page