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"); 
             } 
    } 
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM