【问题标题】:Swift Fade In Animation for Background Animation between A Single View to a PageView ControllerSwift Fade In Animation 用于单个视图到 PageView 控制器之间的背景动画
【发布时间】:2015-02-03 02:04:13
【问题描述】:

所以我试图在主视图控制器中单击按钮后,为页面视图滚动器中的 UI 图像创建淡入动画。这是我的主要故事板。

class MainWorkoutViewController: UIViewController {
    // Outlet used in storyboard
    @IBOutlet var scrollView: UIScrollView?;

    override func viewDidLoad() {
        super.viewDidLoad();


        func scrollViewDidEndDragging(MainWorkoutViewController: UIScrollView,
            withVelocity: CGPoint,
            targetContentOffset : UnsafeMutablePointer<CGPoint>){

        }

        // 1) Create the three views used in the swipe container view
        var AVc :AViewController =  AViewController(nibName: "AViewController", bundle: nil);
        var BVc :BViewController =  BViewController(nibName: "BViewController", bundle: nil);
        var CVc :CViewController =  CViewController(nibName: "CViewController", bundle: nil);


        // 2) Add in each view to the container view hierarchy
        //    Add them in opposite order since the view hieracrhy is a stack
        self.addChildViewController(CVc);
        self.scrollView!.addSubview(CVc.view);
        CVc.didMoveToParentViewController(self);

        self.addChildViewController(BVc);
        self.scrollView!.addSubview(BVc.view);
        BVc.didMoveToParentViewController(self);

        self.addChildViewController(AVc);
        self.scrollView!.addSubview(AVc.view);
        AVc.didMoveToParentViewController(self);


        // 3) Set up the frames of the view controllers to align
        //    with eachother inside the container view
        var adminFrame :CGRect = BVc.view.frame;

        adminFrame.origin.x = adminFrame.width;
        AVc.view.frame = adminFrame;

        var BFrame :CGRect = AVc.view.frame;
        BFrame.origin.x = 2*BFrame.width;
        CVc.view.frame = BFrame;


        // 4) Finally set the size of the scroll view that contains the frames
        var scrollWidth: CGFloat  = 3 * self.view.frame.width
        var scrollHeight: CGFloat  = self.view.frame.size.height
        self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight)
        var frame: CGRect = self.view.frame
        frame.origin.x = frame.size.width * CGFloat(1);
        frame.origin.y = 0;
        self.scrollView!.scrollRectToVisible(frame, animated: false)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {
        self.tabBarController?.selectedIndex = 2
    }
}

这是我拥有的第一个 Viewcontroller 的 .swift 文件,其中包含我想要在屏幕加载时淡入的图像。

import UIKit

class AViewController: UIViewController {

    @IBOutlet var Background: UIImageView!

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)


        UIView.animateWithDuration(1.5, animations: {
            self.Background.alpha = 1.0
        })
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我环顾四周寻找一种方法,这就是为什么我有animatedWithDuration,但我得到的只是屏幕从手机底部向上滑动。有什么想法吗?

【问题讨论】:

  • 你想达到什么效果?滚动到滚动视图中的视图后,不会调用 viewDidAppear。当视图添加到层​​次结构时调用它。您必须手动编写代码以使用 scrollView 委托函数显示标签

标签: ios iphone xcode ipad swift


【解决方案1】:

尝试在 UIView.animateWithDuration 函数之前将 alpha 设置为 0.0

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.background.alpha = 0.0
    UIView.animateWithDuration(1.5, animations: {
        self.background.alpha = 1.0
    })
}

请注意viewDidAppear 在您滚动到滚动视图中的视图后不会被调用。当view 添加到view hierarchy 时调用它。如果您想让标签在您用手指滚动或稍后自动滚动时显示,您必须手动编写代码以使用 scrollView 委托函数显示标签。

你可以做一个交叉溶解动画而不是滚动

CATransaction.flush()

self.scrollView.contentOffset = CGPointMake(self.view.frame.width,0)
UIView.transitionWithView(self.scrollView, duration: 1.0, options:
    .TransitionCrossDissolve, animations: { () -> Void in
     }) { (finished) -> Void in

}

要么使用CATransactionFlush,要么将代码放入viewDidAppear。 删除scrollView.scrollRectToVisible 代码。

【讨论】:

  • 所以当我输入这个时,它确实会淡入,但只有在它向上滑动之后才会出现。有没有办法覆盖它?
  • 你说“它确实淡入,但只有在它向上滑动之后”。那你还想让它做什么?
  • 根本不向上滑动。只需从一个屏幕溶解到下一个屏幕。我设置了一个图案,屏幕具有相同的图案但颜色不同。所以我试图创造一种效果,让它们看起来就像颜色正在相互溶解
  • 您确定要使用滚动视图吗?
  • 主视图是我的登录屏幕,它转到滚动视图,在那里我有其他屏幕。过渡在主登录屏幕和滚动视图之间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多