Skip to content

Instantly share code, notes, and snippets.

@jkuip
Created February 24, 2009 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkuip/69436 to your computer and use it in GitHub Desktop.
Save jkuip/69436 to your computer and use it in GitHub Desktop.
//
// MyCustomView.m
// square3
//
// Created by John Kuiphoff on 2/23/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "MyCustomView.h"
#define kAccelerometerFrequency 10 //Hz
#define degreesToRadians(x) (M_PI * x / 180.0)
@implementation MyCustomView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
}
return self;
}
- (void) awakeFromNib
{
// you have to initialize your view here since it's getting
// instantiated by the nib
squareSize = 100.0f;
twoFingers = NO;
oneFingers = NO;
rotation = 0;
// You have to explicity turn on multitouch for the view
self.multipleTouchEnabled = YES;
// configure for accelerometer
[self configureAccelerometer];
}
-(void)configureAccelerometer
{
UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
if(theAccelerometer)
{
theAccelerometer.updateInterval = 1 / kAccelerometerFrequency;
theAccelerometer.delegate = self;
}
else
{
NSLog(@"Oops we're not running on the device!");
}
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
// Do something with the values.
xField.text = [NSString stringWithFormat:@"%.5f", x];
yField.text = [NSString stringWithFormat:@"%.5f", y];
zField.text = [NSString stringWithFormat:@"%.5f", z];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches began count %d, %@", [touches count], touches);
if([touches count] == 2)
{
twoFingers = YES;
} else if([touches count] == 1) {
oneFingers = YES;
}
// tell the view to redraw
[self setNeedsDisplay];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
float xpos = [touch locationInView:self].x;
float ypos = [touch locationInView:self].y;
// Not sure how to access rect properties, so let's fake it
float centerx = 160;
float centery = 240;
float dx = xpos - centerx;
float dy = ypos - centery;
rotation = atan2(dy,dx);
// One finger can now change size
if(oneFingers == YES)
{
squareSize = dx * 2;
} else {
squareSize = squareSize;
}
// tell the view to redraw
[self setNeedsDisplay];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches moved count %d, %@", [touches count], touches);
// reset the var
twoFingers = NO;
oneFingers = NO;
// tell the view to redraw
[self setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect
{
NSLog(@"drawRect");
CGFloat centerx = rect.size.width/2;
CGFloat centery = rect.size.height/2;
CGFloat half = squareSize/2;
CGRect theRect = CGRectMake(-half, -half, squareSize, squareSize);
// Grab the drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
// like Processing pushMatrix
CGContextSaveGState(context);
CGContextTranslateCTM(context, centerx, centery);
// Uncomment to see the rotated square
CGContextRotateCTM(context, rotation);
// Set red stroke
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
// Set different based on multitouch
if(!twoFingers)
{
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
}
else
{
CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1.0);
}
// Draw a rect with a red stroke
CGContextFillRect(context, theRect);
CGContextStrokeRect(context, theRect);
// like Processing popMatrix
CGContextRestoreGState(context);
}
- (void) dealloc
{
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment