/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #import "OpenVulkanoViewController.h" #pragma mark - #pragma mark OpenVulkanoViewController @implementation OpenVulkanoOrientationLockableViewController -(id)init { [super init]; _rotationLockedView = nil; return self; } -(void) viewDidLoad { [super viewDidLoad]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; if (_rotationLockedView != nil) { _rotationLockedView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)); } } -(CGFloat)getOrientation { if (_rotationLockedView == nil) return 0; return [[_rotationLockedView.layer valueForKeyPath:@"transform.rotation.z"] floatValue]; } -(void)setOrientation:(CGFloat)angle { if (_rotationLockedView != nil) { [_rotationLockedView.layer setValue:@(angle) forKeyPath:@"transform.rotation.z"]; } } -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; if (_rotationLockedView != nil) { [coordinator animateAlongsideTransition:^(id context) { CGAffineTransform deltaTransform = coordinator.targetTransform; CGFloat deltaAngle = atan2f(deltaTransform.b, deltaTransform.a); CGFloat currentRotation = [self getOrientation]; // Adding a small value to the rotation angle forces the animation to occur in a the desired direction, preventing an issue where the view would appear to rotate 2PI radians during a rotation from LandscapeRight -> LandscapeLeft. currentRotation += -1 * deltaAngle + 0.0001; [self setOrientation:currentRotation]; } completion:^(id context) { // Integralize the transform to undo the extra 0.0001 added to the rotation angle. CGAffineTransform currentTransform = _rotationLockedView.transform; currentTransform.a = round(currentTransform.a); currentTransform.b = round(currentTransform.b); currentTransform.c = round(currentTransform.c); currentTransform.d = round(currentTransform.d); _rotationLockedView.transform = currentTransform; }]; } } @end