1 / 22

iPhone App Development Lecture 18 - Accelerometer & Core Graphic

Learn about accelerometers, core graphics, and Quartz 2D in iPhone application development. Build the Ball app from chapter 15 and add touch features. Also, build the Draw app for CG drawing.

jisaacs
Télécharger la présentation

iPhone App Development Lecture 18 - Accelerometer & Core Graphic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEC-492/693/793iPhone Application Development Lecture 18 Wenbing Zhao & Nigamanth Sridhar EEC492/693/793 - iPhone Application Development

  2. Outline • Accelerometer • Core Graphic and Quartz 2D • Assignment: • Build the Ball app in chapter 15 • Challenge: add the touch feature • Build the Draw app for CG drawing EEC492/693/793 - iPhone Application Development

  3. What Are Accelerometers? • Measure changes in force Y direction: opposite of coordinate system EEC492/693/793 - iPhone Application Development

  4. Kinds of Orientation • Physical orientation • How is the device positioned? • Interface orientation • Where is the status bar? EEC492/693/793 - iPhone Application Development

  5. Getting the Physical Orientation • UIDevice class • Start notifications • beginGeneratingDeviceOrientationNotifications • Get Orientation • // delivered to registered observers • UIDeviceOrientationDidChangeNotification • orientation property • Stop notifications • endGeneratingDeviceOrientationNotifications EEC492/693/793 - iPhone Application Development

  6. Getting the Interface Orientation • UIApplication class • statusBarOrientation property • Defines interface orientation, not device orientation • UIViewController class • interfaceOrientation property - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation EEC492/693/793 - iPhone Application Development

  7. Shake Detection • UIEvent type • @property(readonly) UIEventType type; • @property(readonly) UIEventSubtype subtype; • UIEventTypeMotion • UIEventSubtypeMotionShake - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event; - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {  if (event.type == UIEventSubtypeMotionShake) {    //Your code here  }} EEC492/693/793 - iPhone Application Development

  8. Getting the Raw Accelerometer Data • Part of the UIKit framework • Delivers 3-axis data • Configurable update frequency (approx 10–100Hz) • Delegate-based event delivery • Classes • UIAccelerometer • UIAcceleration • Protocol • UIAccelerometerDelegate EEC492/693/793 - iPhone Application Development

  9. Configuring the Accelerometer:Starting the event delivery • Event delivery begins as soon as you assign the delegate - (void)enableAccelerometerEvents { UIAccelerometer* theAccel = [UIAccelerometer sharedAccelerometer]; theAccel.updateInterval = 1/50; // 50 Hz theAccel.delegate = self; } EEC492/693/793 - iPhone Application Development

  10. Defining Your Delegate Object:Processing the accelerometer data • Only one delegate per application • Delivered asynchronously to main thread - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration { // Get the event data UIAccelerationValue x, y, z; x = acceleration.x; y = acceleration.y; z = acceleration.z; // Process the data... } EEC492/693/793 - iPhone Application Development

  11. Configuring the Accelerometer:Choosing an appropriate update frequency • System range is approximately 10–100Hz • Frequency should be based on need • Determine the minimum frequency for your needs • Don’t update too frequently • Target ranges • Game input: 30–60 Hz • Orientation detection: 10–20 Hz EEC492/693/793 - iPhone Application Development

  12. Disabling Event Delivery:Stopping the event delivery - (void)disableAccelerometerEvents { UIAccelerometer* theAccel = [UIAccelerometer sharedAccelerometer]; theAccel.delegate = nil; } EEC492/693/793 - iPhone Application Development

  13. CoreGraphics and Quartz 2D • UIKit offers very basic drawing functionality UIRectFill(CGRect rect); UIRectFrame(CGRect rect); • CoreGraphics: Drawing APIs • CG is a C-based API, not Objective-C • CG and Quartz 2D drawing engine define simple but powerful • graphics primitives • Graphics context • Transformations • Paths • Colors • Fonts • Painting operations EEC492/693/793 - iPhone Application Development

  14. Graphics Contexts • All drawing is done into an opaque graphics context • Draws to screen, bitmap buffer, printer, PDF, etc. • Graphics context setup automatically before invoking drawRect: • Defines current path, line width, transform, etc. • Access the graphics context within drawRect: by calling (CGContextRef)UIGraphicsGetCurrentContext(void); • Use CG calls to change settings • Context only valid for current call to drawRect: • Do not cache a CGContext! EEC492/693/793 - iPhone Application Development

  15. CG Wrappers • UIColor • Convenience for common colors • Easily set the fill and/or stroke colors when drawing UIColor *redColor = [UIColor redColor]; [redColor set]; // drawing will be done in red • UIFont • Access system font • Get font by name UIFont *font = [UIFont systemFontOfSize:14.0]; [myLabel setFont:font]; EEC492/693/793 - iPhone Application Development

  16. Drawing Custom Views • - (void)drawRect:(CGRect)rect • -[UIView drawRect:] does nothing by default • If not overridden, then backgroundColor is used to fill • Override - drawRect: to draw a custom view • rect argument is area to draw • Should never call drawRect: directly • Call –(void)setNeedsDisplay instead! EEC492/693/793 - iPhone Application Development

  17. Simple drawRect: Example • Draw a solid color and shape - (void)drawRect:(CGRect)rect { CGRect bounds = [self bounds]; [[UIColor grayColor] set]; UIRectFill (bounds); CGRect square = CGRectMake (10, 10, 50, 100); [[UIColor redColor] set]; UIRectFill (square); [[UIColor blackColor] set]; UIRectFrame (square); } EEC492/693/793 - iPhone Application Development

  18. Drawing More Complex Shapes • Common steps for drawRect: are • Get current graphics context • Define a path • Set a color • Stroke or fill path • Repeat, if necessary EEC492/693/793 - iPhone Application Development

  19. CGPath • CoreGraphics paths define shapes • Made up of lines, arcs, curves and rectangles • Creation and drawing of paths are two distinct operations • Define path first, then draw it EEC492/693/793 - iPhone Application Development

  20. Path Example - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); [[UIColor grayColor] set]; UIRectFill ([self bounds]); CGContextBeginPath (context); CGContextMoveToPoint (context, 75, 10); CGContextAddLineToPoint (context, 10, 150); CGContextAddLineToPoint (context, 160, 150); CGContextClosePath (context); [[UIColor redColor] setFill]; [[UIColor blackColor] setStroke]; CGContextDrawPath (context, kCGPathFillStroke); } EEC492/693/793 - iPhone Application Development

  21. Drawing Text & Images • You can draw UIImages in -drawRect: - [UIImage drawAtPoint:(CGPoint)point] - [UIImage drawInRect:(CGRect)rect] - [UIImage drawAsPatternInRect:(CGRect)rect] • You can draw NSString in -drawRect: - [NSString drawAtPoint:(CGPoint)point withFont:(UIFont *)font] EEC492/693/793 - iPhone Application Development

  22. Build Two Apps EEC492/693/793 - iPhone Application Development

More Related