⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myview.m

📁 This is an example applications that shows how to handle multiple touches at the same time on the iP
💻 M
📖 第 1 页 / 共 2 页
字号:
/*File: MyView.mAbstract: MyView contains several subviews, each of which can be moved by touch.Illustrates handling touch events, including multiple touches.Version: 1.7Disclaimer: 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 "MyView.h"#import <QuartzCore/QuartzCore.h>  // Needed for animations@interface MyView()@property (nonatomic, retain) UIImageView *firstPieceView;@property (nonatomic, retain) UIImageView *secondPieceView;@property (nonatomic, retain) UIImageView *thirdPieceView;@property (nonatomic, retain) UILabel *touchPhaseText;@property (nonatomic, retain) UILabel *touchInfoText;@property (nonatomic, retain) UILabel *touchTrackingText;@property (nonatomic, retain) UILabel *touchInstructionsText;@property (nonatomic) BOOL piecesOnTop;  @property (nonatomic) CGPoint startTouchPosition;@end@interface MyView (PrivateMethods)-(UILabel *) newLabelWithOffset:(float)offset numberOfLines:(NSUInteger) lines;-(UIImageView *)newPieceViewWithImageNamed:(NSString *)imageName atPostion:(CGPoint)centerPoint;-(void)animateFirstTouchAtPoint:(CGPoint)touchPoint forView:(UIImageView *)theView;-(void)animateView:(UIImageView *)theView toPosition:(CGPoint) thePosition;-(void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event;-(void) dispatchTouchEvent:(UIView *)theView toPosition:(CGPoint)position;-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position;@end@implementation MyView#define GROW_ANIMATION_DURATION_SECONDS 0.15    // Determines how fast a piece size grows when it is moved.#define SHRINK_ANIMATION_DURATION_SECONDS 0.15  // Determines how fast a piece size shrinks when a piece stops moving.@synthesize firstPieceView;@synthesize secondPieceView;@synthesize thirdPieceView;@synthesize touchPhaseText;@synthesize touchInfoText;@synthesize touchTrackingText;@synthesize touchInstructionsText;@synthesize piecesOnTop;@synthesize startTouchPosition;#pragma mark -#pragma mark === Setting up and tearing down ===#pragma mark/* Unarchives the view that is stored in the xib file. Initializes the main view and adds three subviews, each of which have the appearance of a piece that the user can move. Also creates two text labels, one that displays the touch phase, and the other that displays touch information (such as swipe, number of touches). */- (id)initWithCoder:(NSCoder*)coder {	if (self = [super initWithCoder:coder]) {				self.backgroundColor =[UIColor blackColor];				// Set up the ability to track multiple touches.		[self setMultipleTouchEnabled:YES];				// Create the three views -- each calculates its own frame based on the size of its image		firstPieceView = [self newPieceViewWithImageNamed:@"CyanSquare.png" atPostion:self.center];		[self addSubview:firstPieceView];				secondPieceView = [self newPieceViewWithImageNamed:@"YellowSquare.png" atPostion:CGPointMake(self.center.x -100.,self.center.y -100.)];		[self addSubview:secondPieceView];				thirdPieceView = [self newPieceViewWithImageNamed:@"MagentaSquare.png" atPostion:CGPointMake(self.center.x + 100.,self.center.y + 100.0)];		[self addSubview:thirdPieceView];					// Set up a text label to show the touch phase.		// Create and inialize the label.		touchPhaseText = [self newLabelWithOffset:(self.frame.origin.y + 25) numberOfLines: 1];		// Set the default text that shows on startup.		touchPhaseText.text = @"Touches lets you observe touch";		// Add the label as a subview.	    [self addSubview:touchPhaseText];				// Create and inialite a text label to show the number of touches tracked.		touchTrackingText = [self newLabelWithOffset:(self.frame.origin.y + 45) numberOfLines: 1];			// Set the default text that shows on startup.		touchTrackingText.text = @"phases and multiple taps.";		[self addSubview:touchTrackingText];				// Create and inialite a text label to show multiple tap information.		touchInfoText = [self newLabelWithOffset:(self.frame.origin.y + 65) numberOfLines: 1];		// Add the label as a subview.		[self addSubview:touchInfoText];								// Create and intiate a label to provide instructions to the user when pieces overlap.		touchInstructionsText = [self newLabelWithOffset:(self.frame.size.height - 45) numberOfLines: 2];		[self addSubview:touchInstructionsText];	}	return self;}/* Allocates and initializes a label object.  Sets characteristics to default values.*/-(UILabel *) newLabelWithOffset:(float)offset numberOfLines:(NSUInteger) lines{	float textHeight = 20.0;		UILabel *label =  [[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + 10.0, offset, self.frame.size.width - 20.0, textHeight * lines)];	// Set the font size, text color, background color, and alignment.	label.font = [UIFont systemFontOfSize:18.0];	label.textColor = [UIColor lightTextColor];	label.backgroundColor = [UIColor clearColor];	label.textAlignment = UITextAlignmentCenter;    // Set the number of lines	label.numberOfLines = lines;	if (lines > 1)		// Set the linebreak mode if there is more than one line.		label.lineBreakMode = UILineBreakModeWordWrap;	// Initialize as an empty string	label.text = @"";	return label;	}/* Allocates and initializes a piece view object.  Sets characteristics to default values.*/-(UIImageView *)newPieceViewWithImageNamed:(NSString *)imageName atPostion:(CGPoint)centerPoint{	UIImage *image = [UIImage imageNamed:imageName];	UIImageView *theView = [[UIImageView alloc] initWithImage:image];	// Set the center of the view.	theView.center = centerPoint;	// Set alpha so it is slightly transparent to allow seeing pieces move over each other.	theView.alpha = 0.9;	// Disable user interaction for this view. You must do this if you want to handle touches for more than one object at at time.	// You'll get events for the superview, and then dispatch them to the appropriate subview in the touch handling methods.	theView.userInteractionEnabled = NO;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -