Created
August 23, 2011 20:19
-
-
Save mbcharbonneau/1166388 to your computer and use it in GitHub Desktop.
ELCCameraOverlayView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ELCCameraOverlayView.h | |
// ELC Technologies | |
// | |
// Created by Marc Charbonneau on 8/19/11. | |
// Copyright 2011 ELC Technologies. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface ELCCameraOverlayView : UIView <UIAccelerometerDelegate> | |
{ | |
UIButton *_button; | |
UIInterfaceOrientation _interfaceOrientation; | |
} | |
@property(retain, nonatomic) UIButton *button; | |
@property(assign, nonatomic) UIInterfaceOrientation interfaceOrientation; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ELCCameraOverlayView.m | |
// ELC Technologies | |
// | |
// Created by Marc Charbonneau on 8/19/11. | |
// Copyright 2011 ELC Technologies. All rights reserved. | |
// | |
#import "ELCCameraOverlayView.h" | |
@implementation ELCCameraOverlayView | |
#pragma mark API | |
@synthesize button = _button; | |
@synthesize interfaceOrientation = _interfaceOrientation; | |
- (void)setButton:(UIButton *)button; | |
{ | |
if ( _button != button ) | |
{ | |
[_button removeFromSuperview]; | |
[_button release]; | |
_button = [button retain]; | |
[self addSubview:button]; | |
} | |
} | |
- (void)setInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; | |
{ | |
if ( _interfaceOrientation != interfaceOrientation ) | |
{ | |
_interfaceOrientation = interfaceOrientation; | |
[self setNeedsLayout]; | |
} | |
} | |
#pragma mark UIView | |
- (id)initWithFrame:(CGRect)frame; | |
{ | |
if ( ( self = [super initWithFrame:frame] ) ) | |
{ | |
_interfaceOrientation = UIInterfaceOrientationPortrait; | |
_button = [[UIButton alloc] initWithFrame:CGRectMake( 240.0f, 0.0f, 80.0f, 80.0f )]; | |
[[UIAccelerometer sharedAccelerometer] setDelegate:self]; | |
[self addSubview:_button]; | |
} | |
return self; | |
} | |
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; | |
{ | |
if ( [super hitTest:point withEvent:event] == self.button ) | |
return self.button; | |
return nil; | |
} | |
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; | |
{ | |
if ( CGRectContainsPoint( self.button.frame, point ) ) | |
return YES; | |
return NO; | |
} | |
- (void)layoutSubviews; | |
{ | |
CGFloat width = CGRectGetWidth( self.button.frame ); | |
CGFloat height = CGRectGetHeight( self.button.frame ); | |
switch ( self.interfaceOrientation ) | |
{ | |
case UIInterfaceOrientationPortrait: | |
case UIInterfaceOrientationPortraitUpsideDown: | |
self.button.frame = CGRectMake( CGRectGetMaxX( self.bounds ) - width, 0.0f, width, height ); | |
break; | |
case UIInterfaceOrientationLandscapeRight: | |
self.button.frame = CGRectMake( CGRectGetMaxX( self.bounds ) - width, CGRectGetMaxY( self.bounds ) - height - 50.0f, width, height ); | |
break; | |
case UIInterfaceOrientationLandscapeLeft: | |
self.button.frame = CGRectMake( 0.0f, 0.0f, width, height ); | |
break; | |
} | |
} | |
#pragma mark NSObject | |
- (void)dealloc; | |
{ | |
[[UIAccelerometer sharedAccelerometer] setDelegate:nil]; | |
[_button release]; | |
[super dealloc]; | |
} | |
#pragma mark UIAccelerometerDelegate | |
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration; | |
{ | |
CGFloat x = -[acceleration x]; | |
CGFloat y = [acceleration y]; | |
CGFloat angle = atan2(y, x); | |
if ( angle >= -2.25f && angle <= -0.25f ) | |
{ | |
self.interfaceOrientation = UIInterfaceOrientationPortrait; | |
} | |
else if ( angle >= -1.75f && angle <= 0.75f ) | |
{ | |
self.interfaceOrientation = UIInterfaceOrientationLandscapeRight; | |
} | |
else if ( angle >= 0.75f && angle <= 2.25f ) | |
{ | |
self.interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown; | |
} | |
else if ( angle <= -2.25f || angle >= 2.25f ) | |
{ | |
self.interfaceOrientation = UIInterfaceOrientationLandscapeLeft; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment