gpt4 book ai didi

iphone - 获取特定标签栏项目的框架

转载 作者:IT王子 更新时间:2023-10-29 07:44:02 26 4
gpt4 key购买 nike

有没有办法在 UITabBar 中找到特定 UITabBarItem 的框架?

具体来说,我想创建一个图像“落入”其中一个选项卡的动画,类似于例如在邮件中删除电子邮件,或在 iTunes 应用程序中购买轨道。所以我需要动画的目标坐标。

据我所知,没有公共(public) API 来获取坐标,但我很乐意在这一点上弄错。除此之外,我将不得不使用给定选项卡相对于选项卡栏框架的索引来猜测坐标。

最佳答案

Imre 的实现缺少几个恕我直言的重要细节。

  1. UITabBarButton View 不一定按顺序排列。例如,如果您在 iPhone 上有超过 5 个选项卡并且重新排列了选项卡,则 View 可能会乱序。
  2. 如果您使用超过 5 个选项卡,则越界索引仅表示该选项卡在“更多”选项卡后面。在这种情况下,没有理由使用断言失败,只需使用最后一个选项卡的框架即可。

所以我稍微修改了他的代码,然后我想出了这个:

+ (CGRect)frameForTabInTabBar:(UITabBar*)tabBar withIndex:(NSUInteger)index
{
NSMutableArray *tabBarItems = [NSMutableArray arrayWithCapacity:[tabBar.items count]];
for (UIView *view in tabBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")] && [view respondsToSelector:@selector(frame)]) {
// check for the selector -frame to prevent crashes in the very unlikely case that in the future
// objects thar don't implement -frame can be subViews of an UIView
[tabBarItems addObject:view];
}
}
if ([tabBarItems count] == 0) {
// no tabBarItems means either no UITabBarButtons were in the subView, or none responded to -frame
// return CGRectZero to indicate that we couldn't figure out the frame
return CGRectZero;
}

// sort by origin.x of the frame because the items are not necessarily in the correct order
[tabBarItems sortUsingComparator:^NSComparisonResult(UIView *view1, UIView *view2) {
if (view1.frame.origin.x < view2.frame.origin.x) {
return NSOrderedAscending;
}
if (view1.frame.origin.x > view2.frame.origin.x) {
return NSOrderedDescending;
}
NSAssert(NO, @"%@ and %@ share the same origin.x. This should never happen and indicates a substantial change in the framework that renders this method useless.", view1, view2);
return NSOrderedSame;
}];

CGRect frame = CGRectZero;
if (index < [tabBarItems count]) {
// viewController is in a regular tab
UIView *tabView = tabBarItems[index];
if ([tabView respondsToSelector:@selector(frame)]) {
frame = tabView.frame;
}
}
else {
// our target viewController is inside the "more" tab
UIView *tabView = [tabBarItems lastObject];
if ([tabView respondsToSelector:@selector(frame)]) {
frame = tabView.frame;
}
}
return frame;
}

关于iphone - 获取特定标签栏项目的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com