iOS开发从入门到精通--UIGesture手势扩展,平移,滑动,长按
2016-07-28 08:51
411 查看
UIGesture手势扩展,平移,滑动,长按
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImageView * iView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"17_8.png"]]; iView.frame=CGRectMake(50, 50, 200, 300); iView.userInteractionEnabled=YES; //创建一个平移手势 //p1:事件函数处理对象 //p2:事件函数 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAct:)]; //将手势添加到图像视图中 [iView addGestureRecognizer:pan]; //将移动事件手势从图像视图中取消 [iView removeGestureRecognizer:pan]; [self.view addSubview:iView]; //创建一个滑动手势 UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAct:)]; //设定滑动手势接收事件的类型 //UISwipeGestureRecognizerDirectionUp 上 //UISwipeGestureRecognizerDirectionDown 下 //UISwipeGestureRecognizerDirectionLeft 左 //UISwipeGestureRecognizerDirectionRight 右 swipe.direction =UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; [iView addGestureRecognizer:swipe]; //创建长按手势 UILongPressGestureRecognizer * longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAct:)]; [iView addGestureRecognizer:longPress]; //设置长按手势的时间,默认是0.5秒的时间为长按手势 longPress.minimumPressDuration=3; } //长按函数 -(void)longPressAct:(UILongPressGestureRecognizer*)longpress{ //手势的状态对象,到达规定时间,3秒钟,触发函数 if(longpress.state==UIGestureRecognizerStateBegan){ NSLog(@"状态开始"); } //当手指离开屏幕时,结束状态 else if(longpress.state==UIGestureRecognizerStateEnded){ NSLog(@"结束状态"); } } //滑动函数 -(void)swipeAct:(UISwipeGestureRecognizer*)swipe{ NSLog(@"swipe滑动"); } //移动事件函数,只要手指坐标在屏幕上发生变化时,函数就会被调用 -(void)panAct:(UIPanGestureRecognizer*)pan{ //获取移动的坐标,现对于视图的坐标系 //参数:相对的视图对象 // CGPoint pt = [pan translationInView:self.view]; // NSLog(@"pt.x=%.2f,pt.y=%.2f",pt.x,pt.y); //获取移动时的相对速度 CGPoint pv = [pan velocityInView:self.view]; NSLog(@"pv.x=%.2f,pv.y=%.2f",pv.x,pv.y); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- Java中ProcessBuilder使用
- what does “static int function(…) __acquires(..) __releases(…){” mean?
- [转]Quick Tip: Error: 18456, Severity: 14, State: 38.
- POJ-2533-Longest Ordered Subsequence
- Java中String、StringBuffer和StringBuilder
- 86.View the Exhibit to examine the output for the CROSSCHECK BACKUP command. Which statement is true
- vue.js表单数据双向绑定
- ProgressBar(进度条)
- HDU 3415 Max Sum of Max-K-sub-sequence(长度不超过k的最大连续子序列和,单调队列)
- android开发仿ios的UIScrollView
- Android_UI:ViewPager
- [leetcode]115. Distinct Subsequences
- MalformedByteSequenceException: Invalid byte 1 of 1-byte
- POJ 2127 Greatest Common Increasing Subsequence
- Database ORAchk and EXAchk User’s Guide
- iOS剪切板 UIPasteboard
- UITextView不能滑动及显示不全问题(iOS7)
- poj 2533 Longest Ordered Subsequence
- Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompa
- webpack-dev-server+vue构建开发环境(一)