在iPhone应用程序中旋转UIToolBar上的按钮

时间:2009-11-19 22:53:59

标签: iphone iphone-sdk-3.0

有没有办法旋转UIToolBar上的按钮,因为相机应用程序在横向拍照时会这样做?

当用户旋转iPhone时,我想让工具栏保持在同一个位置并旋转按钮,使它们不会侧身。

提前感谢!

2 个答案:

答案 0 :(得分:2)

我不相信有任何内置的东西可以帮你处理。但是,它绝对可以编码。 1)听取消息,告诉您方向正在改变。不允许视图旋转(这会使您的工具栏无法移动),但可以利用这个机会做其他事情 2)用适当的方向旋转90度的图像替换按钮中的图像 3)在执行旋转动画的Core Animation的上下文中进行图像替换。您应该能够找到一些代码示例来了解如何执行此操作。

希望能帮助您朝着正确的方向前进。

答案 1 :(得分:1)

我也必须这样做。像肯建议的那样做替换图像,我找不到一个动画轮换动画的好方法。最好能够在项目的图像上设置变换,这对于简单的UIBarButtonItem是无法做到的,但您可以使用自定义视图构建变换:

代替[[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];

然后你可以这样做:

[UIView beginAnimations:@"rotate barbuttonitems" context:NULL];
item.customView.transform = CGAffineTransformMakeRotation(M_PI_2);
[UIView commitAnimations];