ios 怎么获得触摸ios 屏幕坐标的坐标

iOS触摸事件(基础) - 简书
iOS触摸事件(基础)
对触摸事件进行一个基本整理吧,最基础的滑动事件
我们一般写demo最简单的方法就是写一个touchBegin方法,将事件写在里面,敲击屏幕做出对应事件就好了,但是具体有些什么东西,我们来探讨一下。
基本点击事件,按照iOS的尿性,有点击,肯定有移动,有点击结束等一系列方法
如下:可以多指,但是不建议
// 点击的时候调用
- (void)touchesBegan:(NSSet&UITouch *& *)touches withEvent:(nullable UIEvent *)
// 点击之后不松手,移动的时候调用
- (void)touchesMoved:(NSSet&UITouch *& *)touches withEvent:(nullable UIEvent *)
// 点击点离开屏幕的时候点用,点击结束
- (void)touchesEnded:(NSSet&UITouch *& *)touches withEvent:(nullable UIEvent *)
// 点击取消的时候调用:比如电话打入
- (void)touchesCancelled:(nullable NSSet&UITouch *& *)touches withEvent:(nullable UIEvent *)
iOS9以后又新出了一系列的关于3Dtouch的点击事件
- (void)pressesBegan:(NSSet&UIPress *& *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesChanged:(NSSet&UIPress *& *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesEnded:(NSSet&UIPress *& *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesCancelled:(NSSet&UIPress *& *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
iOS3增加的摇一摇方法,加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
最后这两个方法是用来接收远程事件的,比如播放器进入后台,点击外部组件,调用APP的事件方法
// 返回是否接收外部远程事件
- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender NS_AVAILABLE_IOS(3_0);
// 这个就是接收事件处理方法
- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);
简单的点击事件
我们今天仅仅对点击事件来进行个整理
iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称之为“响应者对象”
UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
四个触摸事件处理方法中,都有NSSet *touches和UIEvent *event两个参数。
如果两根手指同时触摸一个view,那么view只会调用touchesBegan:withEvent:方法一次,touches参数里有两个UITouch对象
如果两根手指一前一后分开触摸同一个view,那么view分别调用两次
touchesBegan:withEvent:方法,并且每次调用时的touches参数只包含一个UITouch对象
根据touches的UITouch的个数可以判断是单点触摸还是多点触摸
当用户用一根手指触摸屏幕,会创建一个与手指相关联的UITouch对象
一根手指对应一个UITouch对象
& UITouch的作用
保存着跟手指相关的信息,比如触摸的对象,时间,阶段
当手指移动时,系统会更新同一个UITouch对象,使之能够一直保持该手指在的触摸位置
当手指离开屏幕的时候,系统会销毁这个UITouch对象
*** 提示:在iPhone开发中,要避免使用双击时间!***
& UITouch的属性
触摸产生时所处的窗口
@property(nonatomic,readonly,retain) UIWindow *
触摸产生时所处的视图
@property(nonatomic,readonly,retain) UIView *
短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly) NSUInteger tapC```
记录了触摸事件产生或变化时的时间,单位是s
@property(nonatomic,readonly) NSTimeI```
当前触摸事件所处的状态
@property(nonatomic,readonly) UITouchP
& UITouch的方法
* 返回值表示触摸在view上的位置
* 这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))
* 调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
- (CGPoint)locationInView:(UIView *)
// 该方法记录了前一个触摸点的位置
- (CGPoint)previousLocationInView:(UIView *)
每产生一个事件,就会产生一个UIEvent对象
UIEvent:称为事件对象,记录产生的时刻和类型
#######& 常见属性
@property(nonatomic,readonly) UIEventT
@property(nonatomic,readonly) UIEventS
事件产生的时间
@property (nonatomic, readonly) NSTimeI
我们做两个Demo测试一下
第一个Demo
要求:在控制器的View上有一个小的view,这个view随着我们的拖动而移动
// 这个要求实现起来很简单,只需要在touchesMoved:方法里进行操作即可
- (void)touchesMoved:(NSSet&UITouch *& *)touches withEvent:(UIEvent *)event
// 因为是NSSet对象,所以我们没办法根据下标取出来内部组件
// 打印一下看看内部
NSLog(@"%zd",touches.count);
UITouch *touch = [touches anyObject];
// locationInView:方法取出当前点坐标
CGPoint curP = [touch locationInView:self];
// previousLocationInView当前点的前一个点
CGPoint preP = [touch previousLocationInView:self];
// precise前缀用于更精确的描点
NSLog(@"preciseLocationInView%@",NSStringFromCGPoint([touch preciseLocationInView:self]));
// 获取x轴偏移量
CGFloat offsetX = curP.x - preP.x;
// 获取y轴偏移量
CGFloat offsetY = curP.y - preP.y;
// 改变view的位置 (frame,center,transform)
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
NSLog(@"%s",__func__);
上面这段代码只要想说明的就是如何拿到这些点击的点,如果去做一些效果的事件
我们来做一个小玩意儿来玩一下
第二个Demo
要求:画板,点击顶部颜色,可以在view上画线,点击撤销,取消最近一次画的线,如下图所示:
思路:把所以的触发点放到数组里,然后描绘出来
直接上代码了
.h文件里提供外界方法名
-(id)initWithFrame:(CGRect)frame
button1Color:(UIColor *)button1Color
button2Color:(UIColor *)button2Color
button3Color:(UIColor *)button3Color
button4Color:(UIColor *)button4Color
drawPaperColor:(UIColor *)paperC
.m文件内部实现
#import "DrawBoard.h"
@interface DrawBoard ()
// 四个颜色按钮
UIButton *_button1;
UIButton *_button2;
UIButton *_button3;
UIButton *_button4;
UIButton *_repealB
UIColor *_
// 存放所有东西的总数组
NSMutableArray *_ziA
@implementation DrawBoard
- (id)initWithFrame:(CGRect)frame button1Color:(UIColor *)button1Color button2Color:(UIColor *)button2Color button3Color:(UIColor *)button3Color button4Color:(UIColor *)button4Color drawPaperColor:(UIColor *)paperColor
if ([super initWithFrame:frame]) {
self.backgroundColor = paperC
float width = [UIScreen mainScreen].bounds.size.
_button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width*0.25, 50)];
_button1.backgroundColor=button1C
[self addSubview:_button1];
//设置点击事件
[_button1 addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
_button2 = [[UIButton alloc] initWithFrame:CGRectMake(width*0.25, 0, width*0.25, 50)];
_button2.backgroundColor=button2C
[self addSubview:_button2];
[_button2 addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
_button3 = [[UIButton alloc]initWithFrame:CGRectMake(width*0.5, 0, width*0.25, 50)];
_button3.backgroundColor=button3C
[self addSubview:_button3];
[_button3 addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
_button4 = [[UIButton alloc]initWithFrame:CGRectMake(width*0.75, 0, width*0.25, 50)];
_button4.backgroundColor=button4C
[self addSubview:_button4];
[_button4 addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
_repealButton = [[UIButton alloc]initWithFrame:CGRectMake(100, [UIScreen mainScreen].bounds.size.height -100, width-200, 50)];
_repealButton.backgroundColor=[UIColor grayColor];
[_repealButton setTitle:@"撤销" forState:UIControlStateNormal];
[self addSubview:_repealButton];
[_repealButton addTarget:self action:@selector(repeal:) forControlEvents:UIControlEventTouchDown];
_ziArray = [[NSMutableArray alloc]init];
_color = [UIColor blackColor];
- (void)changeColor:(UIButton *)btn
_color = btn.backgroundC
- (void)touchesBegan:(NSSet&UITouch *& *)touches withEvent:(UIEvent *)event
// 创建数组,每个数组放每一笔的所有点
NSMutableArray *array = [[NSMutableArray alloc] init];
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSValue *pointValue = [NSValue valueWithCGPoint:point];
[array addObject:pointValue];
// 这一个数组放一个笔画,然后每个笔画放到一个总数组里
[_ziArray addObject:array];
- (void)touchesMoved:(NSSet&UITouch *& *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSValue *pointValue = [NSValue valueWithCGPoint:point];
// 取出放点的这个数组
NSMutableArray *traitArray = [_ziArray lastObject];
[traitArray addObject:pointValue];
[self setNeedsDisplay];
// 开始画图
- (void)drawRect:(CGRect)rect
for (int i = 0; i & [_ziArray count]; i++) {
// NSMutableArray *traitArray=[[NSMutableArray alloc] init];错误
NSMutableArray *traitArray = [_ziArray objectAtIndex:i];
// 拿起画笔的方法
// 这个方法只有在drawRect:方法里才能调用
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置画笔的粗细。。
CGContextSetLineWidth(context,5.0f );
// 设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [[traitArray objectAtIndex:0] CGColor]);
for (int j = 1; j & [traitArray count]-1; j++) {
NSValue *pointValue = [traitArray objectAtIndex:j];
CGPoint firstPoint = [pointValue CGPointValue ];
NSValue *secondValue = [traitArray objectAtIndex:j+1];
CGPoint secondPoint = [secondValue CGPointValue];
// 设置两点的连线起点,
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
// 用点连成线,
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
// 提交画笔
CGContextStrokePath(context);
- (void)repeal:(UIButton *)btn
// 移除最后面的数组
[_ziArray removeLastObject];
[self setNeedsDisplay];
本文来自:http://ios.jobbole.com/84081/ 前言: 按照时间顺序,事件的生命周期是这样的: 事件的产生和传递(事件如何从父控件传递到子控件并寻找到最合适的view、寻找最合适的view的底层实现、拦截事件的处理)-&找到最合适的view后事件的处理...
响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象” UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并...
在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:Lotheve链接:http://www.jianshu.com/p/c294d1bd963d來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请...
iOS中的事件 响应者对象 - 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象” - UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是...
本文介绍了iOS中使用频率较高的触摸事件,并阐述了事件产生和传递的过程,以及响应者链的事件传递过程 触摸事件 简介 在用户使用app过程中,会产生各种各样的事件 iOS中的事件可以分为3大类型触摸事件加速计事件远程控制事件 响应者对象 在iOS中不是任何对象都能处理事件,只...
我愿意 在夜晚 与月亮眉目传情 却不太想与人说话 我愿意 在清晨 听小鸟在枝头歌唱 却不太想与人说话 我愿意 在林中 和大树交换心事 却不太想与人说话 月亮凝视得我通体透明 小鸟唱得我心情愉悦 大树收纳我所有的秘密 我就是不太想与人说话
我从来都不知道能拥有一双鞋子是多么困难的事情,因为我从来都没缺过,也没有关注过,所以无法体会。看到电影里哥哥和妹妹对又旧又破的鞋子都如此珍惜和哥哥为了帮妹妹能有一双鞋子而坚持不懈地去努力奋斗。兄妹俩的孝顺、懂事、勤劳、坚强和善良让我百感交集:因为生活的艰辛,物质的匮...
今天这篇短文详细给大家分享下有关软文营销的话题,悟透并熟练运用,将会协助我们轻松获得海量客户,也许你会恍然大悟,原来网络营销如此简单。 我们以“优源洗发水”为案例来分享: 现在,把自己想象成一个想要购买产品的意向客户,对优源无硅油这款产品很看好(可在百度指数中查看热度)想要...
太冷,太冷 恨那雨不偏不倚 落在我怀中 太冷,太冷 气那风忽轻忽重 吹破我喉咙 湿了怀怎么拥你在怀中 破了音如何吟秋月与春风 太冷
太冷 万一你走了 万一我哭了 太冷 太冷 ,,,,,,,, ,,,,,,,,,
人生所胃的“恶”就是原始冲动、欲望。文明是什么?文明其实就是克制,克制自己原始的冲动……衣食足而后知礼仪,优越的家庭环境,高素质的社会环境,才能培养出文明的现代人……,打扮得漂亮,穿戴名牌,说着恶毒的话,动着恶毒的心机,这是低劣素质的表现。 接近什么样的人,就会走什么样的路...ios 怎么获得触摸屏幕的坐标_百度知道
ios 怎么获得触摸屏幕的坐标
我有更好的答案
过Touch的相应函数来获得:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInV
&#47.y 就是触点的坐标:self];/touchPoint.x ,touchPoint
资深电脑人
为您推荐:
其他类似问题
ios的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。iOS中的触摸事件的总结_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
iOS中的触摸事件的总结
&&ios 触摸事件总结
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩4页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢主题 : UIScrollview里如何获取触点坐标?(触点在subview里的坐标)
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 2(时)
发自: Web Page
UIScrollview里如何获取触点坐标?(触点在subview里的坐标)&&&
插入了一个有滚动条的Scrollview图片,当触点在scrollview里面时,如何获取触点点击的坐标?(指触点相对于整幅滚动图片的坐标,而不是相对于屏幕的坐标)
&
&
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];
&
currentTouch.x &和 currentTouch.y &可以获得触点在屏幕上的坐标。但不知道如何获得在插入了的scrollview里的坐标,并且不是以屏幕左上角为原点,也不是以UIScrollview控件左上角为原点,而是以插入scrollview的图片的左上角为原点。
&
在下初学。。。求助
级别: 新手上路
可可豆: 4 CB
威望: 4 点
在线时间: 22(时)
发自: Web Page
ScrollView的& contentOffset 属性可以获取到当前scrollView中内容的偏移。用偏移再加上在view中的touch坐标可以获得你要的值。
级别: 侠客
UID: 69637
可可豆: 861 CB
威望: 811 点
在线时间: 319(时)
发自: Web Page
scrollview有个contentOffset属性 &把屏幕坐标+&contentOffset&=你要的位置
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 2(时)
发自: Web Page
还有就是,在toucheBegan里写了currentTouch=[touch locationInView:self.view];currentTouchX.text=[[NSString alloc]initWithFormat:@"%f",currentTouch.x];似乎只能获得触点在屏幕内、scrollview之外的坐标,触点在scrollview之内时就获取不到坐标了,这是怎么回事?
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 2(时)
发自: Web Page
还想请问一下,为什么插入了scrollview后,触点在scrollview内时就获取不到它在整个屏幕内的坐标了?
级别: 新手上路
可可豆: 12 CB
威望: 12 点
在线时间: 2(时)
发自: Web Page
明白了,应该设置setInteractionEnable:NO;再就是之前不用contentOffset也可以,改为: currentTouch = [touch locationInView:self.scrollPicture];即可scrollPicture为scrollview的变量名谢谢楼下的解答!
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
viewcontroller 新建一个scrollview
鼠标点击scrollview
获取鼠标点击scrollview上的坐标,坐标是相对scrollview的坐标
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
MyScrollView.h@interface MyScrollView : UIScrollView
@endMyScrollView.m@implementation MyScrollView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyO
CGPoint touchLocation = [touch locationInView:self.window];
NSLog(@&%@&,NSStringFromCGPoint(touchLocation));
}MyViewController.m#import MyScrollView.h
- (void)viewDidLoad {
MyScrollView *v = [[MyScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)];
v.backgroundColor = [UIColor grayColor];
[self.view addSubview:v];
}然后看输出: 17:53:11.513 Demo[3991:c07] {105, 143}
17:53:12.627 Demo[3991:c07] {136, 310}
17:53:14.519 Demo[3991:c07] {277, 313}
17:53:16.718 Demo[3991:c07] {292, 312}
17:53:17.344 Demo[3991:c07] {217, 198}
17:53:17.776 Demo[3991:c07] {152, 155}
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
这是什么平台?ios吗?
ios的话,被点击view重写touchesBegan:withEvent:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
if (touches.count == 1)//单指操作
UITouch *touch = touches.anyO
CGPoint touchLocation = [touch locationInView:self.window];
NSLog(@&%@&,NSStringFromCGPoint(touchLocation));
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。}

我要回帖

更多关于 ios 触摸屏幕关闭键盘 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信