5

我试图在两个视图之间切换。这很简单,代码如下,但我还想同时翻转用于执行翻转的按钮。

当您播放曲目时,您可以在 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];

(此处的图像翻转代码可能无法编译;我在尝试解释我要做什么之后添加了它。)

我遇到麻烦的地方是我想更改导航控制器中最右边的按钮,以便它同时翻转。

我该怎么做呢?我要为哪个视图制作动画,是作为同一个动画块的一部分还是单独的动画块?任何提示将不胜感激,我肯定还没有很好地处理动画。

4

3 回答 3

5

这里有一些讨论,但解决方案并不那么优雅。

首先,由于UIBarButtonItem不是 的后代UIView,您可能无法直接在UIBarButtonItem. 但是,您可以尝试设置 acustomView并为其设置动画。您可以使用相同的动画块。

于 2009-07-17T00:57:14.580 回答
2

好的,这是我实际解决此问题的方法:

我已经在使用自定义标题视图。我没有使用rightBarButtonItem,而是使我的自定义视图更宽。

我创建了按钮两侧的图像,包括导航框架,并将它们嵌入到应用程序中。在我的标题视图中,我放了:

  • AUIView将替代我的正确控件(称为它rightControl),放置适当。
  • 上的一个按钮UIView响应UIControlEventTouchUpInside并触发我的flipSide:.

在运行时,我为每个状态创建一个UIImageView。我把两个都UIImageView放在了rightControl,但隐藏了一个不是默认的。flipSide:我在专用动画块中切换隐藏的标志。

诡异无比。但它有效。

于 2009-07-17T16:19:34.040 回答
2

只需为包含两个按钮的右侧导航按钮使用自定义 UIView 即可。

您可以使用直接的方法来创建显示为右侧导航按钮项的自定义 UIView。此 UIView 应包含您要在其间翻转的两个 UIButton。请记住,UIButton 也是 UIView,因此它们可以使用与普通 UI 视图可以翻转的相同转换来翻转,当然它们也可以被点击!这是一些有效的示例代码:

注意:我使用便利功能将按钮创建为 UIViewController 的自定义类别(注意:您也可以添加相同的代码来为 UIView 创建自定义类别,只需复制相同的行并将 UIViewController 替换为 UIView) - 如果你想也可以使用它,只需通过在下面包含此代码来创建自定义类别,或者您可以像往常一样创建 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];*/
        }
于 2012-04-29T00:14:09.220 回答