gpt4 book ai didi

ios - 如何在 iOS 中使用自定义转换解决此问题?

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:26 25 4
gpt4 key购买 nike

我构建了一个类来实现 View Controller 之间的循环转换。当我点击按钮导航到另一个 View Controller 时,一个圆圈从按钮开始增长,直到它用新 Controller 填满屏幕。当我关闭 View Controller 时,我希望这个圆圈缩小回到原来的位置。它也在工作。唯一的问题是,当关闭正在进行时,圆圈正在缩小,屏幕背面完全是黑色的,并且在动画完成后,新的 viewController 突然出现。

下面是一些效果图:

enter image description here

这是自定义类的代码:

class customTransition: NSObject, UIViewControllerAnimatedTransitioning{

var duration: TimeInterval = 0.5
var startPoint = CGPoint.zero

var circle = UIView()

var circleColor = UIColor.white
enum transitMode: Int {
case presenting, dismissing
}

var transitionMode: transitMode = .presenting

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

let container = transitionContext.containerView
guard let to = transitionContext.view(forKey: UITransitionContextViewKey.to) else {return}
guard let from = transitionContext.view(forKey: UITransitionContextViewKey.from) else {return}

circleColor = to.backgroundColor ?? UIColor.white

if transitionMode == .presenting {
to.translatesAutoresizingMaskIntoConstraints = false
to.center = startPoint

circle = UIView()
circle.backgroundColor = circleColor
circle.frame = getFrameForCircle(rect: to.frame)
circle.layer.cornerRadius = circle.frame.width / 2
circle.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
circle.alpha = 0


circle.addSubview(to)

to.centerXAnchor.constraint(equalTo: circle.centerXAnchor).isActive = true
to.centerYAnchor.constraint(equalTo: circle.centerYAnchor).isActive = true
to.widthAnchor.constraint(equalToConstant: to.frame.width).isActive = true
to.heightAnchor.constraint(equalToConstant: to.frame.height).isActive = true

container.addSubview(circle)

UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveLinear, animations: {
self.circle.center = from.center
self.circle.transform = CGAffineTransform.identity
self.circle.alpha = 1

}) { (sucess) in

transitionContext.completeTransition(sucess)

}

} else if transitionMode == .dismissing {


UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveLinear, animations: {
self.circle.center = self.startPoint
self.circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
self.circle.alpha = 0

}) { (sucess) in
transitionContext.completeTransition(sucess)
}
}
}




func getFrameForCircle(rect: CGRect) -> CGRect{
let width = Float(rect.width)
let height = Float(rect.height)

let diameter = CGFloat(sqrtf(width * width + height * height))

let x: CGFloat = rect.midX - (diameter / 2)
let y: CGFloat = rect.midY - (diameter / 2)

return CGRect(x: x, y: y, width: diameter, height: diameter)
}



}

和实现...

    let circularTransition = customTransition()

当前 View Controller 的调用...我试图设置 secondVC.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext 但是当我设置这一行时它完全忽略了动画转换我不知道为什么。 ..`

 @objc func handlePresent(sender: UIButton){
let secondVC = nextVC()
secondVC.transitioningDelegate = self
present(secondVC, animated: true, completion: nil)

}

委托(delegate)方法:

  func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
circularTransition.startPoint = presentButton.center
circularTransition.transitionMode = .presenting
return circularTransition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
circularTransition.transitionMode = .dismissing
circularTransition.startPoint = presentButton.center

return circularTransition
}

我在这里错过了什么?有什么建议么?不使用 Storyboard,仅使用代码。

最佳答案

如果不使用navigationController,需要在presentedviewController中使用.custom模式。

 import UIKit

class TransViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}
let circularTransition = customTransition()

@IBOutlet var presentButton : UIButton!

@IBAction func handlePresent(sender: UIButton){

if let secondVC = storyboard?.instantiateViewController(withIdentifier: "next"){
secondVC.modalPresentationStyle = .custom
secondVC.transitioningDelegate = self
present(secondVC, animated: true, completion: nil)
}
}

}


class BackViewController: UIViewController {

@IBAction func dismissMe(sender: UIButton){
self.dismiss(animated: true, completion: nil)
}

}

extension TransViewController: UIViewControllerTransitioningDelegate {

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

circularTransition.startPoint = presentButton.center
circularTransition.transitionMode = .presenting
return circularTransition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
circularTransition.transitionMode = .dismissing
circularTransition.startPoint = presentButton.center

return circularTransition
}

}

如果没有 from 或 to view,我们就使用 from 和 to view from containView。

   func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

let container = transitionContext.containerView
var to : UIView!
var from : UIView!
to = transitionContext.view(forKey: UITransitionContextViewKey.to)
if to == nil {to = container}
from = transitionContext.view(forKey: UITransitionContextViewKey.from)
if from == nil {from = container}

其余相同:

 circleColor = to.backgroundColor ?? UIColor.white

if transitionMode == .presenting {
to.translatesAutoresizingMaskIntoConstraints = false
to.center = startPoint

circle = UIView()
circle.backgroundColor = circleColor
circle.frame = getFrameForCircle(rect: to.frame)
circle.layer.cornerRadius = circle.frame.width / 2
circle.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
circle.alpha = 0


circle.addSubview(to)

to.centerXAnchor.constraint(equalTo: circle.centerXAnchor).isActive = true
to.centerYAnchor.constraint(equalTo: circle.centerYAnchor).isActive = true
to.widthAnchor.constraint(equalToConstant: to.frame.width).isActive = true
to.heightAnchor.constraint(equalToConstant: to.frame.height).isActive = true

container.addSubview(circle)

UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveLinear, animations: {
self.circle.center = from.center
self.circle.transform = CGAffineTransform.identity
self.circle.alpha = 1

}) { (sucess) in

transitionContext.completeTransition(sucess)

}

} else if transitionMode == .dismissing {


UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveLinear, animations: {
self.circle.center = self.startPoint
self.circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
self.circle.alpha = 0

}) { (sucess) in
transitionContext.completeTransition(sucess)
}
}
}

关于ios - 如何在 iOS 中使用自定义转换解决此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53355915/

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