错误 - UIImageView UIScrollView播放背景视频

时间:2015-12-10 22:09:03

标签: ios swift uiscrollview uiimageview

我无法获得以下代码进行编译,有人建议吗?

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var avatar: UIImageView!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var contentImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        avatar.layer.cornerRadius = 5.0
        avatar.layer.borderWidth = 4.0
        avatar.layer.borderColor = UIColor.whiteColor().CGColor
        avatar.clipsToBounds = true
        scrollView.delegate = self
        contentImageView.clipsToBounds = true
    }

    override func viewDidAppear(animated: Bool) {
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
        self.presentViewController(vc, animated: true, completion: nil)
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        let yOffset = self.scrollView.contentOffset.y * 0.2
        let availableOffset = min(yOffset, 60)
        let contentRectYOffset = availableOffset / contentImageView.frame.size.height
        contentImageView.layer.contentsRect = CGRectMake(0.0, contentRectYOffset, 1, 1);
    }
}

错误

  

'来自'UIViewController的'Downcast?' 'UIViewController'只能解开   自选项目;你的意思是使用'!'

这发生在

行中
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController

2 个答案:

答案 0 :(得分:0)

你强行抛弃一个已经是同一类型的对象。您只需删除as! UIViewController并假设标识为ViewController的{​​{1}}存在,您可以在需要时强制解包LoginScreen变量。

vc

答案 1 :(得分:0)

感谢R P,您的解决方案很有效。

override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen")
self.presentViewController(vc!, animated: true, completion: nil)

}