|
| 1 | +/* |
| 2 | +See LICENSE folder for this sample’s licensing information. |
| 3 | +*/ |
| 4 | + |
| 5 | +#import "MainUIView.h" |
| 6 | + |
| 7 | +@implementation MainUIView |
| 8 | +{ |
| 9 | + CADisplayLink *_displayLink; |
| 10 | +} |
| 11 | + |
| 12 | ++ (Class) layerClass |
| 13 | +{ |
| 14 | + return [CAMetalLayer class]; |
| 15 | +} |
| 16 | + |
| 17 | +- (void)didMoveToWindow |
| 18 | +{ |
| 19 | + [super didMoveToWindow]; |
| 20 | + |
| 21 | + if(self.window == nil) |
| 22 | + { |
| 23 | + // If moving off of a window destroy the display link. |
| 24 | + [_displayLink invalidate]; |
| 25 | + _displayLink = nil; |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + [self setupCADisplayLinkForScreen:self.window.screen]; |
| 30 | + |
| 31 | + // CADisplayLink callbacks are associated with an 'NSRunLoop'. The currentRunLoop is the |
| 32 | + // the main run loop (since 'didMoveToWindow' is always executed from the main thread. |
| 33 | + [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; |
| 34 | + |
| 35 | + // Perform any actions which need to know the size and scale of the drawable. When UIKit calls |
| 36 | + // didMoveToWindow after the view initialization, this is the first opportunity to notify |
| 37 | + // components of the drawable's size |
| 38 | + [self resizeDrawable:self.window.screen.nativeScale]; |
| 39 | + |
| 40 | + self.layer.opaque = YES; |
| 41 | + self.opaque = YES; |
| 42 | + self.window.opaque = YES; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +- (void)setPaused:(BOOL)paused |
| 48 | +{ |
| 49 | + super.paused = paused; |
| 50 | + |
| 51 | + _displayLink.paused = paused; |
| 52 | +} |
| 53 | + |
| 54 | +- (void)setupCADisplayLinkForScreen:(UIScreen*)screen |
| 55 | +{ |
| 56 | + [self stopRenderLoop]; |
| 57 | + |
| 58 | + _displayLink = [screen displayLinkWithTarget:self selector:@selector(render)]; |
| 59 | + |
| 60 | + _displayLink.paused = self.paused; |
| 61 | + |
| 62 | + _displayLink.preferredFramesPerSecond = 60; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)didEnterBackground:(NSNotification*)notification |
| 66 | +{ |
| 67 | + self.paused = YES; |
| 68 | +} |
| 69 | + |
| 70 | +- (void)willEnterForeground:(NSNotification*)notification |
| 71 | +{ |
| 72 | + self.paused = NO; |
| 73 | +} |
| 74 | + |
| 75 | +- (void)stopRenderLoop |
| 76 | +{ |
| 77 | + [_displayLink invalidate]; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +// Override all methods which indicate the view's size has changed |
| 83 | + |
| 84 | +- (void)setContentScaleFactor:(CGFloat)contentScaleFactor |
| 85 | +{ |
| 86 | + [super setContentScaleFactor:contentScaleFactor]; |
| 87 | + [self resizeDrawable:self.window.screen.nativeScale]; |
| 88 | +} |
| 89 | + |
| 90 | +- (void)layoutSubviews |
| 91 | +{ |
| 92 | + [super layoutSubviews]; |
| 93 | + [self resizeDrawable:self.window.screen.nativeScale]; |
| 94 | +} |
| 95 | + |
| 96 | +- (void)setFrame:(CGRect)frame |
| 97 | +{ |
| 98 | + [super setFrame:frame]; |
| 99 | + [self resizeDrawable:self.window.screen.nativeScale]; |
| 100 | +} |
| 101 | + |
| 102 | +- (void)setBounds:(CGRect)bounds |
| 103 | +{ |
| 104 | + [super setBounds:bounds]; |
| 105 | + [self resizeDrawable:self.window.screen.nativeScale]; |
| 106 | +} |
| 107 | + |
| 108 | +@end |
0 commit comments