从另一个ViewController调用子视图上的AnimateWithDuration时出错

时间:2016-05-21 10:58:20

标签: ios swift uiview uicollectionview subview

我在容器视图中有一个子视图,当我选择了我的集合视图中的项目时,我试图将其设置为动画以向上滑动到屏幕。 滑动子视图的功能在

之下
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

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

当我在viewDidAppear中隐藏子视图时,动画工作正常,但是当我从控制集合视图的其他视图控制器调用它时,我收到错误。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

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

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

    }
}

这种类型的代码完美无缺,没有问题,希望它有用

相关问题