【问题标题】:Error - UIImageView UIScrollView Playing Background Video错误 - UIImageView UIScrollView 播放背景视频
【发布时间】:2015-12-10 22:09:03
【问题描述】:

我无法编译以下代码,大家有什么建议吗?

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 的向下转换?'到 'UIViewController' 只展开 选项;你的意思是用'!'

这发生在一行

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

【问题讨论】:

  • 错误是什么?它发生在哪里?
  • 错误是'从'UIViewController 向下转换?'到 'UIViewController' 只解开可选项;你的意思是用'!' ----------这发生在 ----let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
  • 用所有相关细节更新您的问题,以便人们可以帮助您。

标签: ios swift uiscrollview uiimageview


【解决方案1】:

你强制向下转换一个已经是相同类型的对象。您只需要删除as! UIViewController 并假设存在标识符为LoginScreenViewController,您可以在需要时强制解开vc 变量。

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

【讨论】:

    【解决方案2】:

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

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

    }

    【讨论】:

    • 很高兴它有帮助!如果解决方案有效,您可以将其标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多