?? graphview.m
字號:
/*===== IMPORTANT =====This is sample code demonstrating API, technology or techniques in development.Although this sample code has been reviewed for technical accuracy, it is notfinal. Apple is supplying this information to help you plan for the adoption ofthe technologies and programming interfaces described herein. This informationis subject to change, and software implemented based on this sample code shouldbe tested with final operating system software and final documentation. Newerversions of this sample code may be provided with future seeds of the API ortechnology. For information about updates to this and other developerdocumentation, view the New & Updated sidebars in subsequent documentationseeds.=====================File: GraphView.mAbstract: This class is responsible for updating and drawing the accelerometerhistory of values. The history is a circular buffer implementation, with apointer moving repeatedly through the buffer, resetting to zero each time itreaches the end.Version: 1.6Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.("Apple") in consideration of your agreement to the following terms, and youruse, installation, modification or redistribution of this Apple softwareconstitutes acceptance of these terms. If you do not agree with these terms,please do not use, install, modify or redistribute this Apple software.In consideration of your agreement to abide by the following terms, and subjectto these terms, Apple grants you a personal, non-exclusive license, underApple's copyrights in this original Apple software (the "Apple Software"), touse, reproduce, modify and redistribute the Apple Software, with or withoutmodifications, in source and/or binary forms; provided that if you redistributethe Apple Software in its entirety and without modifications, you must retainthis notice and the following text and disclaimers in all such redistributionsof the Apple Software.Neither the name, trademarks, service marks or logos of Apple Inc. may be usedto endorse or promote products derived from the Apple Software without specificprior written permission from Apple. Except as expressly stated in this notice,no other rights or licenses, express or implied, are granted by Apple herein,including but not limited to any patent rights that may be infringed by yourderivative works or by other works in which the Apple Software may beincorporated.The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NOWARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIEDWARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR INCOMBINATION WITH YOUR PRODUCTS.IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTEGOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/ORDISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OFCONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IFAPPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.Copyright (C) 2008 Apple Inc. All Rights Reserved.*/#import "GraphView.h"// Constant for maximum acceleration.#define kMaxAcceleration 3.0// Constant for the high-pass filter.#define kFilteringFactor 0.1// GraphView class implementation.@implementation GraphView// Instruct the compiler to generate accessors for the property, and use the internal variable _filter for storage.@synthesize filteringEnabled=filter;- (void)updateHistoryWithX:(float)x Y:(float)y Z:(float)z { // If filtering is active, apply a basic high-pass filter to remove the gravity influence from the accelerometer values if (filter) { acceleration[0] = x * kFilteringFactor + acceleration[0] * (1.0 - kFilteringFactor); history[nextIndex][0] = x - acceleration[0]; acceleration[1] = y * kFilteringFactor + acceleration[1] * (1.0 - kFilteringFactor); history[nextIndex][1] = y - acceleration[1]; acceleration[2] = z * kFilteringFactor + acceleration[2] * (1.0 - kFilteringFactor); history[nextIndex][2] = z - acceleration[2]; } else { history[nextIndex][0] = x; history[nextIndex][1] = y; history[nextIndex][2] = z; } // Advance buffer pointer to next position or reset to zero. nextIndex = (nextIndex + 1) % kHistorySize;}- (void)setFilteringEnabled:(BOOL)enabled { filter = enabled; // Reset the acceleration filter. acceleration[0] = acceleration[1] = acceleration[2] = 0.0;}- (void)drawHistory:(unsigned)axis fromIndex:(unsigned)index inContext:(CGContextRef)context bounds:(CGRect)bounds { UIFont *font = [UIFont systemFontOfSize:12]; unsigned i; float value, temp; // Draw the background CGContextSetGrayFillColor(context, 0.6, 1.0); CGContextFillRect(context, bounds); // Draw the intermediate lines CGContextSetGrayStrokeColor(context, 0.5, 1.0); CGContextBeginPath(context); for (value = -kMaxAcceleration + 1.0; value <= kMaxAcceleration - 1.0; value += 1.0) { if (value == 0.0) { continue; } temp = roundf(bounds.origin.y + bounds.size.height / 2 + value / (2 * kMaxAcceleration) * bounds.size.height); CGContextMoveToPoint(context, bounds.origin.x, temp); CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, temp); } CGContextStrokePath(context); // Draw the center line CGContextSetGrayStrokeColor(context, 1.0, 1.0); CGContextBeginPath(context); temp = roundf(bounds.origin.y + bounds.size.height / 2); CGContextMoveToPoint(context, bounds.origin.x, temp); CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, temp); CGContextStrokePath(context); // Draw the top & bottom lines CGContextSetGrayStrokeColor(context, 0.25, 1.0); CGContextBeginPath(context); CGContextMoveToPoint(context, bounds.origin.x, bounds.origin.y); CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, bounds.origin.y); CGContextMoveToPoint(context, bounds.origin.x, bounds.origin.y + bounds.size.height); CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height); CGContextStrokePath(context); // Draw the history lines CGContextSetRGBStrokeColor(context, (axis == 0 ? 1.0 : 0.0), (axis == 1 ? 1.0 : 0.0), (axis == 2 ? 1.0 : 0.0), 1.0); CGContextBeginPath(context); for (i = 0; i < kHistorySize; ++i) { // NOTE: We need to draw upside-down as UIView referential has the Y axis going down value = history[(index + i) % kHistorySize][axis] / -kMaxAcceleration; if (i > 0) { CGContextAddLineToPoint(context, bounds.origin.x + (float)i / (float)(kHistorySize - 1) * bounds.size.width, bounds.origin.y + bounds.size.height / 2 + value * bounds.size.height / 2); } else { CGContextMoveToPoint(context, bounds.origin.x + (float)i / (float)(kHistorySize - 1) * bounds.size.width, bounds.origin.y + bounds.size.height / 2 + value * bounds.size.height / 2); } } CGContextStrokePath(context); // Draw the labels CGContextSetGrayFillColor(context, 1.0, 1.0); CGContextSetAllowsAntialiasing(context, true); for (value = -kMaxAcceleration; value <= kMaxAcceleration - 1.0; value += 1.0) { temp = roundf(bounds.origin.y + bounds.size.height / 2 + value / (2 * kMaxAcceleration) * bounds.size.height); // NOTE: We need to draw upside-down as UIView referential has the Y axis going down [[NSString stringWithFormat:@"%+.1f", -(value >= 0.0 ? value + 1.0 : value)] drawAtPoint:CGPointMake(bounds.origin.x + 4, temp + (value >= 0.0 ? 3 : 0)) withFont:font]; } temp = roundf(bounds.origin.y + bounds.size.height / 2); CGPoint sPoint = CGPointMake(bounds.origin.x + bounds.size.width - 40, temp - 16); [[NSString stringWithFormat:@"%c Axis", 'X' + axis] drawAtPoint:sPoint withFont:font]; CGContextSetAllowsAntialiasing(context, false);}- (void)drawRect:(CGRect)clip { CGSize size = [self bounds].size; CGContextRef context = UIGraphicsGetCurrentContext(); unsigned index = nextIndex, i; // Draw the X, Y & Z graphs with anti-aliasing turned off CGContextSetAllowsAntialiasing(context, false); CGFloat hOver3 = size.height / 3, hOver4 = size.height / 4; for (i = 0; i < 3; ++i) { CGRect hBounds = CGRectMake(0, (hOver3 - hOver4) / 2 + (float)i * hOver3, size.width, hOver4); [self drawHistory:i fromIndex:index inContext:context bounds:hBounds]; } CGContextSetAllowsAntialiasing(context, true); }@end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -