Twitter 风格的 UITabBarController?

发布于 2024-09-18 08:09:49 字数 1043 浏览 4 评论 0原文

我想制作一款使用 UITabBarController 的应用程序,该应用程序类似于适用于 iPhone 和 iPod touch 的 Twitter 应用程序中的应用程序。 (蓝光表示未读,滑动箭头用于在内容视图之间切换)。

有没有简单的方法可以做到这一点?有开源代码吗?

编辑:

我添加了赏金。我正在寻找一个适用于跨设备以及 iOS 4 和 iOS 5 的答案。

编辑:

我搞乱了我的原始代码,并找到了一个简单的解决方案。我在我的动画代码中为 iOS 5 添加了以下检查:

//  iOS 5 changes the subview hierarchy 
//  so we need to check for it here

BOOL isUsingVersionFive = NO;
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
    //On iOS 5, otherwise use old index
    isUsingVersionFive = YES;
}

然后,而不是这个:

CGFloat tabMiddle = CGRectGetMidX([[[[self tabBar] subviews] objectAtIndex:index] frame]);

... 我使用这个:

CGFloat tabMiddle = CGRectGetMidX([[[[self tabBar] subviews] objectAtIndex:index + (isUsingVersionFive ? 1 : 0)] frame]);

尽管如此,仍然坚持赏金,以防我有机会找到可以在答案。

I'd like to make an app that uses a UITabBarController that is similar to the one in the Twitter app for iPhone and iPod touch. (Blue light for unread and sliding arrow for switching between content views).

Is there an easy way to do this? Any open source code?

Edit:

I've added a bounty. I'm looking for an answer that works across devices and on iOS 4 and iOS 5.

EDIT:

I've messed with my original code and I found a simple solution. I've added the following check, for iOS 5 in my animation code:

//  iOS 5 changes the subview hierarchy 
//  so we need to check for it here

BOOL isUsingVersionFive = NO;
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
    //On iOS 5, otherwise use old index
    isUsingVersionFive = YES;
}

Then, instead of this:

CGFloat tabMiddle = CGRectGetMidX([[[[self tabBar] subviews] objectAtIndex:index] frame]);

... I use this:

CGFloat tabMiddle = CGRectGetMidX([[[[self tabBar] subviews] objectAtIndex:index + (isUsingVersionFive ? 1 : 0)] frame]);

Still holding out on the bounty though, in case I get a chance to find something that works in the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深居我梦 2024-09-25 08:09:49

我将创建标准 UITabBarController 的子类,并为蓝光和滑动箭头添加几个子视图。应该不会太难完成。

编辑:
以下是您的子类中应该包含的一些内容的想法。我什至不知道这段代码是否可以编译。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        super.delegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //Create blueLight
    UIImage* img = [UIImage imageNamed:@"blue_light.png"]
    self.blueLight = [[UIImageView alloc] initWithImage:img];
    self.blueLight.center = CGPointMake(320, 460); //I'm using arbitrary numbers here, position it correctly
    [self.view addSubview:self.blueLight];
    [self.blueLight release];

    //Create arrow, similar code.
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //Put code here that moves (animates?) the blue light and the arrow


    //Tell your delegate, what just happened.
    if([myDelegate respondsToSelector:@selector(tabBarController:didSelectViewController:)]){
        [myDelegate tabBarController:self didSelectViewController:viewController]
    }
}

I would create a subclass of the standard UITabBarController and add a couple of subviews for the blue light and the sliding arrow. Shouldn't be too hard to accomplish.

Edit:
Here's an idea of some of the stuff that should go in your subclass. I don't even know if this code will compile.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        super.delegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //Create blueLight
    UIImage* img = [UIImage imageNamed:@"blue_light.png"]
    self.blueLight = [[UIImageView alloc] initWithImage:img];
    self.blueLight.center = CGPointMake(320, 460); //I'm using arbitrary numbers here, position it correctly
    [self.view addSubview:self.blueLight];
    [self.blueLight release];

    //Create arrow, similar code.
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //Put code here that moves (animates?) the blue light and the arrow


    //Tell your delegate, what just happened.
    if([myDelegate respondsToSelector:@selector(tabBarController:didSelectViewController:)]){
        [myDelegate tabBarController:self didSelectViewController:viewController]
    }
}
甲如呢乙后呢 2024-09-25 08:09:49

以下是创建箭头的方法

http://isagoksu.com/ 2009/development/iphone/fancy-uitabbar-like-tweetie/

并按照 Rafael 的回答获取蓝光。

Here is how you create the arrow

http://isagoksu.com/2009/development/iphone/fancy-uitabbar-like-tweetie/

And follow Rafael's answer to get the blue light.

叹沉浮 2024-09-25 08:09:49

虽然是一篇老化的帖子,但我想我会插入一个没有人提到的 GitHub 项目,他们很好地重新创建了 Twitter 风格的选项卡栏,这里是链接:

https://github.com/boctor/idev-recipes/tree/master/CustomTabBar

该项目位于名为“CustomTabBar”的子文件夹中”,更多信息可以在这里找到:

Although an ageing post, I though I would interject with a GitHub project that no one mentioned, they've recreated the Twitter styled tab bar quite well, here is the link:

https://github.com/boctor/idev-recipes/tree/master/CustomTabBar

The project is in the sub folder named "CustomTabBar", more information can be found here:

http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/

我纯我任性 2024-09-25 08:09:49

BCTabBarController 是我自定义 Twitter 样式标签栏的基础,它适用于 iOS 4 和 5:
http://pastebin.me/f9a876a6ad785cb0b2b7ad98b1024847

每个选项卡视图控制器应该为选项卡图像实现以下方法

- (NSString *)iconImageName {
    return @"homeTabIconImage.png";
}

:(在应用程序委托中),您可以像这样初始化选项卡栏控制器:

self.tabBarController = [[[JHTabBarController alloc] init] autorelease];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneHomeViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneAboutUsViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneContactInfoViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneMapUsViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneCreditsViewController alloc] init] autorelease]] autorelease],
                                             nil];

可选,您可以使用以下方法为每个选项卡视图控制器使用自定义背景图像:

- (UIImage *)tabBarController:(JHTabBarController *)tabBarController backgroundImageForTabAtIndex:(NSInteger)index {
    NSString *bgImagefilePath;
    switch (index) {
        case 0:
            bgImagefilePath = @"homeBG.png";
            break;
        case 1:
            bgImagefilePath = @"aboutBG.png";
            break;
        case 2:
            bgImagefilePath = @"contactBG.png";
            break;
        case 3:
            bgImagefilePath = @"mapBG.png";
            break;
        case 4:
            bgImagefilePath = @"creditsBG.png";
            break;
        default:
            bgImagefilePath = @"homeBG.png";
            break;
    }
    return [UIImage imageNamed:bgImagefilePath];
}

选项卡栏顶部的箭头滑动就像 Twitter 选项卡栏一样,转到所选的选项卡。

一些屏幕截图:

BCTabBarController
BCTabBarController

BCTabBarController is what I based my custom twitter style tab-bar on and it works in iOS 4 and 5:
http://pastebin.me/f9a876a6ad785cb0b2b7ad98b1024847

Each tab view controller should implement the following method for the tabs image:

- (NSString *)iconImageName {
    return @"homeTabIconImage.png";
}

(In the App delegate) you would initialize the tab bar controller like so:

self.tabBarController = [[[JHTabBarController alloc] init] autorelease];
self.tabBarController.delegate = self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneHomeViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneAboutUsViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneContactInfoViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneMapUsViewController alloc] init] autorelease]] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:[[[iPhoneCreditsViewController alloc] init] autorelease]] autorelease],
                                             nil];

Optionally, you can use a custom background image for each tabs view controller with this method:

- (UIImage *)tabBarController:(JHTabBarController *)tabBarController backgroundImageForTabAtIndex:(NSInteger)index {
    NSString *bgImagefilePath;
    switch (index) {
        case 0:
            bgImagefilePath = @"homeBG.png";
            break;
        case 1:
            bgImagefilePath = @"aboutBG.png";
            break;
        case 2:
            bgImagefilePath = @"contactBG.png";
            break;
        case 3:
            bgImagefilePath = @"mapBG.png";
            break;
        case 4:
            bgImagefilePath = @"creditsBG.png";
            break;
        default:
            bgImagefilePath = @"homeBG.png";
            break;
    }
    return [UIImage imageNamed:bgImagefilePath];
}

The arrow at the top of the tab bar slides across to the tab that is selected just like the twitter tab bar.

Some screenshots:

BCTabBarController
BCTabBarController

ぽ尐不点ル 2024-09-25 08:09:49

仅仅两天我就为 POC 制作了一款这样的应用程序,事实证明它比你想象的要容易。

首先在我的 applicationdidfinishlaunching: 方法中,我添加了图像视图,其中包含宽度为 64 的指针或箭头图像,因为屏幕宽度为 320,并且选项卡栏中有 5 个选项卡。如果您认为您的项目将具有动态选项卡,然后只需相应地更改图像视图的宽度,您也可以计算它,如下所示。

CGRect rect = tabBarpointerImageView.frame;
rect.size.width = sel.window.frame.size.width / tabbarcontroller.viewControllers.count;
tabBarpointerImageView.frame = rect;

您可能还想将该图像视图的内容模式设置为 uiviewcontentmodecenter
另外,我还通过像这样的计算设置了图像视图的框架

CGRect rect = CGRectZero;
rect.size.width = self.window.frame.size.width / tabbarcontroller.viewControllers.count;
rect.size.height = 10;// as image was of 10 pixel height.
rect.origin.y = self.window.frame.size.height - 49;// as the height of tab bar is 49
tabBarpointerImageView.frame = rect;

,然后在选项卡栏控制器的委托方法中,

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

只需使用动画块来为指针的移动设置动画,就像这样的

[uiview beginanimation:nil context:nil];
CGRect rect = tabBarpointerImageView.frame;
rect.origin.x = rect.size.width * tabbarcontroller.selectedIndex;
tabBarpointerImageView.frame = rect;
[uiview commitanimations];

PS。
抱歉,如果某些拼写不正确。

Just 2day I made one app like this one for POC, it turns out that it is easier than you can think.

First in my applicationdidfinishlaunching: method I added the imageview holding the image of pointer or arrow with the width as 64 as the screen width is 320 and I have 5 tabs in the tab bar. you can also calculate it if you think that your project will have dynamic tabs and then just change the width of the imageview accordingly something like this.

CGRect rect = tabBarpointerImageView.frame;
rect.size.width = sel.window.frame.size.width / tabbarcontroller.viewControllers.count;
tabBarpointerImageView.frame = rect;

You might also want to set the content mode of that image view as uiviewcontentmodecenter
Also I have set the frame of the imageview by some calculation like this

CGRect rect = CGRectZero;
rect.size.width = self.window.frame.size.width / tabbarcontroller.viewControllers.count;
rect.size.height = 10;// as image was of 10 pixel height.
rect.origin.y = self.window.frame.size.height - 49;// as the height of tab bar is 49
tabBarpointerImageView.frame = rect;

And then in the delegate method of tab bar controller

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

just use an animation block to animate the movement of pointer like this

[uiview beginanimation:nil context:nil];
CGRect rect = tabBarpointerImageView.frame;
rect.origin.x = rect.size.width * tabbarcontroller.selectedIndex;
tabBarpointerImageView.frame = rect;
[uiview commitanimations];

PS.
Sorry if some spellings are not correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文