GLKView set drawable properties

后端 未结 3 1788
梦如初夏
梦如初夏 2021-02-11 17:31

I\'m trying to port Apples GLPaint example to use GLKit. Using a UIView, its possible to return the CAEAGLLayer of the view and set the drawableProperties to include kEAGLDrawab

3条回答
  •  攒了一身酷
    2021-02-11 18:15

    Simeon's answer works but changes the behavior for all EAGL-based views in an application. I have some views which need the backing forced and others which don't, so I came up with a slightly different solution by creating subclasses of GLKView and CEAGLLayer, like this:

    @interface RetainedEAGLLayer : CAEAGLLayer
    @end
    
    @implementation RetainedEAGLLayer
    - (void)setDrawableProperties:(NSDictionary *)drawableProperties {
        // Copy the dictionary and add/modify the retained property
        NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithCapacity:drawableProperties.count + 1];
        [drawableProperties enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
            // Copy all keys except the retained backing
            if (![key isKindOfClass:[NSString class]]
            || ![(NSString *)key isEqualToString:kEAGLDrawablePropertyRetainedBacking])
                [mutableDictionary setObject:object forKey:key];
        }];
        // Add the retained backing setting
        [mutableDictionary setObject:@(YES) forKey:kEAGLDrawablePropertyRetainedBacking];
        // Continue
        [super setDrawableProperties:mutableDictionary];
        [mutableDictionary release];
    }
    @end
    

    and this

    @interface RetainedGLKView : GLKView
    @end
    
    @implementation RetainedGLKView
    + (Class)layerClass {
        return [RetainedEAGLLayer class];
    }
    @end
    

    Now I can just use RetainedGLKView instead of GLKView for those views where I want to force a retained backing.

提交回复
热议问题