Snippets
Created by
James Bush
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | //
// Created by Nicolas Manzini on 23.01.14.
// Free under MIT licence
// Copyright (c) 2014 Nicolas Manzini.
// Thanks to Daij-Djan for his help
//
// Modified by James Alan Bush on 01 • 18 • 2017
//
#import <UIKit/UIKit.h>
@interface UIView (Gravity)
@property (nonatomic) BOOL followGravity;
@end
//
// Created by Nicolas Manzini on 23.01.14.
// Free under MIT licence
// Copyright (c) 2014 Nicolas Manzini.
// Thanks to Daij-Djan for his help
//
// Modified by James Alan Bush on 01 • 18 • 2017
//
#import "UIView+Gravity.h"
#import <objc/runtime.h>
#import <CoreMotion/CoreMotion.h>
#define ROTATE_90 M_PI_2
@interface GravityManager : CMMotionManager
@property (strong, nonatomic) NSMutableArray * animatedViews;
@property (assign, nonatomic) CGFloat angle;
+ (instancetype)sharedManager;
- (void)addView:(UIView *)view;
- (void)removeView:(UIView *)view;
@end
static char MyCustomPropertyKey = 0;
@implementation UIView (Gravity)
@dynamic followGravity;
+ (void)load
{
SEL originalSelector = NSSelectorFromString(@"dealloc");
SEL overrideSelector = @selector(xchg_dealloc);
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method overrideMethod = class_getInstanceMethod(self, overrideSelector);
if (class_addMethod(self, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod)))
{
class_replaceMethod(self, overrideSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}
else
{
method_exchangeImplementations(originalMethod, overrideMethod);
}
}
- (void)xchg_dealloc
{
if (self.followGravity)
{
[[GravityManager sharedManager] removeView:self];
}
objc_setAssociatedObject(self, &MyCustomPropertyKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)setFollowGravity:(BOOL)followGravity
{
objc_setAssociatedObject(self, &MyCustomPropertyKey, @(followGravity), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (followGravity)
{
[[GravityManager sharedManager] addView:self];
}
else
{
[[GravityManager sharedManager] removeView:self];
}
}
- (BOOL)followGravity
{
NSNumber * followGravity = objc_getAssociatedObject(self, &MyCustomPropertyKey);
// for nil case
return followGravity ? followGravity.boolValue : NO;
}
@end
@implementation GravityManager
+ (instancetype)sharedManager
{
static dispatch_once_t pred;
static GravityManager * sharedManager = nil;
dispatch_once(&pred, ^
{
sharedManager = [[self alloc] init];
sharedManager.animatedViews = @[].mutableCopy;
sharedManager.deviceMotionUpdateInterval = 0.1;
__weak typeof(sharedManager) weakManager = sharedManager;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[sharedManager startDeviceMotionUpdatesToQueue:queue
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
double angle = atan2(motion.gravity.x, motion.gravity.y);
angle -= ROTATE_90;
if (angle != sharedManager.angle)
{
sharedManager.angle = angle;
CGAffineTransform transform = (angle == 0) ? CGAffineTransformIdentity : CGAffineTransformMakeRotation(angle);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[UIView animateWithDuration:.25
animations:^{
[weakManager.animatedViews setValue:[NSValue valueWithCGAffineTransform:transform] forKey:@"transform"];
}];
}];
}
}];
});
return sharedManager;
}
- (void)addView:(UIView *)view
{
if([self.animatedViews indexOfObjectIdenticalTo:view] == NSNotFound)
{
view.transform =
(self.angle == 0) ? CGAffineTransformIdentity : CGAffineTransformMakeRotation(self.angle);
[self.animatedViews addObject:view];
}
}
- (void)removeView:(UIView *)view
{
[self.animatedViews removeObject:view];
}
@end
|
Comments (2)
You can clone a snippet to your computer for local editing. Learn more.
A modified version of an existing UIView category, which rotates a given view to maintain a parallelism with the horizon as the orientation of an iPhone changes per rotation. This implementation is the most resource-conservative and smoothest of any other.
To use it: Add the category to any implementation file it references the view you want to rotate, and then set the followsGravity property of that view to TRUE.