iPhone开发中touch的事件
2014-07-10 11:57
211 查看
原文地址:http://www.cnblogs.com/ydhliphonedev/archive/2011/09/05/2167184.html
在ViewController中重写touch的事件的方法体就可实现特定的touch功能(但这些touch事件会被加在之上的tableView或scrollView等屏蔽,希望知道解决方案的留下方法).
下面代码实现清扫事件
触摸-轻扫:
多次轻击判断:
捏合操作:
自定义手势“√”:
办法是,继承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");
}
}
}
在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);
如何判断touch到子视图或离开视图
这是在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");
}
}
}
相关文章推荐
- iPhone开发中touch的事件随笔
- iPhone开发中touch的事件随笔
- iPhone开发--检查UIWebView上Touch/UITapGestureRecognizer事件的最简单的方法(转载)
- iPhone开发之区分 touch 事件是单击还是双击的方法
- iOS开发_iphone 触屏指触行为大全_iphone 有哪些触控事件
- iphone应用程序开发指南--触摸事件(笔记 3 )
- Iphone开发(十)简单的列表tableView与行的响应事件
- iPhone开发进阶(8)--- 检测屏幕触摸事件
- Iphone开发(十)简单的列表tableView与行的响应事件
- iPhone开发--提交appStore审核时出现的问题--iPhone/iPod Touch: application executable is missing a required archit
- iPhone开发进阶(8)— 检测屏幕触摸事件
- Sencha touch 开发系列:类与ST组件,事件,自定义组件
- iPhone开发指南-事件处理
- iphone开发:关于触屏事件的一些操作
- iPhone开发--UITextView中的文字改变时触发的事件
- iPhone开发指南-事件处理
- iphone 开发周记2 触摸事件处理与简单绘图。
- Unity iPhone Touch Animation Tutorial,untiy3d iphone简单动画开发教程
- iPhone开发指南-事件处理
- Sencha Touch开发实例:新浪微博iPhone界面