手势操作(单击手势,长按手势,策划手势)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手势操作(单击手势,长按手势,策划手势)相关的知识,希望对你有一定的参考价值。
1.策划手势操作
// // ViewController.m // 1-28策划手势 // // Created by ma c on 16/1/28. // Copyright © 2016年 bjsxt. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) UISwipeGestureRecognizer *recognizer; @property (strong, nonatomic) UIButton *btn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 667)]; [self.btn setBackgroundColor:[UIColor redColor]]; [self.view addSubview:self.btn]; //一个手势只能对应一个,默认为右滑,左滑必须在创建一个 self.recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)]; [self.recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [self.view addGestureRecognizer:self.recognizer]; } - (void)handleSwipeFrom { //如果往右滑 if (self.recognizer.direction==UISwipeGestureRecognizerDirectionRight) { [UIView beginAnimations:@"1" context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationRepeatAutoreverses:NO]; self.btn.frame = CGRectMake(80, 0, 80, 667); [UIView commitAnimations]; } }
2.单击手势和长按手势操作
#import "ViewController.h" @interface ViewController () @property (strong,nonatomic) UITextField *field; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //手势:滑动手势,点击手势,双击手势,长按手势, self.field=[[UITextField alloc] initWithFrame:CGRectMake(80, 211, 114, 30)]; [email protected]"请输入内容"; //注意没设置代理 [self.view addSubview:self.field]; //创建单击手势UITapGestureRecognizer UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wantdosomething)]; //我们创建了一个手势,但是并没有吧手势放在具体的哪个控件上,也就是说:手势必须依托与控件,当具体某个控件上面放了个手势,这个手势才能响应 //给当前视图添加单击手势,(把单击手势放在view上)那么用户在当前视图上单击的时候,就会执行下面的手势关联方法 [self.view addGestureRecognizer:tap]; //创建长按手势 UILongPressGestureRecognizer *longpress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpresswant)]; longpress.minimumPressDuration=2.0;//最小时间为2s以上 [self.view addGestureRecognizer:longpress]; } //这个方法会在手势被触发的时候被调用. - (void)wantdosomething { NSLog(@"点击啦"); [self.field resignFirstResponder]; } - (void)longpresswant { UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"触发动作" message:@"长按手势" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alert show]; }
@end
以上是关于手势操作(单击手势,长按手势,策划手势)的主要内容,如果未能解决你的问题,请参考以下文章
Android使用GestureDetector进行手势检测
Android使用GestureDetector进行手势检测