Snippets

James Bush 74XxB: Untitled snippet

Created by James Bush
//
//  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)

  1. James Bush

    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.

  2. James Bush

    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.

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.