Reputation: 217
I have a UIButton
that is able to change locations on the screen after it has been clicked. The user then clicks a different button to change the image of the button and I want it to stay in its new location. Currently with my code it is reverting back to the original position after changing the image. Here is the code:
First move the Button
(newLocation is a location of a UIImageView
):
movableButton.center = [newLocation center];
Then Change the button image by clicking a different button:
[movableButton setImage:[UIImage imageNamed: @"differentImage.png"] forState:UIControlStateNormal];
Upvotes: 1
Views: 338
Reputation: 451
Ok now i got your question , what you can do is start a counter in 2nd button's action:
-(void)secondButtonClick
{
static int counter = 1;
if(counter %2 == 0)
{
firstButton.center =cgpoint a; // a gain some point where you wan't to place the center
}
if(counter %2 != 0)
{
[firstButton setBackgroundImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal];
}
counter ++;
}
you can modify if condition whether you want position changed before changing image..... Also uou can change the center alsolike this
a.x =a.x+10;
a.y =a.y +10;
myButton.center = a;
Upvotes: 0
Reputation: 19946
I dont understand why it moves. I just tested it, and it does move. I need to look into why it moves and will get back to you.
But this will fix the problem of it moving.
CGPoint currentLoc = self.imageButton.center;
[self.imageButton setImage:[UIImage imageNamed:@"face"] forState:UIControlStateNormal];
self.imageButton.center = currentLoc;
Upvotes: 1
Reputation: 392
Try this
-(IBAction) movingButtonClicked
{
[movingButton setFrame:CGRectMake(movingButton.frame.origin.x+10,movingButton.frame.origin.y+20,50,50)];
}
-(IBAction) changeImage
{
[movingButton setImage:[UIImage imageNamed: @"differentImage.png"] forState:UIControlStateNormal];
}
Upvotes: 0