【问题标题】:Error Calling AnimateWithDuration on a Subview from another ViewController从另一个 ViewController 对子视图调用 AnimateWithDuration 时出错
【发布时间】:2016-09-18 14:58:04
【问题描述】:

我在容器视图中有一个子视图,当我选择我的集合视图中的一个项目时,我正在尝试对其进行动画处理以滑入屏幕。 下面是上滑子视图的功能

func showCastControl() {
    UIView.animateWithDuration(0.5) { () -> Void in
        self.castControlViewController.frame.origin.y = 584
    }
}

这会向上滑动子视图。 我从控制didSelectItem() 中的集合视图的视图控制器调用它。我调用了包含 animate 函数的容器视图控制器并将其分配给变量gcvc。然后我在集合视图中选择一个项目时调用 animate 函数:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PhotosCollectionViewCell
    let photoToCast = self.images[indexPath.row]
    photosToCast.append(photoToCast)
    print("selected a photo")
    print(photosToCast.count)

    if photosToCast .isEmpty == false {
        gcvc.showCastControl()
    }
}

问题是应用程序崩溃,我收到一个致命错误:

unexpectedly found nil while unwrapping an optional value

此错误突出显示为在容器视图控制器中的表演控制功能的实现中发生。

当我在viewDidAppear 中隐藏子视图时,动画效果很好,但是当我从控制集合视图的另一个视图控制器调用它时,我得到了错误。 任何帮助表示赞赏。

【问题讨论】:

  • 什么是self.castControlViewController?如果是UIView,为什么要命名为...viewController?这可能与您的问题没有直接关系,但您应该避免这样做。
  • 我想我应该删除名称中的控制器。谢谢
  • 我建议在调用showCastControl() 时检查castControlViewController 是否为nil。如果castControlViewController 为nil,请尝试找出它为nil 的原因,然后可以解决问题。我的猜测:正如你所说,The animation works fine when I hide the subview in **viewDidAppear**, ,可能是当你从其他视图控制器调用动画时,castControlViewController 尚未构建,所以你在这里得到了 nil。

标签: ios swift uiview uicollectionview subview


【解决方案1】:

我还使用translatesAutoresizingMaskIntoConstraints 使用以下格式代码开发UITextfield,使用此代码

func showCastControl() {

     UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in

          self.castControlViewController.translatesAutoresizingMaskIntoConstraints = true

          self.castControlViewController.frame = CGRectMake(0, 558, self.view.frame.size.width, self.view.frame.size.height)

        }) { (Bool) -> Void in

    }
}

这种类型的代码运行良好,没有问题,希望对您有所帮助

【讨论】:

  • 感谢您的回答。我得到的问题不是动画。就像我从容器视图控制器的 viewDidAppear 调用动画时提到的那样。当我从 UICollectionViewController 调用它时,我得到了错误。
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
相关资源
最近更新 更多