gpt4 book ai didi

ios - UIView、Swift 上的深度页面转换

转载 作者:IT王子 更新时间:2023-10-29 05:17:22 25 4
gpt4 key购买 nike

如何创建像这样的深度页面变换动画(从上到下,从下到上)https://youtu.be/c2ccXwwmcnA .我在 Google 中搜索过,但我不知道如何在 iOS 中实现。

示例输出如下:

in depth effect

最佳答案

我遵循了教程 Animated Transitions in Swift我得到了这个效果:

in depth effect

如果你不想阅读整个教程,基本上你必须这样做:

1。创建 TrasitionManager

//
// TransitionManager.swift
// Aleph Retamal
//
// Created by Aleph Retamal on 4/19/15.
// Copyright (c) 2015 Aleph Retamal. All rights reserved.
//

import UIKit

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

private var presenting:Bool = true
var leftSide:Bool = true

// MARK: UIViewControllerAnimatedTransitioning protocol methods

// animate a change from one viewcontroller to another
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

// get reference to our fromView, toView and the container view that we should perform the transition in
let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!


let offScreenRight = CGAffineTransformConcat(CGAffineTransformMakeTranslation(container!.frame.width, 0), CGAffineTransformMakeScale(0.1, 0.1))
let offScreenLeft = CGAffineTransformMakeTranslation(-container!.frame.width, 0)

// start the toView to the right of the screen
if !leftSide {
if self.presenting {
toView.transform = offScreenRight
} else {
toView.transform = offScreenLeft
}
} else {
if self.presenting {
toView.transform = offScreenLeft
} else {
toView.transform = offScreenRight
}
}

// add the both views to our view controller
container!.addSubview(toView)
container!.addSubview(fromView)

// get the duration of the animation
// DON'T just type '0.5s' -- the reason why won't make sense until the next post
// but for now it's important to just follow this approach
let duration = self.transitionDuration(transitionContext)

// perform the animation!
// for this example, just slid both fromView and toView to the left at the same time
// meaning fromView is pushed off the screen and toView slides into view
// we also use the block animation usingSpringWithDamping for a little bounce
UIView.animateWithDuration(duration, animations: { () -> Void in
if !self.leftSide {
if self.presenting {
fromView.transform = offScreenLeft
} else {
container!.sendSubviewToBack(fromView)
fromView.transform = offScreenRight
}
} else {
if self.presenting {
fromView.transform = offScreenRight
container!.sendSubviewToBack(fromView)
} else {
fromView.transform = offScreenLeft
}
}

toView.transform = CGAffineTransformIdentity
}) { (finished) -> Void in
// tell our transitionContext object that we've finished animating
transitionContext.completeTransition(true)
}

}

// return how many seconds the transiton animation will take
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 1.0
}

// MARK: UIViewControllerTransitioningDelegate protocol methods

// return the animataor when presenting a viewcontroller
// remmeber that an animator (or animation controller) is any object that aheres to the UIViewControllerAnimatedTransitioning protocol
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self
}

// return the animator used when dismissing from a viewcontroller
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = false
return self
}

}

这是您设置 View 应如何转换的地方

let offScreenRight = CGAffineTransformConcat(CGAffineTransformMakeTranslation(container!.frame.width, 0), CGAffineTransformMakeScale(0.1, 0.1))
let offScreenLeft = CGAffineTransformMakeTranslation(-container!.frame.width, 0)

在这种情况下,一个 View 会翻译 + 缩放,而另一个 View 只会翻译

2。在你的 ViewController

中实例化 TransitionManager
let transitionManager = TransitionManager()

3。设置转换委托(delegate)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
transitionManager.leftSide = false
let toViewController = segue.destinationViewController
toViewController.transitioningDelegate = self.transitionManager
}

如果您希望 View 来自右侧,leftSide = false

transitionManager.leftSide = false

就是这样!

编辑:

实现这个效果:

in depth effect 2

这是项目的 git:https://github.com/alaphao/indepth

使用约束来对齐和调整大小:

  1. 创建 2 个与其父 View 具有相同宽度/高度的 View

  2. 将两者与CenterX

    对齐
  3. 设置与底部的距离 = 0 并为约束创建导出

  4. 创建一个PanGestureRecognizer

  5. 根据平移 translationInView.y

  6. 为前 View 底部约束设置动画

关于ios - UIView、Swift 上的深度页面转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33798300/

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