iPhone开发中touch的事件随笔(附touch移动事件)

原文地址: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);

如何判断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"); 
             } 
    } 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值