本文摘自:http://www.cocoachina.com/blog/article.php?type=blog&itemid=147
处理多点触控事件Handling Multi-Touch Events
处理多点触控事件,你自己的定制UIview子类别(或者,不频常,你自己的定制UIApplication或UIwindow子类别),要实行至少在其中的UIResponder方法事件处理。以下章节描述这些方法,讨论的方法处理常用手势,显示出典型的回应者物件处理一个复杂序列多点触控事件,并提出了一些技术事件处理。
在这一章节:
事件的处理方法
处理轻拍的手势
处理重拍的手势
在处理复杂的多点触控顺序
事件处理技巧
事件的处理方法
在一个多点触控序列,应用程式分派了一系列的事件讯息。接受和处理这些信息,回应者物件类别必须实作(implement)至少有下列情形之一的方法(methods)公告
UIResponder :
﹣(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
﹣(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
﹣(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UIScrollView *scrollView = (UIScrollView*)[self superview];
UITouch *touch = [touches anyObject];
CGSize size;
CGPoint point;
if([touch tapCount] == 2) {
if(![_viewController _isZoomed]) {
point = [touch locationInView:self];
size = [self bounds].size;
point.x /= size.width;
point.y /= size.height;
[_viewController _setZoomed:YES];
size = [scrollView contentSize];
point.x *= size.width;
point.y *= size.height;
size = [scrollView bounds].size;
point.x -= size.width / 2;
point.y -= size.height / 2;
[scrollView setContentOffset:point animated:NO];
}
else
[_viewController _setZoomed:NO];
}
}
-- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
if (touch.info & UITouchInfoSwipedRight) {
[self showPreviousImage];
} else if (touch.info & UITouchInfoSwipedLeft) {
[self showNextImage];
}
return;
}
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
// Only move the placard view if the touch was in the placard view
if ([touch view] != placardView) {
// On double tap outside placard view, update placard's display string
if ([touch tapCount] == 2) {
[placardView setupNextDisplayString];
}
return;
}
// "Pulse" the placard view by scaling up then down
// Use UIView's built-in animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
placardView.transform = transform;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
transform = CGAffineTransformMakeScale(1.1, 1.1);
placardView.transform = transform;
[UIView commitAnimations];
// Move the placardView to under the touch
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
placardView.center = [self convertPoint:[touch locationInView:self] fromView:placardView];
[UIView commitAnimations];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
// If the touch was in the placardView, move the placardView to its location
if ([touch view] == placardView) {
CGPoint location = [touch locationInView:self];
location = [self convertPoint:location fromView:placardView];
placardView.center = location;
return;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
// If the touch was in the placardView, bounce it back to the center
if ([touch view] == placardView) {
// Disable user interaction so subsequent touches don't interfere with animation
self.userInteractionEnabled = NO;
[self animatePlacardViewToCenter];
return;
}
}
- (void)touchesChangedWithEvent:(UIEvent *)event
{
// if the instrument body is showing...
if (self.frontShowing) {
//iterate through all the touches currently active
NSSet *touches = [event allTouches];
for (UITouch *myTouch in touches) {
CGPoint location = [myTouch locationInView:self];
//if it's a new touch
if (myTouch.phase == UITouchPhaseBegan) {
//test for all kalimba prongs
NSInteger i;
for (i = 0; i < NUMBER_OF_PRONGS ; i++) {
if (CGRectContainsPoint(hitRectPlays, location)) {
//got one... play the note and make it glow
[[InstrumentModel sharedInstrumentModel] playNoteForRegion:(i)];
[[self.glowLayers objectAtIndex:i] setHidden: NO];
}
}
} else if (myTouch.phase == UITouchPhaseMoved) {
CGPoint oldLocation = [myTouch previousLocationInView:self];
// if touch moved off a prong, stop glowing
NSInteger i;
for (i = 0; i < NUMBER_OF_PRONGS ; i++) {
if (CGRectContainsPoint(hitRectPlays, oldLocation) &&
!CGRectContainsPoint(hitRectPlays, location)) {
[[self.glowLayers objectAtIndex:i] setHidden: YES];
}
}
} else if (myTouch.phase == UITouchPhaseEnded) {
// if touch ends on a prong, stop glowing
NSInteger i;
for (i = 0; i < NUMBER_OF_PRONGS ; i++) {
if (CGRectContainsPoint(hitRectPlays, location)) {
[[self.glowLayers objectAtIndex:i] setHidden: YES];
}
}
}
}
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint location = [[touches anyObject] locationInView:self];
CALayer *hitLayer = [[self layer] hitTest:[self convertPoint:location fromView:nil]];
if (hitLayer == infoImage) {
[self displayInfo];
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if ([touches count] == [[event touchesForView:self] count]) {
// last finger has lifted....
}
}