📄 appcontroller.m
字号:
/*File: AppController.mAbstract: The UIApplication delegate class, which is the central controller ofthe application.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 "AppController.h"//CONSTANTS:#define kPaletteHeight 30#define kPaletteSize 5#define kAccelerometerFrequency 25 //Hz#define kFilteringFactor 0.1#define kMinEraseInterval 0.5#define kEraseAccelerationThreshold 2.0// Padding for margins#define kLeftMargin 10.0#define kTopMargin 10.0#define kRightMargin 10.0 //FUNCTIONS:/* HSL2RGB Converts hue, saturation, luminance values to the equivalent red, green and blue values. For details on this conversion, see Fundamentals of Interactive Computer Graphics by Foley and van Dam (1982, Addison and Wesley) You can also find HSL to RGB conversion algorithms by searching the Internet. See also http://en.wikipedia.org/wiki/HSV_color_space for a theoretical explanation */static void HSL2RGB(float h, float s, float l, float* outR, float* outG, float* outB){ float temp1, temp2; float temp[3]; int i; // Check for saturation. If there isn't any just return the luminance value for each, which results in gray. if(s == 0.0) { if(outR) *outR = l; if(outG) *outG = l; if(outB) *outB = l; return; } // Test for luminance and compute temporary values based on luminance and saturation if(l < 0.5) temp2 = l * (1.0 + s); else temp2 = l + s - l * s; temp1 = 2.0 * l - temp2; // Compute intermediate values based on hue temp[0] = h + 1.0 / 3.0; temp[1] = h; temp[2] = h - 1.0 / 3.0; for(i = 0; i < 3; ++i) { // Adjust the range if(temp[i] < 0.0) temp[i] += 1.0; if(temp[i] > 1.0) temp[i] -= 1.0; if(6.0 * temp[i] < 1.0) temp[i] = temp1 + (temp2 - temp1) * 6.0 * temp[i]; else { if(2.0 * temp[i] < 1.0) temp[i] = temp2; else { if(3.0 * temp[i] < 2.0) temp[i] = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp[i]) * 6.0; else temp[i] = temp1; } } } // Assign temporary values to R, G, B if(outR) *outR = temp[0]; if(outG) *outG = temp[1]; if(outB) *outB = temp[2];}//CLASS IMPLEMENTATIONS:@implementation AppController- (void) applicationDidFinishLaunching:(UIApplication*)application{ CGRect rect = [[UIScreen mainScreen] applicationFrame]; CGFloat components[3]; //Create a full-screen window window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setBackgroundColor:[UIColor blackColor]]; //Create the OpenGL drawing view and add it to the window drawingView = [[PaintingView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)]; // - kPaletteHeight [window addSubview:drawingView]; // Create a segmented control so that the user can choose the brush color. UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects: [UIImage imageNamed:@"Red.png"], [UIImage imageNamed:@"Yellow.png"], [UIImage imageNamed:@"Green.png"], [UIImage imageNamed:@"Blue.png"], [UIImage imageNamed:@"Purple.png"], nil]]; // Compute a rectangle that is positioned correctly for the segmented control you'll use as a brush color palette CGRect frame = CGRectMake(rect.origin.x + kLeftMargin, rect.size.height - kPaletteHeight - kTopMargin, rect.size.width - (kLeftMargin + kRightMargin), kPaletteHeight); segmentedControl.frame = frame; // When the user chooses a color, the method changeBrushColor: is called. [segmentedControl addTarget:self action:@selector(changeBrushColor:) forControlEvents:UIControlEventValueChanged]; segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; // Make sure the color of the color complements the black background segmentedControl.tintColor = [UIColor darkGrayColor]; // Set the third color (index values start at 0) segmentedControl.selectedSegmentIndex = 2; // Add the control to the window [window addSubview:segmentedControl]; // Now that the control is added, you can release it [segmentedControl release]; // Define a starting color HSL2RGB((CGFloat) 2.0 / (CGFloat)kPaletteSize, kSaturation, kLuminosity, &components[0], &components[1], &components[2]); // Set the color using OpenGL glColor4f(components[0], components[1], components[2], kBrushOpacity); //Show the window [window makeKeyAndVisible]; // Look in the Info.plist file and you'll see the status bar is hidden // Set the style to black so it matches the background of the application [application setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO]; // Now show the status bar, but animate to the style. [application setStatusBarHidden:NO animated:YES]; //Configure and enable the accelerometer [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)]; [[UIAccelerometer sharedAccelerometer] setDelegate:self]; //Load the sounds NSBundle *mainBundle = [NSBundle mainBundle]; erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]]; selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];}// Release resources when they are no longer needed,- (void) dealloc{ [selectSound release]; [erasingSound release]; [drawingView release]; [window release]; [super dealloc];}// Change the brush color- (void)changeBrushColor:(id)sender{ CGFloat components[3]; //Play sound [selectSound play]; //Set the new brush color HSL2RGB((CGFloat)[sender selectedSegmentIndex] / (CGFloat)kPaletteSize, kSaturation, kLuminosity, &components[0], &components[1], &components[2]); glColor4f(components[0], components[1], components[2], kBrushOpacity); }// Called when the accelerometer detects motion; plays the erase sound and redraws the view if the motion is over a threshold.- (void) accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{ UIAccelerationValue length, x, y, z; //Use a basic high-pass filter to remove the influence of the gravity myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor); myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor); myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor); // Compute values for the three axes of the acceleromater x = acceleration.x - myAccelerometer[0]; y = acceleration.y - myAccelerometer[0]; z = acceleration.z - myAccelerometer[0]; //Compute the intensity of the current acceleration length = sqrt(x * x + y * y + z * z); // If above a given threshold, play the erase sounds and erase the drawing view if((length >= kEraseAccelerationThreshold) && (CFAbsoluteTimeGetCurrent() > lastTime + kMinEraseInterval)) { [erasingSound play]; [drawingView erase]; lastTime = CFAbsoluteTimeGetCurrent(); }}@end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -