UITouch触摸事件

UITouch触摸事件

  • 主要为三个方法
1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3.    UITouch *touch = [touches anyObject];4.    CGPoint point = [touch locationInView:self];5.    start = point;6.    end = point;7.8.}9.10.-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{11.12.    UITouch *touch = [touches anyObject];13.    CGPoint point = [touch locationInView:self];14.    end = point;15.16.    [self setNeedsDisplay];17.}18.19.-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{20.21.    UITouch *touch = [touches anyObject];22.    CGPoint point = [touch locationInView:self];23.    end = point;24.25.    [self setNeedsDisplay];26.27.28.}29.

  • 示例
1.#import "myView.h"2.3.@interface myView ()4.{5.    CGPoint start;6.    CGPoint end;7.}8.@end9.10.11.@implementation myView12.13.-(instancetype)initWithFrame:(CGRect)frame{14.    if (self = [super initWithFrame:frame]) {15.        self.backgroundColor = [UIColor whiteColor];16.    }17.    return self;18.}19.20.21.22.-(void)drawRect:(CGRect)rect{23.24.    if (start.x != end.x && start.y != end.y) {25.26.        CGFloat w = fabs(end.x - start.x);27.        CGFloat h = fabs(end.y - start.y);28.29.        CGFloat x = end.x < start.x ? end.x : start.x;30.        CGFloat y = end.y < start.y ? end.y : start.y;31.32.        //画圆33.        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:34.                              CGRectMake(x, y, w, h)];35.36.        //画方37.//        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];38.39.        [[UIColor redColor] setStroke];40.        [path stroke];41.    }42.43.}44.45.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{46.47.    UITouch *touch = [touches anyObject];48.    CGPoint point = [touch locationInView:self];49.    start = point;50.    end = point;51.52.}53.54.-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{55.56.    UITouch *touch = [touches anyObject];57.    CGPoint point = [touch locationInView:self];58.    end = point;59.60.    [self setNeedsDisplay];61.}62.63.-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{64.65.    UITouch *touch = [touches anyObject];66.    CGPoint point = [touch locationInView:self];67.    end = point;68.69.    [self setNeedsDisplay];70.71.72.}73.74.75.76.77.@end78.

时间: 2024-08-06 19:44:40

UITouch触摸事件的相关文章

iOS:触摸控件UITouch、事件类UIEvent

UITouch:触摸控件类   UIEvent:事件类 ??????UITouch的介绍?????? 一.触摸状态类型枚举 typedef NS_ENUM(NSInteger, UITouchPhase) { UITouchPhaseBegan,             // 开始触摸 UITouchPhaseMoved,             // 触摸移动 UITouchPhaseStationary,       // 触摸没有移动 UITouchPhaseEnded,        

触摸事件UITouch的应用

因为UIView或者UIViewController都是继承与UIResponder ,所以都有UITouch这个事件.当用户点击屏幕的时候,会产生触摸事件. 通过UITouch事件,可以监听到开始触摸.触摸移动过程.触摸结束以及触摸打断四个不同阶段的状态,在这些方法中,我们能够获取到很多有用的信息,比如触摸点的坐标.触摸的手指数.触摸的次数等等,下面通过一个小例子来说明一下. 详细代码如下: /* 定义属性 */ @interface ViewController () { CGPoint _

IOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 二.UIResponder UIResponder内部提供了以下方法来处理事件触摸事件- (v

IOS开发—事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 二.UIResponder UIResponder内部提供了以下方法来处理事件触摸事件- (v

UITouch/UIResponder:iOS上触摸事件的视图检测和事件传递

iPhone上有非常流畅的用户触摸交互体验,能检测各种手势:点击,滑动,放大缩小,旋转.大多数情况都是用UI*GestureRecognizer这样的手势对象来关联手势事件和手势处理函数.也有时候,会看到第三方代码里会在如下函数中进行处理: -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event: 那么问题就来了,手势和touch到底有什么区别和联系?这一切还得从头iOS触摸事件检测,以及UIResponder(响应者)开始说起

touches,motion触摸事件响应

//触摸事件响应需要重写方法 1 // 触摸时触发该方法(消息发送) 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 3 UITouch *touch = [touches anyObject]; 4 CGPoint point = [touch locationInView:self.rootView.touchView];//locationInView:得到当前点击下在指定视图中

iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事件(手势操作).运动事件.远程控制事件等展开学习: iOS事件简介 触摸事件 手势识别 运动事件 远程控制事件 iOS事件 在iOS中事件分为三类: 触摸事件:通过触摸.手势进行触发(例如手指点击.缩放) 运动事件:通过加速器进行触发(例如手机晃动) 远程控制事件:通过其他远程设备触发(例如耳机控制

iOS边练边学--触摸事件以及能够拖拽的UIView的练习

一.用户在使用APP的过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: 二.响应者对象 在iOS中只有继承了了UIResponder的对象才能接受并处理事件,这样的对象称之为“响应者对象” UIApplication.UIViewController.UIView都继承自UIResponder,因此他们都是响应者对象,都能够接受并处理事件 UIResponder内部提供了以下方法来处理事件 三.练习中对UIView的触摸事件进行了熟悉 四.UITouch 一根手指对应一个UITou

2.0 触摸事件

UIView不接收触摸事件的三种情况: 1.不接收用户交互 userInteractionEnabled = NO 2.隐藏 hidden = YES 3.透明 alpha = 0.0 ~ 0.01 4. 如果子视图的位置超出了父视图的有效范围, 那么子视图也是无法与用户交互的, 即使设置了父视图的 clipsToBounds = NO, 可以看懂, 但是也是无法与用户交互的 提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它