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更)的更多相关文章

  1. ios手势

    iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小   1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...

  2. iOS手势学习UIGestureRecognizer & cocos2d 手势推荐

    iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer  // 长按UIPanGestur ...

  3. 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 ...

  4. iOS手势处理

    iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...

  5. iOS 手势识别器概述

    手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...

  6. swift 实现iOS手势密码、指纹密码、faceID

    本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...

  7. 嵌入式:UCOSIII的使用(17.01.24补充)

    0.一些移植.系统相关 OS_CFG_APP.H /* --------------------- MISCELLANEOUS ------------------ */ #define OS_CFG ...

  8. iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  9. 【IOS开发笔记03-视图相关】简单计算器的实现

    UIView 经过前几天的快速学习,我们初步了解的IOS开发的一些知识,中间因为拉的太急,忽略了很多基础知识点,这些知识点单独拿出来学习太过枯燥,我们在今后的项目中再逐步补齐,今天我们来学习APP视图 ...

随机推荐

  1. 微服务架构之spring cloud feign

    在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...

  2. CSS技巧教程:margin在IE中的表现

    margin的位移方向是指margin数值为正值时候的情形,如果是负值则位移方向相反. 如上图所示:黄色子元素盒的margin-top,margin-left为负值时,如-10px,则黄色子元素盒向上 ...

  3. eclipse设置模板及格式

    1)     首先要有code_templates.xml 及 code_formatter.xml 两个文件,下面有代码,直接拷贝出来. code_formatter.xml: <?xml v ...

  4. java笔记--String类对象解析与运用

    --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877236.html "谢谢-- 1.String中的equals和==的 ...

  5. Angular5中提取公共组件之checkbox list

    因为工作原因,需要使用到checkbox list多选项功能. 一直在尝试在checkbox组件中添加NgModel的属性,但是只能在单个checkbox复选框上使用,checkbox list就没办 ...

  6. React学习笔记(五)State&声明周期

    React学习笔记(五) 四.State&声明周期 可以为组件添加"状态(state)".状态与属性相似,但是状态是私有的,完全受控于当前组件. 局部状态就是只能用于类(定 ...

  7. 深入浅出SharePoint2013——常用术语

    CAS(Code Access Security)自定义代码访问安全性 Sandboxed solution 沙箱解决方案

  8. 深入浅出SharePoint2010——请假系统无代码篇之工作流设计

    使用SharePoint Designer 2010进行设计. 主要使用的Actions如下图所示. 关于权限,考虑到严谨的权限设计,所以所有参与人员均为Read权限. 关于请假天数的计算,请假天数的 ...

  9. December 26th 2016 Week 53rd Monday

    Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. If the world is so cruel, I wo ...

  10. Foj 2299 Prefix(AC自动机、DP)

    Foj 2299 Prefix 题意 给定串s.正整数n,问有多少长度为n的字符串t满足:s[0...i]是t的子串,s[0...i+1]不是. 题解 求有多少长度为n的字符串t满足:s[0...i] ...