> IOS开发在线手册 > iOS开发-UISwipeGestureRecognizer滑动手势

滑动手势也算是iOS中交互中很重要的一部分,上下左右滑动,UISwipeGestureRecognizer可以很轻松的解决这个问题,没什么难度直接看代码吧:

    UISwipeGestureRecognizer  *upSwipeGestureRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    upSwipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:upSwipeGestureRecognizer];
    
    
    UISwipeGestureRecognizer  *downSwipeGestureRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    downSwipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:downSwipeGestureRecognizer];
    
    
    UISwipeGestureRecognizer  *leftSwipeGestureRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    leftSwipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer];
    
    
    UISwipeGestureRecognizer  *rightSwipeGestureRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    rightSwipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGestureRecognizer];

手势处理:

-(void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)recongnizer{
    if (recongnizer.direction==UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"向上滑动");
        NSLog(@"博客园-FlyElephant");
    }
    
    if (recongnizer.direction==UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下滑动");
        NSLog(@"原文地址:Http://www.cnblogs.com/xiaofeixiang");
    }
    
    if (recongnizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左滑动");
        NSLog(@"iOS技术交流群:228407086");
    }
    
    if (recongnizer.direction==UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"向右滑动");
    }
}

效果图:

iOS开发-UISwipeGestureRecognizer滑动手势