在 swift 中向 presentViewController 添加自定义转换

adding a custom transition to presentViewController in swift

我有一个 viewController,它有一个 TableView 和一个 accessoryTypedetailbutton 的单元格。 当按下 accessoryType 时,我会显示一个带有文本的半透明视图。

我希望此视图以特定方式显示,因此我创建了一个自定义视图控制器。

然而,尝试 运行 detailbutton 中的 viewController 是行不通的。

我的部分代码:

//Defined in the class
let customTransitionManager = TransitionManager()

//in tableView cellForRowAtIndexPath
if (tableView == continent){
        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: "continentSelector")
        cell.textLabel?.text = "Continent"
        cell.accessoryType = UITableViewCellAccessoryType.DetailButton;
        return cell


func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
    println(indexPath)
    let detailController = storyboard?.instantiateViewControllerWithIdentifier("detailButtonStoryBoard") as? detailedView
    detailController?.modalPresentationStyle = .Custom
    detailController?.transitioningDelegate = customTransitionManager;
    presentViewController(detailController!, animated: true, completion: nil)
}

运行 此代码给我以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

以下突出显示:

-> 0x100209cf0 <function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()+44>: brk    #0x1

我看到有两个地方可以使用 nil 值。第一个是在索引路径处的行的单元格中,但这不太可能。第二个也是更有可能的一个是您在呈现 detailContoller 时尝试解包的地方。您是否确认您正在从情节提要中取回控制器?

更新:我在评论中提出的解决方案格式很糟糕,这里有一个更好的格式

找到解决方案使用 segue 而不是手动呈现控制器。

  1. 从 didSelectRowAtIndexPath 调用 performSegue
  2. 在 prepareForSegue 方法中输入:

    let toViewController = segue.destinationViewController as UIViewController
    toViewController.transitioningDelegate = self.customTransitionManager`
    

本教程的全部功劳:http://mathewsanders.com/animated-transitions-in-swift/#custom-transition-animations

更新 2:下载了您的项目并开始运行。以下是我所做的更改:

  1. 我在牛仔裤控制器和细节控制器之间添加了一个模态转场。我称之为 'DetailSegue'
  2. 我更新了 jeans.swift 文件 accessoryButtonTappedForRowWithIndexPath 方法如下:

    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
        println(indexPath)
        self.performSegueWithIdentifier("DetailSegue", sender: self)
    }
    
  3. 然后我更新了 TransitionManger animateTransition 函数如下,基本上使往返视图可选并适当处理 nil

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        //get reference to our FROM View, To View, and container view that we should perform the transition in
        let container = transitionContext.containerView()
    
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
    
        //setup from 2D transform that we'll use in the animation
        let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
        let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)
    
        //start the view to the right of the screen 
        toView?.transform = offScreenRight
    
        //add both the views to our view controller
        if toView != nil {
            container.addSubview(toView!)
        }
    
        if fromView != nil {
            container.addSubview(fromView!)
        }
    
        // get the duration of the animation
        let duration = self.transitionDuration(transitionContext)
    
        //perform the animation
    
        UIView.animateWithDuration(duration, delay: 0.0, options: nil, animations: {
            fromView?.transform = offScreenLeft
            toView?.transform = CGAffineTransformIdentity
            }, completion: { finished in transitionContext.completeTransition(true)})
    }
    

这是最终结果的截屏视频:http://screencast.com/t/2mC07BLCC

我希望这能帮助你推进你的项目。