[IOS]触摸事件和手势

简介:

如何使用IOS中的触摸事件和手势,这也是增加我们IOS应用的一个重要的一个功能?下面我来用一个简单的Demo来入门一下吧!

实现的功能具备右滑动和双击操作:


双击切换图片:


友情提醒:要实现两点滑动,按住alt键和shift键试试

操作步骤:

1.创建一个SingleView的项目,在页面上添加一个子view和几个label;

2.ViewController.h:

#import <UIKit/UIKit.h>  @interface DXWViewController : UIViewController @property (retain, nonatomic) IBOutlet UILabel *state; @property (retain, nonatomic) IBOutlet UILabel *tapCount; @property (retain, nonatomic) IBOutlet UILabel *xPoint; @property (retain, nonatomic) IBOutlet UILabel *yPoint; @property (retain, nonatomic) IBOutlet UILabel *touchNum; @property (retain, nonatomic) IBOutlet UIView *otherView; @property(retain,nonatomic)UIImageView *imgView; @property(retain,nonatomic)UIImage *img; @end

3.ViewController.m:

#import "DXWViewController.h"  @interface DXWViewController ()  @end  @implementation DXWViewController  //加载图片 -(void)loadimag:(NSString *)imgName {     self.img = [UIImage imageNamed:imgName];     self.imgView = [[UIImageView alloc] initWithImage:self.img];     CGRect rec = CGRectMake(5, 5, 150, 117);     self.imgView.frame = rec;     [self.otherView addSubview:self.imgView];          [self.imgView release]; }  - (void)viewDidLoad {     [super viewDidLoad];     self.img = [UIImage imageNamed:@"flower1.jpg"]; 	//花开     [self loadimag:@"flower1.jpg"];      }  -(void)fun:(NSSet *)touches {     UITouch *touch = [touches anyObject];     int tapCount = [touch tapCount];     CGPoint point = [touch locationInView:self.view];          self.tapCount.text = [NSString stringWithFormat:@"tapCount %d",tapCount];     int touchNum = [touches count];     self.xPoint.text = [NSString stringWithFormat:@"x %.1f",point.x];     self.yPoint.text = [NSString stringWithFormat:@"y %.1f",point.y];     //接受多点,要设置允许多点     self.touchNum.text = [NSString stringWithFormat:@"touchNum %d",touchNum];  } //返回点击次数 -(int)getNumber:(NSSet *)touches {     UITouch *touch = [touches anyObject];     int tapCount = [touch tapCount];     CGPoint point = [touch locationInView:self.view];          self.tapCount.text = [NSString stringWithFormat:@"tapCount %d",tapCount];     int touchNum = [touches count];     self.xPoint.text = [NSString stringWithFormat:@"x %.1f",point.x];     self.yPoint.text = [NSString stringWithFormat:@"y %.1f",point.y];     //接受多点,要设置允许多点     self.touchNum.text = [NSString stringWithFormat:@"touchNum %d",touchNum];     return tapCount;      } BOOL flag = true;  float x=0;  //支持多点 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {          //如果有子view的话如何获取子view的触摸事件     //touches = [event touchesForView:self.otherView];     self.state.text = @"touchesBegin";     [self fun:touches];     [self fun:[event touchesForView:self.otherView]];               if ([self getNumber:[event touchesForView:self.otherView]] == 2) {         if (flag) {                          [self loadimag:@"flower.jpg"];             flag = false;         }         else         {             [self loadimag:@"flower1.jpg"];             flag = true;         }     }               UITouch *touch = [touches anyObject];     CGPoint point = [touch locationInView:self.otherView];     x = point.x;          } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {      } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {      } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {     //自己自定义手势     UITouch *touch = [touches anyObject];     CGPoint point = [touch locationInView:self.otherView];     float moveX = 0;     moveX = point.x;     if ((moveX-x)>100) {         NSLog(@"右滑");     } }  - (void)dealloc {     [_state release];     [_tapCount release];     [_xPoint release];     [_yPoint release];     [_touchNum release];     [_otherView release];     [super dealloc]; } @end 

4.要注意,要把页面设置一下,设置能多点识别的属性。

5.也可以通过拖放一个手势控件来实现手势,在页面中有一个Swipe Gesture Recognizer的手势控件

- (void)viewDidLoad {     [super viewDidLoad];     //代码创建手势     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];     swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;          //添加上手势,控件添加     [self.view addGestureRecognizer:swipe]; }  -(void)gesture:(id)sender {     NSLog(@"left or right"); }  - (IBAction)swipe:(id)sender {     NSLog(@"right"); }

6.实现单击/双击触发事件        

-(void)viewLoad { //点击事件     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun1)];     //单点触摸     tap.numberOfTouchesRequired = 1;     //点击几次,如果是1就是单击     tap.numberOfTapsRequired = 1;     [self.view addGestureRecognizer:tap]; }  //一次点击事件 -(void)fun1 {     NSLog(@"click1"); } 

==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

 

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17587497

欢迎关注我的微博:http://weibo.com/u/2590571922

















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366413 ,如需转载请自行联系原作者
相关文章
|
iOS开发
(六)IOS手势和触摸的用法
(六)IOS手势和触摸的用法
189 0
|
程序员 API iOS开发
iOS UIView添加快捷手势回调
iOS UIView添加快捷手势回调
|
iOS开发
IOS触摸事件
IOS触摸事件
64 0
|
iOS开发
如何使用iOS手势UIGestureRecognizer
如何使用iOS手势UIGestureRecognizer
74 0
|
iOS开发
iOS之UIPickerView滚动事件
在开发中,我们会用到pickerview滚动条,那如何监听到pickview的滚动事件呢,之前开发就遇到过一个问题,快速滑动秒选确定按钮,地址显示不对的问题,解决办法为在点选确定辅助按钮的时候判断当时的pickerView是否正在滚动,如果在滚动则不允许触发点选确定后的其他操作。
315 0
iOS之UIPickerView滚动事件
|
iOS开发
(七) IOS 响应者链和手势
(七) IOS 响应者链和手势
315 0
|
iOS开发 开发者
玩转iOS转场动画(二)
玩转iOS转场动画
204 0
|
iOS开发 UED 开发者
玩转iOS转场动画(一)
玩转iOS转场动画
431 0
玩转iOS转场动画(一)