iOS swift : Transition Animation on same View Controller

标签 ios iphone swift uiviewcontroller

我怎样才能在单个 View Controller 上获得过渡动画效果(如页面幻灯片) - 这样看起来我正在过渡到一个新页面/ Controller (尽管在同一个 View 中只有数据发生变化)?

例如:我有一个 UIViewController,它有 Previous 和 Next 按钮。单击下一步这些按钮时,不同的标题和图像将显示在“相同UIViewController 上。

我知道这可以通过 UIPageViewController 来完成 - 但我是 iOS 的新手,发现以这种方式实现相当困难。

最佳答案

我也有同样的情况。但是因为我们不希望有复杂的幻灯片动画,所以我不会实现 UIPageViewController。相反,我提出了以下解决方案:

  • 想法是创建快照 View 并在其上启动动画,同时在用户开始滑动时使用新数据更新 View 。通过这样做,我们可以让人感觉我们正在转换到另一个 View 。

  • 下面的示例代码是用 Xamarin.iOS 编写的,但我相信它可以很容易地转移到 Swift/Obj-C:

            UIView snapShotView = View.SnapshotView(false); // Create snapshot for the view (I assumed that View is the container)
            View.Add(snapShotView);
            snapShotView.Frame = View.Frame; //Add snapshot and set its frame so the snapshot will be overlapped the original View
    
            CGRect snapshotEndFrame = snapShotView.Frame;
            snapshotEndFrame.X = -snapShotView.Frame.Width;
    
            UIView.Animate(0.7, 0, UIViewAnimationOptions.CurveEaseInOut,
            () =>
            {
                //Load new data to your View
                ...
    
                //Animate the snapshot view
                snapShotView.Frame = snapshotEndFrame;
            },
            () =>
            {
                //Remove Snap Shot View after doing animation to prevent memory leak
                snapShotView.RemoveFromSuperview();
                snapShotView.Dispose();
            });
    

关于iOS swift : Transition Animation on same View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30466168/

相关文章:

generics - 您能否扩展具有特定约束的通用类(例如 Array)?

c# - 如何用 Device.OnPlatform 翻译这个 Padding? (iOS 案例)

ios - 在 ContainerView 内的 UICollectionView 中计算单元格宽度

ios - 如何将用delphi berlin制作的iOS应用程序部署到App store?

iphone - 单元测试和 TDD、OCUnit 与 Google Tool Box

iphone - 在iPhone上使用UIWebView禁用全屏youtube视频显示

iphone - 使用特定日期将文本保存在 plist 文件中

uitableview - 不想在播放流媒体后等待 3 秒调用 didSelectRowAtIndexPath

ios - 减少png图像的文件大小

android - flutter : Publish App Privately