- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
// location.x
// location.y}
----------------------------------------------------------------------------------------------------
// 多點觸控
A. 新增一個 Window-based Application Project: multiTouch
B. 新增繼承 UIView 的 Class: TouchView
----------------------------------------------------------------------------------------------------
C. multiTouchAppDelegate.h
#import <UIKit/UIKit.h>
//@add
#import "TouchView.h"
@interface multiTouchAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
----------------------------------------------------------------------------------------------------
D. multiTouchAppDelegate.m
#import "multiTouchAppDelegate.h"
@implementation multiTouchAppDelegate
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//@add
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque]; // 改變狀態列背景
TouchView *touchView = [[[TouchView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[window addSubview:touchView];
[window makeKeyAndVisible];
return YES;
}
----------------------------------------------------------------------------------------------------
E. TouchView.h
#import <UIKit/UIKit.h>
@interface TouchView : UIView {
//@add
CGPoint touch1;
CGPoint touch2;
double unitDistance;
double initialDistance;
}
//@add
- (double)getDistance:(CGPoint)fromPoint toPoint:(CGPoint)otherPoint;
@end
----------------------------------------------------------------------------------------------------
F. TouchView.m
#import "TouchView.h"
@implementation TouchView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
//@add
[self setMultipleTouchEnabled:YES];
}
return self;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *allTouches = [touches allObjects];
int count = [allTouches count];
if (count > 0)
touch1 = [[allTouches objectAtIndex:0] locationInView:self];
if (count > 1)
{
touch2 = [[allTouches objectAtIndex:1] locationInView:self];
initialDistance = [self getDistance:touch1 toPoint:touch2];
NSLog(@"initialDistance:%f",initialDistance);
}
[self setNeedsDisplay]; // 強制更新畫面: invoke -> drawRect
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [touches allObjects];
int count = [allTouches count];
if (count > 0)
touch1 = [[allTouches objectAtIndex:0] locationInView:self];
if (count > 1)
{
touch2 = [[allTouches objectAtIndex:1] locationInView:self];
unitDistance = [self getDistance:touch1 toPoint:touch2];
NSLog(@"unitDistance:%f",unitDistance);
}
[self setNeedsDisplay]; // 強制更新畫面: invoke -> drawRect
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (initialDistance > unitDistance) {
// 縮小
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"multiTouch" message:@"Your touch is close to eachother" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil];
[alert show];
[alert release];
}
else if (initialDistance < unitDistance){
// 放大
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"multiTouch" message:@"Your touch is distant from eachother" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil];
[alert show];
[alert release];
}
}
// 二點距離: 平方相加, 開根號
- (double)getDistance:(CGPoint)fromPoint toPoint:(CGPoint)otherPoint
{
double deltaX = otherPoint.x - fromPoint.x;
double deltaY = otherPoint.y - fromPoint.y;
return sqrt(pow(deltaX, 2) + pow(deltaY, 2));
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//@add Line
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context,touch1.x,touch1.y);
CGContextAddLineToPoint(context,touch2.x,touch2.y);
CGContextStrokePath(context);
}
- (void)dealloc {
[super dealloc];
}
@end
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。