Graphics iPhone; Sensors




CS175

Chris Pollett

Nov 12, 2014

Outline

Introduction

QuartzTestView.h

QuartzTestView.m

Here is the implementation of QuartzTestView. The drawRect method is where we override the base UIView ways of drawing things. We also have code to handle touch events (when they start, end and how they change).


#import "QuartzTestView.h"
#import "UIRandomColor.h"

@implementation QuartzTestView
//we're using initWithCoder as we are loading the view from a nib
// and so init and initWithFrame will never be called
-(id)initWithCoder:(NSCoder *)coder
{
    if((self = [super initWithCoder:coder])) {
        _currentColor = [UIColor redColor];
        _useRandomColor = NO;
        _drawImage = [UIImage imageNamed:@"myphoto.jpg"];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, _currentColor.CGColor);
    CGContextSetFillColorWithColor(context, _currentColor.CGColor);

    CGRect currentRect = CGRectMake((_firstTouch.x > _lastTouch.x) ? _lastTouch.x : _firstTouch.x,
                                    (_firstTouch.y > _lastTouch.y) ? _lastTouch.y : _lastTouch.y,
                                    fabsf(_firstTouch.x - _lastTouch.x),
                                    fabsf(_firstTouch.y - _lastTouch.y));
    switch (_shapeType)
    {
        case kLineShape:
            CGContextMoveToPoint(context, _firstTouch.x, _firstTouch.y);
            CGContextAddLineToPoint(context, _lastTouch.x, _lastTouch.y);
            CGContextStrokePath(context);
            break;

        case kRectShape:
            CGContextAddRect(context, currentRect);
            CGContextDrawPath(context, kCGPathFillStroke);
            break;

        case kEllipseShape:
            CGContextAddEllipseInRect(context, currentRect);
            CGContextDrawPath(context, kCGPathFillStroke);
            break;

        case kImageShape: {
            CGFloat horizontalOffset = _drawImage.size.width/2;

            CGFloat verticalOffset = _drawImage.size.height/2;
            CGPoint drawPoint = CGPointMake(_lastTouch.x - horizontalOffset,
                                            _lastTouch.y - verticalOffset);
            [_drawImage drawAtPoint:drawPoint];
            break;
        }
        default:
            break;
    }
}

// Basic idea for touch handlers is to store in fields the touch info and redraw
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(_useRandomColor) {
        self.currentColor = [UIColor randomColor];
    }
    UITouch *touch = [touches anyObject];
    _firstTouch = [touch locationInView:self]; //store touch start info
    _lastTouch = [touch locationInView:self];
    NSLog(@"hi there");
    [self setNeedsDisplay]; // redraw view
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"hi there");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"hi there");
    UITouch *touch = [touches anyObject];
    _lastTouch = [touch locationInView:self];
    [self setNeedsDisplay]; // redraw view
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"hi there");
    UITouch *touch = [touches anyObject];
    _lastTouch = [touch locationInView:self];
    [self setNeedsDisplay]; // redraw view
    
}
@end

QuartzTestViewController.h

QuartzTestViewController.m

QuartzTestViewController Storyboard

Using Locations and the Accelerometer

SensorTestsViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreMotion/CoreMotion.h>

@interface SensorTestsViewController : UIViewController <CLLocationManagerDelegate> 
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@property (weak, nonatomic) IBOutlet UILabel *headingLabel;
@property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CMMotionManager *motionManager;
@property (strong, nonatomic) NSOperationQueue *queue;


@end

SensorTestsViewController.xib

SensorTestsViewController.m

Remarks

Sensor App Running

Android Sensor's Revisited

AcceleratorDemo.java