8.1 Detecting Swipe Gestures

本文介绍了如何在iOS应用中实现滑动手势识别,并根据不同方向触发相应的操作。通过实例展示了如何使用SwiftUI创建一个响应不同滑动方向的应用,包括向下、向上、向左和向右滑动的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


滑动手势


#import "ViewController.h"


@interface ViewController ()


@property (nonatomic, retain)UISwipeGestureRecognizer *swipeGestureRecognizer;


@end


@implementation ViewController


- (void) handleSwipes:(UISwipeGestureRecognizer *)paramSender{

    NSLog(@"paramSender.direction= %d",paramSender.direction);

    if (paramSender.direction & UISwipeGestureRecognizerDirectionDown){

        NSLog(@"Swiped Down.");

    }

    if (paramSender.direction & UISwipeGestureRecognizerDirectionLeft){

        NSLog(@"Swiped Left.");

    }

    if (paramSender.direction & UISwipeGestureRecognizerDirectionRight){

        NSLog(@"Swiped Right.");

    }

    if (paramSender.direction & UISwipeGestureRecognizerDirectionUp){

        NSLog(@"Swiped Up.");

    }

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    /* Instantiate the object */

    self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self

                                                                            action:@selector(handleSwipes:)];

    /* Swipes that are performed from right to left are to be detected */

    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft| UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp;

    /* Just one finger needed */

    self.swipeGestureRecognizer.numberOfTouchesRequired = 1;

    /* Add it to the view */

    [self.view addGestureRecognizer:self.swipeGestureRecognizer];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



输出:

2014-04-09 15:37:05.210 cookbook[686:a0b] paramSender.direction= 15

2014-04-09 15:37:05.211 cookbook[686:a0b] Swiped Down.

2014-04-09 15:37:05.211 cookbook[686:a0b] Swiped Left.

2014-04-09 15:37:05.212 cookbook[686:a0b] Swiped Right.

2014-04-09 15:37:05.212 cookbook[686:a0b] Swiped Up.



发现只有左右滑有响应,上下滑没响应,不知道是不是我理解有误还是怎么着。懂的人请留言


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值