加载uiview时出现黑屏

时间:2017-12-21 10:44:13

标签: ios swift uiviewcontroller avplayer

我遇到的问题是当我加载它时 UIViewController.removeSpinner(spinner:self.view)

它给我一个空白的黑色视图,我不知道如何从按钮功能访问sv变量:

让sv = UIViewController.displaySpinner(onView:self.view)

即时使用此灭绝来加载微调器并移除 http://brainwashinc.com/2017/07/21/loading-activity-indicator-ios-swift/

我真正想要做的事情:

1-获取语音留言的网址。

2-运行微调器以加载播放器。

3-玩。

4-如果观察者发现它已经结束,则删除微调器。

这是我的代码:

 @IBAction func vnClick(_ sender: UIButton) {
    let vnInfo = itemss[sender.tag]
    print(vnInfo.vnUrl)
    let sv = UIViewController.displaySpinner(onView: self.view)
    if let url = NSURL(string: vnInfo.vnUrl) {
        player = AVPlayer(url: url as URL)
        player.play()
        //check if ended playing to remove spinner
        NotificationCenter.default.addObserver(self,  selector:#selector(playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)


    }
}
@objc func playerDidFinishPlaying(note: NSNotification) {
    UIViewController.removeSpinner(spinner: self.view)
}

2 个答案:

答案 0 :(得分:1)

您应该做更像

的事情
private var sv : UIView?
 @IBAction func vnClick(_ sender: UIButton) {
    let vnInfo = itemss[sender.tag]
    print(vnInfo.vnUrl)
    sv = UIViewController.displaySpinner(onView: self.view)
    if let url = NSURL(string: vnInfo.vnUrl) {
        player = AVPlayer(url: url as URL)
        player.play()
        //check if ended playing to remove spinner
        NotificationCenter.default.addObserver(self,  selector:#selector(playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
    }
}

@objc func playerDidFinishPlaying(note: NSNotification) {
    if let sv = sv {
        UIViewController.removeSpinner(spinner: sv) 
    }
}

答案 1 :(得分:1)

你的错误是你在这里传递self.view,这将删除视图

playerDidFinishPlaying()中的

UIViewController.removeSpinner(微调器: self.view

你需要这样做,把spinnerview对象放在按钮点击之外,

var spinnerView: UIView!

然后分配,

spinnerView = UIViewController.displaySpinner(onView: self.view)

在删除时,传递spinnerview而不是self.view

UIViewController.removeSpinner(spinner: self.spinnerView)

代码就像,

@IBAction func vnClick(_ sender: UIButton) {
let vnInfo = itemss[sender.tag]
print(vnInfo.vnUrl)
spinnerView = UIViewController.displaySpinner(onView: self.view)
if let url = NSURL(string: vnInfo.vnUrl) {
    player = AVPlayer(url: url as URL)
    player.play()
    //check if ended playing to remove spinner
    NotificationCenter.default.addObserver(self,  selector:#selector(playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
}
}

@objc func playerDidFinishPlaying(note: NSNotification) {
    UIViewController.removeSpinner(spinner: self.spinnerView)
}