UITabBarController - 如何制作简单开关的动画?
我对这件事还很陌生。这个应用程序只是我的第二个。
我有一个 TabBarController,有两种在视图之间切换的方法:
方法 1:使用选项卡栏图标。我已经提供了必要的委托回调。它是这样的:
/***************************************************************\**
\brief This animates the view transitions, and also sets up anything
that needs doing between views.
*****************************************************************/
- (BOOL)tabBarController:(UITabBarController *)inTabBarController
shouldSelectViewController:(UIViewController *)inViewController
{
BOOL ret = NO;
// Need to have all of these to work.
if ( inTabBarController && [inTabBarController view] && inViewController && [inViewController view] )
{
UIView *srcView = [[inTabBarController selectedViewController] view];
UIView *dstView = [inViewController view];
if ( srcView != dstView )
{
if ( srcView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlDown
completion:nil];
}
else if ( dstView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlUp
completion:nil];
}
else if ( srcView == [listSearchController view] && dstView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
else if ( dstView == [listSearchController view] && srcView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
}
ret = YES;
}
}
return ret;
}
效果很好。
方法 2:抓住滑动,并以这种方式触发转换,如下所示:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[tabBarController setSelectedIndex:1];
}
这样做的问题是我无法让转换起作用。如果我将转换代码添加到滑动处理程序中,如下所示:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[UIView transitionFromView:[listSearchController view]
toView:[mapSearchController view]
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
[tabBarController setSelectedIndex:1];
}
它第一次工作,但在后续操作中出现蛇眼。
我确信我在这里犯了一些基本的、可笑的错误,并且正在寻找线索。
线索,有人吗?
谢谢!
I'm still fairly new at this stuff. This app is only my second.
I have a TabBarController, and there are two ways of switching between the views:
METHOD 1: Use the Tab Bar Icons. I've provided the requisite delegate callback. It is something like this:
/***************************************************************\**
\brief This animates the view transitions, and also sets up anything
that needs doing between views.
*****************************************************************/
- (BOOL)tabBarController:(UITabBarController *)inTabBarController
shouldSelectViewController:(UIViewController *)inViewController
{
BOOL ret = NO;
// Need to have all of these to work.
if ( inTabBarController && [inTabBarController view] && inViewController && [inViewController view] )
{
UIView *srcView = [[inTabBarController selectedViewController] view];
UIView *dstView = [inViewController view];
if ( srcView != dstView )
{
if ( srcView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlDown
completion:nil];
}
else if ( dstView == [prefsController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionCurlUp
completion:nil];
}
else if ( srcView == [listSearchController view] && dstView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
else if ( dstView == [listSearchController view] && srcView == [mapSearchController view] )
{
[UIView transitionFromView:srcView
toView:dstView
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
}
ret = YES;
}
}
return ret;
}
Works great.
METHOD 2: Grab swipes, and trigger the transitions that way, like so:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[tabBarController setSelectedIndex:1];
}
The problem with this, is that I can't get the transition to work. If I add the transition code to the swipe handler, like so:
/***************************************************************\**
\brief Gesture Callback -Swipes from the List View to the Map View
*****************************************************************/
- (IBAction)swipeFromList:(UIGestureRecognizer *)sender
{
[UIView transitionFromView:[listSearchController view]
toView:[mapSearchController view]
duration:0.25
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
[tabBarController setSelectedIndex:1];
}
It works the first time, but comes up snake eyes on subsequent goes.
I'm sure that I am making some basic, chuckleheaded error here, and am searching for clues.
Clues, anyone?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇。我解决了它,我不太清楚为什么,但它有效。
这就是我所做的:
首先,我将转换分解为标准方法:
接下来,我在标签栏转换发生之前拦截它,并覆盖它,如下所示:
然后,我将以下代码添加到滑动陷阱中:
现在,这里的问题是,当我返回到我滑动的视图(不使用选项卡栏)时,我总是会崩溃。崩溃表明目标视图已被取消分配。
在检查了一些鸡内脏后,我决定我需要捂住鼻子,并保留之前的观点,就像这样:
可以。我用仪器检查过。没有泄漏。
Wow. I solved it, and I am not exactly sure why, but it works.
Here's what I did:
First, I factored the transitions into a standard method:
Next, I intercept the tab bar transition before it happens, and override it, like so:
Then, I added the following code to the swipe traps:
Now, the problem here, was that I always got a crash when returning to a view that I had swiped from (not using the tab bar). The crash said that the destination view had been deallocated.
After checking some chicken entrails, I decided that I needed to hold my nose, and retain the previous view, like so:
That works. I checked it with Instruments. No leaks.
此代码运行良好 **
This code is working fine **