UIView动画 - 在打开时意外地发现了nil

时间:2016-06-08 10:19:51

标签: ios swift animation uiview

我遇到这个错误的问题:

  

在展开时意外地发现了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
            }
    })
}

我很乐意帮忙:)

1 个答案:

答案 0 :(得分:1)

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

因此,当动画块试图访问预期的实例时,它会找到nil而不是...