Rotating buttons on UIToolBar in an iPhone app
Is there any way to rotate the buttons on a UIToolBar as the camera application does when you take a picture in landscape?
When the user rotates the iP开发者_开发知识库hone I want to keep the toolbar on the same place and rotate the buttons so they don't stand sideways.
thanks in advance!
I don't believe there's anything built-in to handle this for you. However, it's definitely possible to code this.
1) Listen for the message telling you the orientation is changing. Don't allow the view to rotate (this will keep your toolbar from moving), but use the opportunity to do other stuff.
2) Replace the images in the buttons with images rotated 90 degrees in whichever direction is appropriate.
3) Do the image replacement within the context of a Core Animation which performs a rotation animation. You should be able to find some code samples for how to do this.
Hope that helps you look in the right direction.
i had to do this too. doing replace-the-image like Ken suggested, i couldn't find a good way to animate the rotation. it's best to be able to set the transform on the item's image, which you can't do to a simple UIBarButtonItem but you can with one built with a custom view:
in place of [[UIBarButtonItem alloc] initWithImage:image target:t action:a] :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:t action:a forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES; // makes it highlight like normal
item = [[UIBarButtonItem alloc] initWithCustomView:button];
then you can do:
[UIView beginAnimations:@"rotate barbuttonitems" context:NULL];
item.customView.transform = CGAffineTransformMakeRotation(M_PI_2);
[UIView commitAnimations];
精彩评论