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

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
Hello,
I have a number of UIButtons moving in an animation where they overlap. Is there a way to programmatically move one button in front of another when passing a given coordinate?

Code:
- (IBAction) onTimer1 {
	button1.center = CGPointMake(button1.center.x+pos1.x,button1.center.y+pos1.y);
	
	if (button1.center.x > 270|| button1.center.x+pos1.y < 55) 
		pos1.x = -pos1.x;
	if (button1.center.y > 400|| button1.center.y+pos1.y < 292.5) 
		pos1.y = -pos1.y;
	if (button1.center.y > 395)
		button1.transform = CGAffineTransformMakeScale(1, 1);
//Here is where I want to move to front.
	if (button1.center.y < 394)
		button1.transform = CGAffineTransformMakeScale(.5, .5); //...
//Here is where I want to move to back.
}

Thanks,
Nick
 
The only way I can think of doing it would be to alter the zPosition property of the buttons layer to position it above the other buttons.

Thank you, Sir. That seemed to do the trick.

Code:
- (IBAction) onTimer1 {
	button1.center = CGPointMake(button1.center.x+pos1.x,button1.center.y+pos1.y);
	
	if (button1.center.x > 270|| button1.center.x+pos1.y < 55) 
		pos1.x = -pos1.x;
	if (button1.center.y > 400|| button1.center.y+pos1.y < 292.5) 
		pos1.y = -pos1.y;
	if (button1.center.y > 395){
		button1.transform = CGAffineTransformMakeScale(1, 1);
button1.layer.zPosition=5.0f;
}
	if (button1.center.y < 394){
		button1.transform = CGAffineTransformMakeScale(.5, .5); //...
button1.layer.zPosition=-5.0f;
}
}

Nick
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.