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

时间:2015-03-01 20:38:58

标签: ios iphone swift uiviewanimationtransition

我有一个viewController,其TableViewaccessoryType detailbutton的{​​{1}}。 按下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

1 个答案:

答案 0 :(得分:3)

我看到两个你可以有零值的地方。第一个是在索引路径的行的单元格中,但这不太可能。第二个也是更有可能的是你在展示detailContoller时尝试打开它的地方。您是否确认从故事板中取回控制器?

更新:我在评论中放置的解决方案格式可怕,这是一个更好的格式

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

  1. 从didSelectRowAtIndexPath
  2. 调用performSegue 在prepareForSegue方法中
  3. 把这个:

    let toViewController = segue.destinationViewController as UIViewController
    toViewController.transitioningDelegate = self.customTransitionManager`
    
  4. 完全归功于本教程:http://mathewsanders.com/animated-transitions-in-swift/#custom-transition-animations

    更新2:下载了您的项目并使其正常运行。以下是我所做的更改:

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

      func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath){
          println(indexPath)
          self.performSegueWithIdentifier("DetailSegue", sender: self)
      }
      
    3. 然后我更新了TransitionManger animateTransition函数,如下所示,基本上使to和from视图可选,并适当处理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)})
      }
      
    4. 以下是最终结果的截屏视频:http://screencast.com/t/2mC07BLCC

      我希望这可以帮助您推进项目。