我想这两个视图之间翻转。 这很简单,代码如下,但我也想要同时翻转用于执行翻页的按钮。
你可以看到,当你播放曲目iPod的应用程序这种行为; 攻翻转按钮封面和曲目列表,之间翻转,但它同时翻转按钮。
这是导航控制器上的页面,按钮我要翻盖的rightBarButtonItem
。
这里是我到目前为止的代码。 这翻转的观点,但不是rightBarButton。
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.5f];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
showingBackside = !showingBackside;
if (showingBackside) {
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft
forView: self.view
cache: YES];
[self.view addSubview: backside.view];
[frontside.view removeFromSuperview];
} else {
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
forView: self.view
cache: YES];
[self.view addSubview: frontside.view];
[backside.view removeFromSuperview];
}
// flip image, too
NSString *newImage = showingBackside ? @"backside.png" : @"frontside.png";
[(self.navigationItem.rightBarButtonItem) setImage: [UIImage imageNamed: newImage]];
[UIView commitAnimations];
(这里的图像翻转代码可能无法编译,我加入之后,试图解释什么,我试图做的。)
当我遇到麻烦的是我要改变导航控制器最右边的按钮所以它同时翻转。
我该怎么做呢? 我什么动画视图,并做我这样做是因为相同的动画块单独的一个或一部分? 任何提示,将不胜感激,我绝对没有对动画很好地处理呢。
这里也有一些讨论, 在这里 ,但解决的办法也不是那么优雅。
首先,因为UIBarButtonItem
不是的后代UIView
,你可能无法直接对使用UIKit的动画UIBarButtonItem
。 但是,您可以尝试设置customView
和动画这一点。 您可以使用同样的动画块。
好了,这就是我确实是解决这个问题:
我已经使用自定义标题视图。 而不是使用rightBarButtonItem
,我做了我的自定义视图更宽。
我创建的按钮的两侧,完成与导航帧的图像,并把它们嵌入到应用程序。 在我的标题来看,我把:
- 一个
UIView
,这将是我的替代正确的控制(称之为rightControl
),适当定位。 - 在A键
UIView
,响应UIControlEventTouchUpInside
并触发我的flipSide:
。
在运行时创建一个UIImageView
每个状态。 我putboth UIImageView
S IN rightControl
,但隐藏了一个不违约。 我周围切换隐藏的旗帜flipSide:
在一个专门的动画块。
疯狂怪异。 但是,它的工作原理。
只需使用一个自定义的UIView为包含两个按钮之间翻转右侧导航按钮。
您可以使用创建显示为右导航按钮项自定义的UIView的直接方法。 这应该的UIView包含要与翻转两个UIButtons。 请记住,UIButtons使用的是正常的UI视图可以翻转,当然他们可以挖掘相同的转换UIViews也使他们能够翻转! 下面是一些工作示例代码:
注:我用一个方便的功能,以创建按钮的UIViewController中的自定义类别(注:您可以添加相同的代码来创建UIView的自定义类别太多,只是复制相同的路线,并与UIView的替代的UIViewController) - 如果你想使用它也只是通过包括下面这段代码创建一个自定义类别,交替您可以创建UIButtons像平时那样。
// create custom category for UIViewController to allow common button creation routine, add to .m or .mm file or add to a .h file and #import that .h file
@interface UIViewController (ButtonAndSelector)
- (UIButton *)createUIButtonWithImage:(UIImage *)image forState:(UIControlState)state withSelector:(SEL)selector usingFrame:(CGRect)buttonImageFrame;
@end
@implementation UIViewController (ButtonAndSelector)
- (UIButton *)createUIButtonWithImage:(UIImage *)buttonImage forState:(UIControlState)state withSelector:(SEL)selector usingFrame:(CGRect)buttonImageFrame
{
UIButton *button = [[UIButton alloc] initWithFrame:buttonImageFrame];
[button setBackgroundImage:buttonImage forState:state];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
return button;
}
@end
// add this to your .h file:
@property (strong, nonatomic) UIView *coverListView;
@property (strong, nonatomic) UIButton *listButton;
@property (strong, nonatomic) UIButton *coverButton;
- (void)animateCoverListButtonFlip;
// add this to your .m or .mm file to synthesize the variables:
@synthesize coverListView;
@synthesize listButton;
@synthesize coverButton;
// add this to your .m or .mm file in the viewDidLoad:
// setup right button bar (flips between list icon and coverart image)
self.coverListView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 30)];
self.coverListView.backgroundColor = [UIColor clearColor];
self.coverListView.opaque = NO;
self.listButton = [self createUIButtonWithImage:[UIImage imageNamed:@"navbar_icon_tracklisting"] forState:UIControlStateNormal withSelector:@selector(showHideQueue) usingFrame:CGRectMake(0, 0, 32, 30)];
self.listButton.backgroundColor = [UIColor clearColor];
self.listButton.showsTouchWhenHighlighted = NO;
self.coverButton = [self createUIButtonWithImage:[UIImage imageNamed:@"default_coverart_small"] forState:UIControlStateNormal withSelector:@selector(showHideQueue) usingFrame:CGRectMake(0, 0, 32, 30)];
[self.coverListView addSubview:self.coverButton]; // show coverButton by default
self.coverButton.showsTouchWhenHighlighted = NO;
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.coverListView];
[self.navigationItem setRightBarButtonItem:barButtonItem];
// add this to viewDidAppear if you want to flip the button when the screen appears like the build in iPod app does
[self animateCoverListButtonFlip];
// add this routine to flip the right navigation bar custom view / buttons
- (void)animateCoverListButtonFlip
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:([self.listButton superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.coverListView cache:YES];
if ([self.listButton superview]) {
[self.listButton removeFromSuperview];
[self.coverListView addSubview:self.coverButton];
} else {
[self.coverButton removeFromSuperview];
[self.coverListView addSubview:self.listButton];
}
[UIView commitAnimations];
}
// when the playing album cover changes, remember to update the coverButton:
UIImage *artworkImage; // set this to the current playing album image
[self.coverButton setImage:artworkImage forState:UIControlStateNormal];
// don't forget to call the animateCoverListButtonFlip in the button click handler (shown above as showHideQueue) that shows and hides the cover/queue(list of album tracks0 - something like this:
- (void)showHideQueue
{
[self animateCoverListButtonFlip];
/* replace the code below that is commented out here with your own code that transitions between your cover view and your list view of album tracks, this code shows my transition and references views that are not part of this example/answer, but you don't need those - you'll have your own cover view (musicPlayerView) and list view (musicQueueView) to flip between.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:([musicQueueView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.contentView cache:YES];
if ([musicQueueView superview]) { // if music queue is displayed
[musicQueueView removeFromSuperview];
[self.contentView addSubview:musicPlayerView];
} else {
[musicPlayerView removeFromSuperview];
[self.contentView addSubview:musicQueueView];
[[musicQueueView queueTableView] reloadData];
}
[UIView commitAnimations];*/
}