//
// SwipeViewController.m
// 9 GestureGecognizer
//
// Created by Tracy on 15/5/28.
// Copyright (c) 2015年 Tracy. All rights reserved.
//
#import "SwipeViewController.h"
@interface SwipeViewController ()
@property (strong, nonatomic) UILabel *labelMessage;//显示消息的标签
@end
@implementation SwipeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self onCreate];
[self createGestureRecognizer];
}
#pragma mark 创建界面控件
- (void)onCreate {
//创建1个标签
self.labelMessage = [[UILabel alloc]initWithFrame:CGRectMake(10, 64, 200, 30)];
self.labelMessage.text = @"检测扫动";
//加入视图中
[self.view addSubview:self.labelMessage];
}
#pragma mark 创建手势识别器
- (void)createGestureRecognizer {
//横扫手势识别器
UISwipeGestureRecognizer *leftToRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
//竖扫手势识别器
UISwipeGestureRecognizer *topToBottom = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
//设置手势识别器属性(扫动方向)
leftToRight.direction = UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
topToBottom.direction = UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;
//注册手势识别器
[self.view addGestureRecognizer:leftToRight];
[self.view addGestureRecognizer:topToBottom];
}
#pragma mark 识别手势方法
- (void)swipeHandle:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == (UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft) ) {
self.labelMessage.text = @"检测到横扫!";
} else if (recognizer.direction == (UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp)) {
self.labelMessage.text = @"检测到竖扫!";
}
}
@end