AppDelegate.m
//#import "COSTouchVisualizerWindow.h"
#import "AppDelegate.h"
#import "TapViewController.h"
#import "PanViewController.h"
#import "EdgePanViewController.h"
#import "PinchViewController.h"
#import "SwipeViewController.h"
#import "LongPressViewController.h"
/*
手势识别:特定的手势 , 将低级别的动作处理封装为了高级别的动作 , 简化处理;
UIGestureRecognizer 的子类:
tap , pan , swipe , longPress , Pinch(捏合)
*/
/*
手势分为单独手势和连续手势
*/
@interface AppDelegate ()<UITableViewDataSource, UITableViewDelegate>
{
UITableViewController *_tableVC;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_tableVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
_tableVC.tableView.delegate = self;
_tableVC.tableView.dataSource = self;
_tableVC.edgesForExtendedLayout = UIRectEdgeNone;
_tableVC.navigationItem.title = @"Gesture1 Demo";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:_tableVC];
// UITapGestureRecognizer
//numberOfTapsRequired:这一次点击手指需要点几次?
//numberOfTouchesRequired:这一次点击需要几个手指来点击?
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
NSString *title;
switch (indexPath.row) {
case 0:
title = @"Gesture - Tap";
break;
case 1:
title = @"Gesture - Pan";
break;
case 2:
title = @"Gesture - EdgePan";
break;
case 3:
title = @"Gesture - Pinch";
break;
case 4:
title = @"Gesture - Swipe";
break;
case 5:
title = @"Gesture - LongPress";
break;
default:
break;
}
cell.textLabel.text = title;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *targetVC;
switch (indexPath.row) {
case 0:
targetVC = (UIViewController *)[[TapViewController alloc] init];
break;
case 1:
targetVC = (UIViewController *)[[PanViewController alloc] init];
break;
case 2:
targetVC = (UIViewController *)[[EdgePanViewController alloc] init];
break;
case 3:
targetVC = (UIViewController *)[[PinchViewController alloc] init];
break;
case 4:
targetVC = (UIViewController *)[[SwipeViewController alloc] init];
break;
case 5:
targetVC = (UIViewController *)[[LongPressViewController alloc] init];
break;
default:
break;
}
if (targetVC)
{
[_tableVC.navigationController pushViewController:targetVC animated:YES];
}
}
#pragma mark - Other Method
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
TestView.m
#import "TestView.h"
@implementation TestView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"TestView touch began");
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"TestView touch ended");
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"TestView touch cancelled");
}
@end
TapViewController.m
#import "TapViewController.h"
@interface TapViewController ()
{
UIView *_testView;
}
@end
@implementation TapViewController
#pragma mark - init
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"Tap";
self.edgesForExtendedLayout = UIRectEdgeNone;
_testView = [[UIView alloc] init];
_testView.backgroundColor = [UIColor orangeColor];
//创建手势:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[_testView addGestureRecognizer:tapGesture];
[tapGesture addTarget:self action:@selector(tap2:)];
}
return self;
}
#pragma mark - lifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_testView.frame = CGRectMake(50, 50, 150, 150);
[self.view addSubview:_testView];
}
- (void)tap:(UITapGestureRecognizer *)tapGesture {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个警告信息" preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"已取消操作");
}]];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
NSLog(@"跳转到 alertVC");
}];
}
- (void)tap2:(UITapGestureRecognizer *)tapGesture {
NSLog(@"执行了tap2方法");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
PanViewController.m
#import "PanViewController.h"
@interface PanViewController ()
{
UIView *_testView;
}
@end
@implementation PanViewController
#pragma mark - init
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"Pan";
self.edgesForExtendedLayout = UIRectEdgeNone;
_testView = [[UIView alloc] init];
_testView.backgroundColor = [UIColor redColor];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_testView addGestureRecognizer:panGesture];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_testView.frame = CGRectMake(50, 50, 150, 150);
[self.view addSubview:_testView];
}
- (void)pan:(UIPanGestureRecognizer *)panGesture {
//获取当前手势在其父视图上的位移:
CGPoint translatePoint = [panGesture translationInView:self.view];
//产生了多少位移就进行多少偏移:
_testView.center = CGPointMake(_testView.center.x + translatePoint.x, _testView.center.y + translatePoint.y);
//每次都将偏移量在它的父视图上重置为0 , 否则它会偏移量每次会递增发生较大移动:
// [panGesture setTranslation: CGPointZero inView:self.view];
if (panGesture.state == UIGestureRecognizerStatePossible) {
NSLog(@"可能");
} else if (panGesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始");
} else if (panGesture.state == UIGestureRecognizerStateChanged) {
NSLog(@"改变");
} else if (panGesture.state == UIGestureRecognizerStateEnded) {
NSLog(@"结束");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
EdgePanViewController.m
#import "EdgePanViewController.h"
@interface EdgePanViewController ()
{
UIView *_testView;
}
@end
@implementation EdgePanViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"Edge Pan";
self.edgesForExtendedLayout = UIRectEdgeNone;
_testView = [[UIView alloc] init];
_testView.backgroundColor = [UIColor orangeColor];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_testView.frame = CGRectMake(50, 50, 150, 150);
[self.view addSubview:_testView];
//创建屏幕边缘滑动手势:
UIScreenEdgePanGestureRecognizer *edgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
//右侧边缘作用手势:
edgePanGesture.edges = UIRectEdgeRight;
[_testView addGestureRecognizer:edgePanGesture];
}
- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)edgePanGesture {
//获取当前手势在当前视图上的位移:
CGPoint translatePoint = [edgePanGesture translationInView:self.view];
_testView.center = CGPointMake(_testView.center.x - translatePoint.x, _testView.center.y);
[edgePanGesture setTranslation:CGPointZero inView:self.view];
// [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
PinchViewController.m
#import "PinchViewController.h"
@interface PinchViewController ()
{
UIView *_testView;
}
@end
@implementation PinchViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"Pinch";
self.edgesForExtendedLayout = UIRectEdgeNone;
_testView = [[UIView alloc] init];
_testView.backgroundColor = [UIColor orangeColor];
_testView.frame = CGRectMake(150, 200, 150, 150);
[self.view addSubview:_testView];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[_testView addGestureRecognizer:pinchGesture];
}
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture {
if (pinchGesture.state == UIGestureRecognizerStateChanged) {
_testView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
} else if (pinchGesture.state == UIGestureRecognizerStateEnded) {
_testView.transform = CGAffineTransformIdentity;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
SwipeViewController
#import "SwipeViewController.h"
@interface SwipeViewController ()
{
UIView *_testView;
}
@end
@implementation SwipeViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"Swipe 清扫手势";
self.edgesForExtendedLayout = UIRectEdgeNone;
_testView = [[UIView alloc] init];
_testView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_testView];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_testView.frame = CGRectMake(50, 50, 150, 150);
//创建清扫手势:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//清扫手势定义为上下方向:
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
[_testView addGestureRecognizer:swipeGesture];
}
- (void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:@"Swipe Gesture" preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消操作");
}]];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
NSLog(@"弹窗出现了~");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
LongPressViewController.m
#import "LongPressViewController.h"
@interface LongPressViewController ()
{
UIView *_testView;
}
@end
@implementation LongPressViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"Long Press";
self.edgesForExtendedLayout = UIRectEdgeNone;
_testView = [[UIView alloc] init];
_testView.backgroundColor = [UIColor orangeColor];
_testView.frame = CGRectMake(50, 50, 150, 150);
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
longPressGesture.minimumPressDuration = 2.0f;
[_testView addGestureRecognizer:longPressGesture];
[self.view addSubview:_testView];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)longPressGesture:(UILongPressGestureRecognizer *)longPressGesture {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"longPressGesture" preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消操作");
}]];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
NSLog(@"弹出警告");
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end