iOS:手势与矩形、点运算相关(18-01-24更)
1、矩形、点运算
1、获取当前的View在Window的frame
2、包含判断
3、获取点击在响应者 touchesBegan 的位置
4、UIScrollView、UITableView 实时 位置 相关
2、手势
1、点击(UITapGestureRecognizer)
2、拖移(UIPanGestureRecognizer)、轻扫(UISwipeGestureRecognizer)
3、长按(UILongPressGestureRecognizer)
4、旋转(UIRotationGestureRecognizer)
5、捏合(UIPinchGestureRecognizer)
3、触摸
0、写在前面
可通过 NSStringFromCGPoint 等方法,用"%@"调试输出,不用再 point.x、point.y 列出来。rect 方法同。
1、矩形、点运算
1、获取当前的View在Window的frame
UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
CGRect rect=[_myButton convertRect:_myButton.bounds toView:window];
后续补充:
toView:nil 。参数置空,相当于默认window。
相关的(layer 也有类似):
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
toView,当前的点,是调用者的坐标系,想切换到view的坐标系。
fromView,当前的点,是fromView的坐标系,想切换到调用者的坐标系。
2、包含判断
// 矩形 包含 该矩形
CGRectContainsRect
// 矩形 包含 该点?
CGRectContainsPoint
3、获取点击在响应者 touchesBegan 的位置
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(point));
}
4、UIScrollView、UITableView 实时 位置 相关
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// scrollView 的偏移量
scrollView.contentOffset
// 当前手指在 windows 的位置
[scrollView.panGestureRecognizer locationInView:nil];
// 相对 手指按下时 的位移
[scrollView.panGestureRecognizer translationInView:nil];
// 移动的加速度
[scrollView.panGestureRecognizer velocityInView:nil];
}
2、手势
1、点击(UITapGestureRecognizer)
//单击
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.view addGestureRecognizer:tapGesture]; //双击
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
doubleTapGesture.numberOfTapsRequired = 2; //设置次数
[self.view addGestureRecognizer:doubleTapGesture]; //双击优先
[tapGesture requireGestureRecognizerToFail:doubleTapGesture]; -(void)doubleTapAction:(UITapGestureRecognizer*)gesture
{
NSLog(@"双击了");
} -(void)tapAction:(UITapGestureRecognizer*)gesture
{
NSLog(@"单击了");
}
2、拖移(UIPanGestureRecognizer)、轻扫(UISwipeGestureRecognizer)
//拖移
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:panGesture]; //轻扫
UISwipeGestureRecognizer *swipGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAciton:)];
swipGesture.direction = UISwipeGestureRecognizerDirectionLeft; //设置方向
[self.view addGestureRecognizer:swipGesture]; //轻扫优先
[panGesture requireGestureRecognizerToFail:swipGesture]; -(void)swipAciton:(UISwipeGestureRecognizer*)gesture
{
NSLog(@"轻扫");
} -(void)panAction:(UIPanGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
后续补充:轻扫想要识别向左还是向右,需要建2个轻扫的手势!
如果只建1个,设(左 | 右)方向,那么响应的函数,会显示(左 | 右)触发,无法辨别!
3、长按(UILongPressGestureRecognizer)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 2.0; //最短时间
[self.view addGestureRecognizer:longPress]; -(void)longPressAction:(UILongPressGestureRecognizer*)longAction
{
if (longAction.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if(longAction.state == UIGestureRecognizerStateChanged)
{
NSLog(@"长按位置改变");
return; }else if(longAction.state == UIGestureRecognizerStateEnded)
{
NSLog(@"长按结束");
return;
}
}
4、旋转(UIRotationGestureRecognizer)
UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
[self.view addGestureRecognizer:rotateGesture]; -(void)rotateAction:(UIRotationGestureRecognizer*)gesture
{
NSLog(@"%f",gesture.rotation*(180/M_PI));
}
5、捏合(UIPinchGestureRecognizer)
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinchGesture]; CGFloat _lastValue; -(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
if (_lastValue > pinch.scale) {
_imageView.transform = CGAffineTransformScale(_imageView.transform, 0.8, 0.8);
}else{
_imageView.transform = CGAffineTransformScale(_imageView.transform, 1.2, 1.2);
}
_lastValue = pinch.scale;
}
3、触摸
//触摸开始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"开始触摸"); [self.view endEditing:YES]; UITouch *touch = [touches anyObject];
if (touch.tapCount == 1)
{
[self performSelector:@selector(singleClick) withObject:nil afterDelay:0.3];
}
else if(touch.tapCount == 2)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleClick) object:nil];
NSLog(@"双击");
} //locationInView 在视图上的定位
CGPoint point = [touch locationInView:self];
NSLog(@"%@",NSStringFromCGPoint(point));
} -(void)singleClick
{
NSLog(@"单击");
} -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸结束");
} -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸移动");
} -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//电话呼入
NSLog(@"取消触摸");
}
iOS:手势与矩形、点运算相关(18-01-24更)的更多相关文章
- ios手势
iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...
- iOS手势学习UIGestureRecognizer & cocos2d 手势推荐
iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer // 长按UIPanGestur ...
- Wed Jul 04 18:01:38 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended
Wed Jul 04 18:01:38 CST 2018 WARN: Establishing SSL connection without server's identity verificatio ...
- iOS手势处理
iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...
- iOS 手势识别器概述
手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...
- swift 实现iOS手势密码、指纹密码、faceID
本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...
- 嵌入式:UCOSIII的使用(17.01.24补充)
0.一些移植.系统相关 OS_CFG_APP.H /* --------------------- MISCELLANEOUS ------------------ */ #define OS_CFG ...
- iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- 【IOS开发笔记03-视图相关】简单计算器的实现
UIView 经过前几天的快速学习,我们初步了解的IOS开发的一些知识,中间因为拉的太急,忽略了很多基础知识点,这些知识点单独拿出来学习太过枯燥,我们在今后的项目中再逐步补齐,今天我们来学习APP视图 ...
随机推荐
- 须知的css——margin不重叠的情形
margin重叠 摘自css2.1规范中文版 CSS中,两个或者多个盒(可能但不一定是兄弟)的相邻的margin会被结合成一个margin.Margin按这种方式结合叫重叠(collapse),产生的 ...
- Java设计模式—备忘录模式
个人感觉备忘录模式是一个比较难的设计模式,备忘录模式就是一个对象的备份模式,提供了一种程序数据的备份方法. 定义如下:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以 ...
- VS2010项目转换成VS2008
声明:本篇文章不是本人原创,但是网站的地址没有记下来,所以不能贴出来.但此方法本人亲自验证有效. 一.将.sln文件中的 Microsoft Visual Studio Solution File, ...
- Java基础之StringBuffer和StringBuilder的区别
StringBuffer是一个字符串的缓存类,属于一个容器,对于容器,我们可以进行增删改查. StringBuffer的容器长度是可变的,并且里面可以存放多种的数据类型.它跟其他容器,比如数组,是很不 ...
- 【Python系列】Python包管理器pip
缘起 这段时间忙着给朋友搞事,忙了好长一段时间,木有写博客很长时间了.之间做了两个东西,一个是邮件自动发送脚本,一个是数据处理软件.其中,在做数据处理软件的时候使用到了非Python系统库,是两个第三 ...
- 用jsp实现网站登录界面的制作,并连接数据库
课堂测试 任务需求: 撰写一篇博客 需要网站系统开发需要掌握的技术: 本次课堂测试的源程序代码: 运行结果截图: 说明课堂测试未按时完成的原因. 列出你对这门课的希望和自己的目标,并具体列出你计划每周 ...
- 《Python指南》学习笔记 一
更新时间:2018-06-14 <Python指南>原文在这里.本篇笔记主要是划重点. Python 3.6.3 1.简单入门 1.1 编码 默认情况下,Python 源文件是 UTF-8 ...
- Android 手机 黑域
黒域地址下载: http://pan.baidu.com/s/1bDYerc 连接手机,选择USB使用方式为“用作MIDI设备“ 0. (手机) 打开黑域,阅读向导1. (手机) 打开黑域,按屏幕提示 ...
- December 12th 2016 Week 51st Monday
Nothing is impossible for a willing heart. 心之所愿,无所不成. I wish I can be a strong, clever, powerful and ...
- January 05 2017 Week 1st Thursday
The true nobility is in being superior to your previous self. 真正的高贵在于超越过去的自己. I will be satisfied if ...