iphone - 从标签栏弹出的图标

标签 iphone ios objective-c xcode uitabbarcontroller

我很好奇从标签栏弹出图标的正确方法。例如,Food Spotting 应用程序(请参见下文)。具有三个图标,如果您单击它,它们会从中心选项卡栏项目中弹出。

有没有一种标准的方法来做这样的事情?谢谢!

enter image description here

最佳答案

我认为普遍的共识是,对于第一步,您应该将 UIButton 放在中心标签栏按钮上。链接到的代码 gazzola 似乎对此有所帮助。下一步是获取中心按钮触摸的句柄,并为新按钮制作动画。

以下代码展示了如何创建新按钮并以类似于 Food Spotting 应用的方式为它们制作动画。

假设您定义了两个属性:

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

第一个是中心按钮的导出,第二个是确定按钮是否显示的 bool 值。

- (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];
}

在 viewDidAppear 中,我只是设置按钮,这些都可以在 Storyboard上完成。

以下方法将绑定(bind)到展开按钮的 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;
        }];
    }
}

这会使按钮呈放射状或呈半圆形动画,并且它们会随着移动而变大。请注意,您应该弄清楚您希望按钮动画的确切位置并相应地调整数字。同样显然,应该为按钮提供图像而不仅仅是背景颜色。

此代码未添加 Food Spotting 所具有的弹跳动画,即按钮经过其目的地并弹回的动画。要添加它,只需将动画中的按钮帧更改为您希望按钮移动的最远位置,然后在完成 block 中添加另一个动画,将它们移动到最终目的地。

我会发布最终结果的图片,但我没有足够的声誉。

关于iphone - 从标签栏弹出的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15407984/

相关文章:

objective-c - 以编程方式在委托(delegate)中调用 Storyboard

iphone - [UIWebView loadRequest] 是否阻塞线程?

iphone - 重置 UITextField 左边距

ios - NSOperationQueue 与同步 NSURLConnection

ios - 使用 getdatainbackground 从 Parse 检索多个图像(IOS - Swift)

ios - 还押资源 - 预计时间(以及如何根据下载进度显示提醒)

iphone - GKPlayer、游戏中心、头像

ios - Xcode:在多人之间共享相同的 Bundle ID

iphone - 滑动浏览一堆图像?

objective-c - MKAnnotationView 帧大小在缩放之前不会更新 MapView