【问题标题】:iOS - Swift : Expand user profile picture in UITableViewiOS - Swift:在 UITableView 中展开用户个人资料图片
【发布时间】:2018-09-12 15:55:48
【问题描述】:

我正在尝试从UITableViewCell 获取动画(保存有关用户的信息,带有个人资料图片),方法是从此扩展用户的个人资料图片:

//////////////////
到这个
//////////////

在此输入代码

它可以工作,但问题是动画不知何故从屏幕的一角开始,而不是从UITableViewCell 中的圆形图像开始。

func handlePictureTap(conversationTableCell: ConversationTableCell) {
    guard let cellIndexPath = self.conversationsTableView.indexPath(for: conversationTableCell) else { return }

    transitionDelegate.openingFrame = self.frameForCellAtIndexPath(cellIndexPath)

    let userProfileViewController = UserViewController(nibName: "UserViewController", bundle: nil)
    userProfileViewController.recentProfileProtocol = self
    userProfileViewController.transitioningDelegate = transitionDelegate
    userProfileViewController.modalPresentationStyle = .overCurrentContext
    userProfileViewController.userProfileName = conversationTableCell.conversationName.text!
    userProfileViewController.userProfileImage = conversationTableCell.conversationPicture.image

    self.present(navEditorViewController, animated: true, completion: nil)
} 

还有 frameForCellAtIndexPath 的函数。

func frameForCellAtIndexPath(_ indexPath: IndexPath) -> CGRect {
    let cell = self.conversationsTableView.cellForRow(at: indexPath) as! ConversationTableCell

    let profileImageFrame = cell.conversationPicture.frame

    return profileImageFrame
}

我无法为其获取与整个屏幕相关的正确帧,以便我的动画从单元格的个人资料图片开始。

欢迎提出建议

【问题讨论】:

  • 感谢 Nitish 的编辑。由于分数低,无法直接添加图片:)

标签: ios swift uitableview popup expand


【解决方案1】:

您从图像的框架开始动画,该框架相对于放置在左上角的表格视图单元格坐标系。

要确定单元格相对于表格视图的当前位置,您可以使用以下内容:

let rect = tableView.rectForRow(at: indexPath)

如果你需要相对于另一个视图的矩形,你可以使用 UIViews

convert(_ rect: NSRect, from view: NSView?) -> NSRect

更新

要将其应用于您的特定情况,假设您需要屏幕坐标中的 openingRect,您可以执行以下操作:

let cellOrigin = tableView.rectForRow(at: indexPath).origin

let rectRelativeToTable = CGRect(x: imageFrame.origin.x + cellOrigin.x,
                                 y: imageFrame.origin.y + cellOrigin.y,
                                 width: imageFrame.size.width,
                                 height: imageFrame.size.height)

let openingRect = tableView.convert(rectRelativeToTable, to: nil)

【讨论】:

  • 但是用你的方法,动画是从单元格的中心开始的,这不是我想要的。
  • 救命稻草。只是这样做了,忘记了cellOrigin。谢谢你,先生:)
【解决方案2】:

以防万一其他人正在寻找相同的动画,这里是过渡类:
DismissalAnimator

import UIKit

class DismissalAnimator: NSObject, UIViewControllerAnimatedTransitioning {
var openingFrame: CGRect?


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


func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
    let containerView = transitionContext.containerView

    let animationDuration = self .transitionDuration(using: transitionContext)

    let snapshotView = fromViewController.view.snapshotView(afterScreenUpdates: true)
    containerView.addSubview(snapshotView!)

    fromViewController.view.alpha = 0.0

    UIView.animate(
        withDuration: animationDuration,
        animations: { () -> Void in
            snapshotView!.frame = self.openingFrame!
            snapshotView!.alpha = 0.0
    },
        completion: { (finished) -> Void in
            snapshotView!.removeFromSuperview()

            fromViewController.view.removeFromSuperview()

            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    })
  }  

} 

PresentationAnimator

import UIKit

class PresentationAnimator: NSObject, UIViewControllerAnimatedTransitioning {
var openingFrame: CGRect?


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


func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
    let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
    let containerView = transitionContext.containerView

    let animationDuration = self.transitionDuration(using: transitionContext)

    containerView.addSubview(toViewController.view)

    let snapshotView = toViewController.view.snapshotView(afterScreenUpdates: true)
    snapshotView!.frame = openingFrame!

    toViewController.view.alpha = 0.0
    snapshotView!.alpha = 0

    containerView.addSubview(snapshotView!)

    UIView.animate(
        withDuration: animationDuration,
        delay: 0.0,
        usingSpringWithDamping: 2.0,
        initialSpringVelocity: 5.0,
        options: UIViewAnimationOptions.curveEaseOut,
        animations: { () -> Void in
            snapshotView!.frame = fromViewController.view.frame
            snapshotView!.alpha = 1
    },
        completion: { (finished) -> Void in
            snapshotView!.removeFromSuperview()

            toViewController.view.alpha = 1.0

            transitionContext.completeTransition(finished)
    })
  }
}  

TransitioningDelegate

import UIKit

class TransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
var openingFrame: CGRect?


func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let presentationAnimator = PresentationAnimator()
    presentationAnimator.openingFrame = openingFrame

    return presentationAnimator
}


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let dismissAnimator = DismissalAnimator()
    dismissAnimator.openingFrame = openingFrame!

    return dismissAnimator
   }
}
  • @Stephan Schlecht 提供的上述代码将为您提供相同的动画。

【讨论】:

    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多