【问题标题】:UIView animation - unexpectedly found nil while unwrappingUIView 动画 - 展开时意外发现 nil
【发布时间】:2016-06-08 10:19:50
【问题描述】:

这个错误我有一些问题:

打开包装时意外发现 nil

调试器向我显示此错误的位置,但我不知道出了什么问题。错误出现在 UIView 动画块功能上。我在识别问题时遇到了问题。有一个代码:

override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self,
                                                             selector: #selector(playerItemDidReachEnd),
                                                             name: AVPlayerItemDidPlayToEndTimeNotification,
                                                             object: self.player?.currentItem);
}

func playerItemDidReachEnd(notification: NSNotification) {
    self.player?.seekToTime(kCMTimeZero)
    self.player?.play()

    self.freeEffectAnimationImage.layer.removeAllAnimations()
    self.freeEffectAnimationImage.center = self.beginingPoint
    self.freeEffectAnimationImage.stopAnimating()
    self.freeEffectAnimationImage.startAnimating()

    if beginingPoint.x < 15 {
        self.beginingPoint = CGPointMake(-CGRectGetWidth(freeEffectAnimationImage.frame), beginingPoint.y)
    }

    if endingPoint.x > self.view.frame.size.width - 15 {
        self.endingPoint = CGPointMake(self.view.frame.size.width + CGRectGetWidth(freeEffectAnimationImage.frame), endingPoint.y)
    }
    UIView.animateWithDuration(self.animationDuration/2, delay:freeEffectDelay, options:UIViewAnimationOptions.Autoreverse, animations: {
        self.freeEffectAnimationImage.center = self.endingPoint
        }, completion: {
            (success:Bool) in
            if success {
                self.freeEffectAnimationImage.center = self.beginingPoint
            }
    })
}

我会很高兴得到帮助:)

【问题讨论】:

  • 你调试看看值是什么?
  • 提供的代码没有显示beginingPoint 和endingPoint 是如何初始化的,除非它们的值符合特定标准(if 块)。调用UIView.animation之前的值是多少
  • freeEffectAnimationImage、self.beginingPoint 或 self.endingPoint 是可选的吗?如果是这样,请尝试自我。 freeEffectAnimationImage?.center
  • 哪一行导致错误?
  • 你在哪里设置freeEffectAnimationImage?有根据的猜测是当您收到通知时它尚未初始化......

标签: ios swift animation uiview


【解决方案1】:

有根据的猜测是,当收到AVPlayerItemDidPlayToEndTimeNotification-通知时,您的self.freeEffectAnimationImage 尚未创建。

因此,当动画块尝试访问预期的实例时,它会发现 nil 并死掉......

【讨论】:

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