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

x12

macrumors newbie
Original poster
May 20, 2009
27
0
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
 
- (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
 
i want to be able to move image1 between the 2 points
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.
 
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
 
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
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?
 
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
 
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
 
it would seem that i am not smart enough, confused actually.
 
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
 
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
 
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
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.
 
Yeah, i think so, been through all sorts of tutorials for touches so i kind of got my head round it all, thanks again
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.