原文地址:http://www.cnblogs.com/ydhliphonedev/archive/2011/09/05/2167184.html
在ViewController中重写touch的事件的方法体就可实现特定的touch功能(但这些touch事件会被加在之上的tableView或scrollView等屏蔽,希望知道解决方案的留下方法).
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text =@"Touches Began"; //开始触摸的方法 [self updateLabelsFromTouches:touches]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ messageLabel.text =@"Touches Cancelled";//触摸取消的方法 [self updateLabelsFromTouches:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text =@"Touches Stopped.";//触摸结束的方法 [self updateLabelsFromTouches:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { messageLabel.text =@"Drag Detected";//触摸移动的方法 [self updateLabelsFromTouches:touches]; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view];//开始触摸 } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{ UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); if (deltaX >= kMinimumGestureLength && deltaY <=kMaximumVariance) {//kMinimumGestureLength 最小移动长度 kMaximumVariance 最大偏移长度 label.text =@"Horizontal swipe detected";//水平消除 [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } elseif (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { label.text =@"Vertical swipe detected";//垂直消除 [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject];//实例一个uitouch
NSUInteger tapCount = [touch tapCount]; //计算touch的tapCount次数 switch (tapCount)
{
case1: [self singleTap]; break; case2: [self doubleTap]; break; case3: [self tripleTap]; break; case4: [self quadrupleTap]; break; default: break; } }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{//开始触碰 if ([touches count] ==2)
{//检测是否为两个手指触点 NSArray *twoTouches = [touches allObjects]; UITouch *first = [twoTouches objectAtIndex:0]; UITouch *second = [twoTouches objectAtIndex:1]; initialDistance = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{//移动手指 if ([touches count] ==2)
{ NSArray *twoTouches = [touches allObjects]; UITouch *first = [twoTouches objectAtIndex:0]; UITouch *second = [twoTouches objectAtIndex:1]; CGFloat currentDistance = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); if (initialDistance ==0) initialDistance = currentDistance; //根据移动前后的坐标距离差检测是捏合的手势还是打开的手势 elseif (currentDistance - initialDistance > kMinimumPinchDelta)
{//检测是否大于最小移动值kMinimumPinchDelta label.text =@"Outward Pinch"; [self performSelector:@selector(eraseLabel) withObject:nil afterDelay:1.6f]; } elseif (initialDistance - currentDistance > kMinimumPinchDelta)
{ label.text =@"Inward Pinch"; [self performSelector:@selector(something) withObject:nil afterDelay:1.6f]; } } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{//触碰结束
initialDistance =0; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; lastPreviousPoint = point; lastCurrentPoint = point; lineLengthSoFar =0.0f; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{ UITouch *touch = [touches anyObject]; CGPoint previousPoint = [touch previousLocationInView:self.view]; CGPoint currentPoint = [touch locationInView:self.view]; CGFloat angle = angleBetweenLines(lastPreviousPoint, //计算两条线之间的角度 lastCurrentPoint, previousPoint, currentPoint); if (angle >= kMinimumCheckMarkAngle&& angle <= kMaximumCheckMarkAngle && lineLengthSoFar > kMinimumCheckMarkLength)
{//检测手势被承认的条件 kMinimumCheckMarkAngle 最小角度kMaximumCheckMarkAngle最大角度kMinimumCheckMarkLength 画线最小长度 label.text =@"Checkmark"; [self performSelector:@selector(something) withObject:nil afterDelay:1.6]; } lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint);//lineLengthSoFar , lastPreviousPoint, lastCurrentPoint 重新赋值
lastPreviousPoint = previousPoint;
lastCurrentPoint = currentPoint; } 这里用到一个判断两条直线的角度的方法 angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPointlin2End);
这是在ios开发中常见的功能。即,touch移动事件,是移动到当前视图的子视图中,还是移动到当前视图以外了。
办法是,继承UIView,覆盖touchesMoved方法:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
if (![self pointInside:[touch locationInView:self] withEvent:nil]){
NSLog(@"touches moved outside the view");
}else
{
UIView *hitView=[self hitTest:[[touches anyObject] locationInView:self] withEvent:nil];
if (hitView==self){
NSLog(@"touches moved in the view");
}else
{
NSLog(@"touches moved in the subview");
}
}
}