zoukankan      html  css  js  c++  java
  • iPhone开发中touch的事件随笔

    在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);

    by MAC-z

  • 相关阅读:
    Odd sum CodeForces
    Chips CodeForces
    Secrets CodeForces
    Voting CodeForces
    Jury Meeting CodeForces
    Planning CodeForces
    Maxim Buys an Apartment CodeForces
    Chemistry in Berland CodeForces
    Monitor CodeForces
    Four Segments CodeForces
  • 原文地址:https://www.cnblogs.com/ydhliphonedev/p/2167184.html
Copyright © 2011-2022 走看看