代码之家  ›  专栏  ›  技术社区  ›  Steven Fisher

iPhone右翻转按钮(如iTunes)

  •  5
  • Steven Fisher  · 技术社区  · 16 年前

    我正试图在两种观点之间转换。这很简单,代码在下面,但我还想同时翻转用于执行翻转的按钮。

    当您播放曲目时,您可以在iPod应用程序中看到这种行为;点击翻页按钮会在封面和曲目列表之间翻转,但同时也会翻转按钮。

    这是导航控制器上的一个页面,我要翻转的按钮是 rightBarButtonItem .

    这是我到目前为止的代码。这将翻转视图,但不是右键按钮。

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

    (这里的图像翻转代码可能无法编译;我在后面添加了它,试图解释我要做的事情。)

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

    我该怎么做?我要制作什么视图的动画,是作为同一动画块的一部分还是作为单独的一个动画块?如果有什么建议,我肯定还没有很好的动画处理能力。

    3 回复  |  直到 13 年前
        1
  •  5
  •   Can Berk Güder Pugalmuni    16 年前

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

    首先,从 UIBarButtonItem 不是的后代 UIView ,您可能无法直接在 栏按钮项 . 但是,您可以尝试设置 customView 并以此为动画。可以使用相同的动画块。

        2
  •  2
  •   Steven Fisher    16 年前

    好吧,我是这么做的:

    我已经在使用自定义标题视图。而不是使用 rightBarButtonItem ,我扩大了我的自定义视图。

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

    • UIView 那将是我的替代品 rightControl ,位置适当。
    • 一个按钮在 UIVIEW 响应 UIControlEventTouchUpInside 触发我的 flipSide: .

    在运行时,我创建一个 UIImageView 对于每个州。我把两个都扔了 UIIVIEVIEW S在 权利控制 ,但隐藏非默认值。我把隐藏的旗帜 翻转: 在专用动画块中。

    非常奇怪。但它有效。

        3
  •  2
  •   Richie Hyatt    13 年前

    只需对包含两个按钮的右导航按钮使用自定义uiview即可在其中切换。

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

    注意:我使用一个便利功能创建按钮作为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];*/
            }
    
    推荐文章