• 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 @"";
    }
    }





  • 相关阅读:
    冲刺阶段九
    冲刺阶段八
    学习进度十一
    人月神话阅读笔记01
    单词统计续
    冲刺阶段七
    冲刺阶段六
    冲刺阶段五
    bzoj1570: [JSOI2008]Blue Mary的旅行
    bzoj 1690: [Usaco2007 Dec]奶牛的旅行
  • 原文地址:https://www.cnblogs.com/mybkn/p/2417446.html
Copyright © 2020-2023  润新知