Shark Deng

Interactive Designer @Shanju Design in Shanghai

touched caffe / mxnet / python / wordpress / bootstrap / Arduino / Raspberry Pi / webgl / three.js / PIXI / 3dMax

Email

Github

Swift – glkView在UIVC和GLKVC中嵌套细节

2019-11-12 23:44:34

静态glkView(只渲染一次)在UIVC中嵌套:

class SignatureViewController: UIViewController {
    
  
    override func viewDidLoad() {

        //static glkview
        class Test: GLKView {
            override init(frame: CGRect) {
                super.init(frame: frame)
                self.context = EAGLContext(api: .openGLES2)!
                EAGLContext.setCurrent(self.context)
            }
            
            required init?(coder aDecoder: NSCoder) {
                fatalError("init(coder:) has not been implemented")
            }
            
            //init will trigger once
            override func draw(_ rect: CGRect) {
                glClearColor(1.0, 0.0, 0.0, 1.0)
                glClear(GLbitfield(GL_COLOR_BUFFER_BIT) | GLbitfield(GL_DEPTH_BUFFER_BIT))
                print("hello")
            }
        }
        let a = Test(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        self.view.addSubview(a)
   
    }
    
 
}

静态glkView在GLKVC中,完全一样,除了UIVC换成GLKVC

class SignatureViewController: GLKViewController {
    
    override func viewDidLoad() {

        //static glkview
        class Test: GLKView {
            override init(frame: CGRect) {
                super.init(frame: frame)
                self.context = EAGLContext(api: .openGLES2)!
                EAGLContext.setCurrent(self.context)
            }
            
            required init?(coder aDecoder: NSCoder) {
                fatalError("init(coder:) has not been implemented")
            }
            
            //init will trigger once
            override func draw(_ rect: CGRect) {
                glClearColor(1.0, 0.0, 0.0, 1.0)
                glClear(GLbitfield(GL_COLOR_BUFFER_BIT) | GLbitfield(GL_DEPTH_BUFFER_BIT))
                print("hello")
            }
        }
        
        let a = Test(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        self.view.addSubview(a)
        
    }
    
    //gameView.context to activate this func
    override func glkView(_ view: GLKView, drawIn rect: CGRect) {
        
    }
}

 

 



——By SharkDeng
如果你喜欢我的文章,欢迎红包赞赏

Leave a Reply

Your email address will not be published. Required fields are marked *