简体   繁体   English

从选项卡栏弹出的图标

[英]Icons that Popup from Tab Bar

I am curious as to the proper way to make icons popup from a tab bar. 我对从标签栏弹出图标的正确方法感到好奇。 For example, the Food Spotting app (please see below). 例如,Food Spotting应用程序(请参阅下文)。 Has three icons that spring up from the center tab bar item if you click on it. 如果单击该图标,则会从中心选项卡栏项目弹出三个图标。

Is there a standard way to go about doing something like this? 是否有标准的方法可以执行这样的操作? Thank you! 谢谢!

在此处输入图片说明

I think the general consensus is that for the first step, you should place a UIButton over the center tab bar button. 我认为普遍的共识是,第一步,您应该将UIButton放在中心标签栏按钮上。 The code gazzola linked to seems helpful for this. 链接到gazzola的代码似乎对此有所帮助。 The next step is to get a handle on the center button touch and animate the new buttons out of it. 下一步是获得中心按钮触摸的手柄,并为其中的新按钮设置动画。

The following code shows how to create the new buttons and animate them in a similar fashion to the Food Spotting app. 以下代码显示了如何创建新按钮并以与Food Spotting应用类似的方式对其进行动画处理。

Assuming you have two properties defined: 假设您定义了两个属性:

@property (nonatomic, weak) IBOutlet UIButton *ourExpandButton;
@property (nonatomic, assign) BOOL buttonsExpanded;

The first being an outlet to the center button, and the second a boolean for determining if the buttons are showing. 第一个是中央按钮的出口,第二个是用于确定按钮是否显示的布尔值。

- (void)viewDidAppear:(BOOL)animated
{
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];

    button1.backgroundColor = [UIColor redColor];
    button2.backgroundColor = [UIColor blueColor];
    button3.backgroundColor = [UIColor yellowColor];

    // Make the buttons 0 width and height so when we animate they seem to grow
    // Position them in the center of the button that expands them
    button1.frame = CGRectMake(self.ourExpandButton.center.x, self.ourExpandButton.center.y, 0.0, 0.0);
    button2.frame = CGRectMake(self.ourExpandButton.center.x, self.ourExpandButton.center.y, 0.0, 0.0);
    button3.frame = CGRectMake(self.ourExpandButton.center.x, self.ourExpandButton.center.y, 0.0, 0.0);

    [button1 addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];

    button1.tag = 101;
    button2.tag = 102;
    button3.tag = 103;

    [self.view addSubview:button1];
    [self.view addSubview:button2];
    [self.view addSubview:button3];
}

In the viewDidAppear I'm just setting up the buttons, which could all be done on the storyboard. 在viewDidAppear中,我只是设置按钮,所有这些都可以在情节提要上完成。

The following method would be tied to the touchUpInside event of the expand button. 以下方法将与扩展按钮的touchUpInside事件相关联。

- (IBAction)expandButtonTouched:(id)sender
{
    if (!self.buttonsExpanded) {
        [UIView animateWithDuration:0.25 animations:^{
            float screenWidth = self.view.frame.size.width;
            float screenHeight = self.view.frame.size.height;
            [[self.view viewWithTag:101] setFrame:CGRectMake((screenWidth/3 - 22.0), (screenHeight - 100), 44.0, 44.0)];
            [[self.view viewWithTag:102] setFrame:CGRectMake((screenWidth/2 - 22.0), (screenHeight - 150), 44.0, 44.0)];
            [[self.view viewWithTag:103] setFrame:CGRectMake((2*screenWidth/3 - 22.0), (screenHeight - 100), 44.0, 44.0)];
        } completion:^(BOOL finished) {
            self.buttonsExpanded = YES;
        }];
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            [[self.view viewWithTag:101] setFrame:CGRectMake(self.ourExpandButton.center.x, self.ourExpandButton.center.y, 0.0, 0.0)];
            [[self.view viewWithTag:102] setFrame:CGRectMake(self.ourExpandButton.center.x, self.ourExpandButton.center.y, 0.0, 0.0)];
            [[self.view viewWithTag:103] setFrame:CGRectMake(self.ourExpandButton.center.x, self.ourExpandButton.center.y, 0.0, 0.0)];
        } completion:^(BOOL finished) {
            self.buttonsExpanded = NO;
        }];
    }
}

This animates the buttons out radially or in a semi-circle, and they grow as they move. 这将使按钮呈放射状或半圆形动画,并且它们会随着移动而增长。 Note that you should figure out where exactly you want the buttons to animate to and adjust the numbers accordingly. 请注意,您应该弄清楚按钮要设置动画的确切位置,并相应地调整数字。 Also obviously the buttons should be given an image instead of just a background color. 同样显然,应该给按钮赋予图像,而不只是背景色。

This code doesn't add the bounce animation that Food Spotting has, where the buttons go past their destination and recoil back. 此代码不添加Food Spotting所具有的弹跳动画,在这些动画中,按钮会越过目的地并后退。 To add that, just change the button frames in the animation to the furthest you want the buttons to go, then add another animation in the completion block that moves them to their final destinations. 要添加此内容,只需将动画中的按钮帧更改为您想要按钮移动的最远位置,然后在完成块中添加另一个动画,即可将其移动到最终目标。

I would post an image of the final result, but I don't have enough reputation. 我会发布最终结果的图片,但我的声誉不高。

查看此代码有助于半圆显示

There is an example here ( git code ) showing how to make centered tabbar buttons. 这里有一个示例( git code )显示了如何使居中的标签栏按钮。 It is a nice start for what you need, them you should check the codes posted by Dhara and Rushabh. 这是您需要的一个不错的开始,您应该检查Dhara和Rushabh发布的代码。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM