zoukankan      html  css  js  c++  java
  • iphone开发中的手势操作:Swipes

    - (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) {
    label.text = @"Horizontal swipe detected";
    [self performSelector:@selector(eraseText)
    withObject:nil afterDelay:2];
    } else if (deltaY >= kMinimumGestureLength &&
    deltaX <= kMaximumVariance){
    label.text = @"Vertical swipe detected";
    [self performSelector:@selector(eraseText) withObject:nil
    afterDelay:2];
    }
    }

    亦可以使用Automatic Gesture Recognition :(UIGestureRecognizer)

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc]
    initWithTarget:self action:@selector(reportVerticalSwipe:)];
    vertical.direction = UISwipeGestureRecognizerDirectionUp|
    UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:vertical];

    UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc]
    initWithTarget:self action:@selector(reportHorizontalSwipe:)];
    horizontal.direction = UISwipeGestureRecognizerDirectionLeft|
    UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:horizontal];
    }

    然后加上自定义的两个响应方法:

    - (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer {
    label.text = @"Horizontal swipe detected";
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
    }

    - (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer {
    label.text = @"Vertical swipe detected";
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
    }

    就OK啦!

    以上的只是单个轻扫动作,下面的是多个轻扫动作同时进行的情况:

    在viewDidLoad中添加循环:

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    for (NSUInteger touchCount = 1; touchCount <= 5; touchCount++) {
    UISwipeGestureRecognizer *vertical;
    vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self
    action:@selector(reportVerticalSwipe:)];
    vertical.direction = UISwipeGestureRecognizerDirectionUp|
    UISwipeGestureRecognizerDirectionDown;
    vertical.numberOfTouchesRequired = touchCount;
    [self.view addGestureRecognizer:vertical];

    UISwipeGestureRecognizer *horizontal;
    horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self
    action:@selector(reportHorizontalSwipe:)];
    horizontal.direction = UISwipeGestureRecognizerDirectionLeft|
    UISwipeGestureRecognizerDirectionRight;
    horizontal.numberOfTouchesRequired = touchCount;
    [self.view addGestureRecognizer:horizontal];
    }
    }

    修改对于响应动作函数:

    - (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer {
    label.text = [NSString stringWithFormat:@"%@Horizontal swipe detected",
    [self descriptionForTouchCount:[recognizer numberOfTouches]]];
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
    }

    - (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer {
    label.text = [NSString stringWithFormat:@"%@Vertical swipe detected",
    [self descriptionForTouchCount:[recognizer numberOfTouches]]];;
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
    }
    - (NSString *)descriptionForTouchCount:(NSUInteger)touchCount {
    switch (touchCount) {
    case 2:
    return @"Double ";
    case 3:
    return @"Triple ";
    case 4:
    return @"Quadruple ";
    case 5:
    return @"Quintuple ";
    default:
    return @"";
    }
    }





  • 相关阅读:
    axis2 WebService的发布与调用
    Lucene、Compass学习以及与SSH的整合
    qsort函数应用大全
    Effective C++ ——模板和泛型编程
    Effective C++ ——继承与面向对象设计
    Effective C++ ——实现
    Effective C++ ——设计与声明
    Effective C++ ——资源管理
    Effective C++ ——构造/析构/赋值运算符
    Effective C++ ——让自己习惯C++
  • 原文地址:https://www.cnblogs.com/mybkn/p/2417446.html
Copyright © 2011-2022 走看看