Reputation: 13
In my app I have an up and a down button. My app functions like a Game Boy if you will, and I have a UIImageView containing an arrow that moves to a CGPoint each time the button is pressed.
I have three options the user can select, but I can only program my down button to go down once, instead of being able to select down to the third option. Same goes for my up button.
My question is:
How can I tap my down button once to go down, then go down again, then go up and then up again?
Confusing, I know, but I am a young developer. Thanks!
My code for moving the arrow UP:
[arrow setCenter:CGPointMake(204, 93)];
My code for moving the arrow DOWN:
[arrow setCenter:CGPointMake(176, 140)];
Upvotes: 0
Views: 149
Reputation: 2255
Here's some hints on how to approach your problem. Try to separate the option selection from your display code. Keep track of the selection option, not the position of the arrow.
Let's say your options are 0, 1, 2. In your interface:
@property (nonatomic, assign) NSUInteger option;
In your init
or elsewhere:
self.option = 0; // or whatever the default option is
On pressing the up button:
self.option = self.option++;
On pressing the down button:
self.option = self.option--;
Next, before doing the increment or decrement, check that you're not decrementing past 0 (the bottom option) or incrementing past 2 (the top option).
After setting the option value, place the arrow appropriately based on the option. Change the Y values below to the Y center of your labels.
static const CGFloat optionYs[3] = { 10.0, 20.0, 30.0 };
CGFloat y = optionYs[self.option];
[arrow setCenter:CGPointMake(x, y)];
Hopefully this sets you on the right track!
Upvotes: 1
Reputation: 1127
You could try using the properties of the arrow to move it up and down a small amount from where it is currently located instead of hard-coding which location you want it to go to when a button is pressed.
For up:
[arrow setCenter:CGPointMake(arrow.center.x, arrow.center.y - 10.0)];
For down:
[arrow setCenter:CGPointMake(arrow.center.x, arrow.center.y + 10.0)];
Edit:
If you have three different y values that you want your arrow to stick to, you need to check what the current value of y is before you set the new one. So, say you want your arrow to go to 10.0, 20.0, or 30.0. If it's 30.0 and you press up, you want it to change to 20.0. If it's 20.0, you want it to change to 10.0. If it's 10.0, you want it to wrap around and go back to 30.0. Something like this should work:
if([arrow.center.y == 30.0)
{
[arrow setCenter:CGPointMake(arrow.center.x, 20.0)];
}
else if(arrow.center.y == 20.0)
{
[arrow setCenter:CGPointMake(arrow.center.x, 10.0)];
}
else
{
[arrow setCenter:CGPointMake(arrow.center.x, 30.0)];
}
Down is similar.
Upvotes: 0