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(@"取消触摸");
}
来源:oschina
链接:https://my.oschina.net/u/4311028/blog/4216723