gpt4 book ai didi

ios - 从框架呈现 UIViewController

转载 作者:行者123 更新时间:2023-11-29 00:18:49 25 4
gpt4 key购买 nike

我按此顺序显示图像 slider 。

UIViewController > UITableview > UITableviewCell > UICollectionview > UICollectionViewCell > UIImage

用户可以滑动 UICollectionview 和查看图像。问题是我需要做动画。当用户点击我的 UICollectionviewCell 时,它应该从该单元格开始动画并显示全屏,就像在这个库中一样。

https://github.com/suzuki-0000/SKPhotoBrowser

问题是我需要使用 MWPhotoBrowser 而我不能那样显示。

我也在考虑使用英雄动画库。

https://github.com/lkzhao/Hero

但我的观点层次结构和他们的例子是不同的。我该怎么办?

最佳答案

你必须使用自定义转换

引用:

https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/CustomizingtheTransitionAnimations.html

代码工作

关于图片选择

加入VC_A

var selectedImage: UIImageView?
let transition = PopAnimator()

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

coordinator.animate(
alongsideTransition: {context in
self.bgImage.alpha = (size.width>size.height) ? 0.25 : 0.55
self.positionListItems()
},
completion: nil
)
}
//position all images inside the list
func positionListItems() {
let listHeight = listView.frame.height
let itemHeight: CGFloat = listHeight * 1.33
let aspectRatio = UIScreen.main.bounds.height / UIScreen.main.bounds.width
let itemWidth: CGFloat = itemHeight / aspectRatio

let horizontalPadding: CGFloat = 10.0

for i in herbs.indices {
let imageView = listView.viewWithTag(i) as! UIImageView
imageView.frame = CGRect(
x: CGFloat(i) * itemWidth + CGFloat(i+1) * horizontalPadding, y: 0.0,
width: itemWidth, height: itemHeight)
}

listView.contentSize = CGSize(
width: CGFloat(herbs.count) * (itemWidth + horizontalPadding) + horizontalPadding,
height: 0)
}

// On image selection
VC_B.transitioningDelegate = self
present(VC_B, animated: true, completion: nil)



// add extension
extension VC_A: UIViewControllerTransitioningDelegate {

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.originFrame = selectedImage!.superview!.convert(selectedImage!.frame, to: nil)

transition.presenting = true
selectedImage!.isHidden = true

return transition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.presenting = false
return transition
}
}

和动画类

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {

let duration = 1.0
var presenting = true
var originFrame = CGRect.zero

var dismissCompletion: (()->Void)?

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

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView

let toView = transitionContext.view(forKey: .to)!

let herbView = presenting ? toView : transitionContext.view(forKey: .from)!

let initialFrame = presenting ? originFrame : herbView.frame
let finalFrame = presenting ? herbView.frame : originFrame

let xScaleFactor = presenting ?

initialFrame.width / finalFrame.width :
finalFrame.width / initialFrame.width

let yScaleFactor = presenting ?

initialFrame.height / finalFrame.height :
finalFrame.height / initialFrame.height

let scaleTransform = CGAffineTransform(scaleX: xScaleFactor, y: yScaleFactor)

if presenting {
herbView.transform = scaleTransform
herbView.center = CGPoint(
x: initialFrame.midX,
y: initialFrame.midY)
herbView.clipsToBounds = true
}

containerView.addSubview(toView)
containerView.bringSubview(toFront: herbView)

UIView.animate(withDuration: duration, delay:0.0, usingSpringWithDamping: 0.4,
initialSpringVelocity: 0.0,
animations: {
herbView.transform = self.presenting ?
CGAffineTransform.identity : scaleTransform
herbView.center = CGPoint(x: finalFrame.midX,
y: finalFrame.midY)
},
completion:{_ in
if !self.presenting {
self.dismissCompletion?()
}
transitionContext.completeTransition(true)
}
)
}

}

关于ios - 从框架呈现 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564111/

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