【问题标题】:iPhone flip right button (like iTunes)iPhone 向右翻转按钮(如 iTunes)
【发布时间】:2012-04-29 08:28:34
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: iphone core-animation rightbarbuttonitem


    【解决方案1】:

    有一些讨论here,但解决方案不是那么优雅。

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

    【讨论】:

    • 一夜之间我意识到这实际上是一个比我认为的更好的主意。我想我可以捕捉到足够多的图像来使 UIImageView 看起来像一个按钮,这意味着我可以完成它。谢谢。
    【解决方案2】:

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

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

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

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

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

    非常奇怪。但它有效。

    【讨论】:

    • 你能分享一些代码吗?与类似的事情作斗争
    【解决方案3】:

    只需为右侧导航按钮使用自定义 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];*/
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2023-04-10
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多