PDA

View Full Version : Move Button to front programmatically




nickculbertson
Jan 24, 2011, 11:28 AM
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?

- (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



robbieduncan
Jan 24, 2011, 11:31 AM
The only way I can think of doing it would be to alter the zPosition (http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/zPosition) property of the buttons layer to position it above the other buttons.

dejo
Jan 24, 2011, 01:20 PM
Well, there's the possibility of UIView's bringSubviewToFront:

nickculbertson
Jan 24, 2011, 01:24 PM
The only way I can think of doing it would be to alter the zPosition (http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/zPosition) property of the buttons layer to position it above the other buttons.

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


- (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

robbieduncan
Jan 25, 2011, 04:19 AM
Well, there's the possibility of UIView's bringSubviewToFront:

I suspect that does the same thing in one handy call so is probably a better option, yes :)

dejo
Jan 25, 2011, 10:01 AM
I suspect that does the same thing in one handy call so is probably a better option, yes :)
Yeah, I wasn't sure if the OP was wanting to move the view to the very front or just higher up in the z-index, so that's why I said "possibly". :)