首页 技术 正文
技术 2022年11月15日
0 收藏 876 点赞 2,489 浏览 5161 个字
Touch:在与设备的多点触摸屏交互时生成。响应者对象
响应者对象就是可以响应事件并对事件作出处理。在iOS中,存在UIResponder类,它定义了响应者对象的所有方法。UIApplication、UIView等类都继承了UIResponder类这些类的实例都可以当作响应者。 第一响应者
当前接受触摸的响应者对象被称为第一响应者,即表示当前该对象正在与用户交互,它是响应者链的开端。响应者链
事件被交由第一响应者对象处理,如果第一响应者不处理,事件被沿着响应者链向上传递,交给下一个响应者。一般来说,第一响应者是个视图或控件,并且首先对事件进行响应,如果第一响应者不处理该事件,事件就会被传递给它的视图控制器,如果此视图控制器不处理该事件,则将事件传递给父视图,如果父视图没有响应,则该事件转到父视图控制器。
,以此类推,直到顶层视图。接下来会沿着顶层视图到窗口(UIWindow对象)再到程序(UIApplication对象)。如果UIApplication不响应该事件,该事件逐渐进入睡眠状态。当发生触摸时,会触发下列方法:
touchesBegan:withEvent
touchesMoved:withEvent
touchesEnded:withEvent
touchesCancelled:withEvent 在给定的触摸阶段中,如果发生新的触摸动作或已有的触摸动作发生变化,应用程序就会发送如下消息:
当一个或多个手指触碰屏幕时,发送touchesBegan:withEvent:消息。
当一个或多个手指在屏幕上移动时,发送touchesMoved:withEvent:消息。
当一个或多个手指离开屏幕时,发送touchesEnded:withEvent:消息。
当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:withEvent:消息。以上方法都有两个参数。
第一个参数是一个UITouch对象的集合,表示给定阶段中 新的或者发生变化的触摸动作;第二个参数是一个UIEvent对象,表示这个特定的事件。为了处理给定阶段的事件,响应者对象常常从传入的集合参数中取得一或多个UITouch对象,然后考察这些对象的属性或取得它们的位置.
.如果需要处理所有触摸对象,可以向该NSSet对象发送anyObject消息。
.UITouch实例的tapCount属性还可以给出发生多少次触碰
.UITouch类中有一个名为locationInView:的重要方法,如果传入self参数值,它会给出触摸动作在响应者坐标系统中的位置检测双击手势
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == ) {
CGPoint tapPoint = [theTouch locationInView:self];
// Process a double-tap gesture
}
}响应者类并不是必须实现上面列出的事件方法。举例来说,如果它只对手指离开屏幕感兴趣,则只需要实现touchesEnded:withEvent:方法就可以了。UILabel *_messageLabel;
@property(nonatomic, retain) UILabel *messageLabel;@synthesize messageLabel = _messageLabel; //创建messageLabel
_messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
_messageLabel.backgroundColor = [UIColor clearColor];
_messageLabel.textAlignment = UITextAlignmentLeft;
[self.view addSubview:messageLabel];- (void)updateMessageLabelFromTouches:(NSSet *)touches touchStr:(NSString *)touchStr
{
int munTaps = [[touches anyobject] tapCount];
NSString *tapsMessage = [NSString stringWithFormat:@"%@,%d touches",touchStr,munTaps]
_messageLabel.text = tapsMessage;
}
// 手指触碰屏幕时
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSString *tmpStr = @"Touches Began";
[sele updateMessageLabelFromTouches:touches touchStr:tmpStr];}// 手指在屏幕上移动时
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
NSString *tmpStr = @"Touches Moved";
[sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
}// 手指离开屏幕时
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
NSString *tmpStr = @"Touches Ended";
[sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
}// 当触摸序列被系统事件所取消时
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSString *tmpStr = @"Touches Cancelled";
[sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
}.手势:是指从你用一个或多个手指接触屏幕时开始,直到你的手指离开屏幕为止所发生的所有事件。2.1.UITapGestureRecognizer敲击手势(单击和双击)
 2.2.UIPanGestureRecognizer(拖动手势)
 2.3.UIPinchGestureRecognizer(缩放手势)
 2.4.UISwipeGestureRecognizer(滑动手势)
 2.5.UIRotationGestureRecognizer(旋转手势)
 2.6.UILongPressGestureRecognizer(长按手势) • .UITapGestureRecognizer敲击手势
UIView *_view
_View = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
[_view setBackgroundColor:[UIColor redColor]];// 单击的 Recognizer
UITapGestureRecognizer *tapRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//点击的次数
tapRecognizer.numberOfTapsRequired = ; // 单击
tapRecognizer.numberOfTapsRequired = ; // 双击
//点击的手指数
singleRecognizer.numberOfTouchesRequired = ;//给view添加一个手势监测;
[tapView addGestureRecognizer:singleRecognizer];// 单击或双击后调用的方法
- (void) tap:(UITapGestureRecognizer *)sender
{
if (sender.numberOfTapsRequired == ) {
[_view setBackgroundColor:[UIColor greenColor]];
}else if(sender.numberOfTapsRequired == ){
//双击
[_view setBackgroundColor:[UIColor yellowColor]];
}
}.1UIPanGestureRecognizer(拖动手势)
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_view addGestureRecognizer: panRecognizer];-(void) pan:(UIPanGestureRecognizer *) pan
{
[_panView setBackgroundColor:[UIColor yellowColor]]; }.1UIPinchGestureRecognizer(缩放手势)
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
[_view addGestureRecognizer: *pinchRecognizer];- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
[_pinchView setBackgroundColor:[UIColor yellowColor]];
}4.1 UISwipeGestureRecognizer 滑动手势
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//设置滑动方向 Down/Left/Right/Up
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[_view addGestureRecognizer:swipeRecognizer];- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
[_swipeView setBackgroundColor:[UIColor yellowColor]];
}.1UIRotationGestureRecognizer(旋转手势)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]
[_view addGestureRecognizer:rotation];- (void) rotation:(UIRotationGestureRecognizer *)rotation
{
[_rotationView setBackgroundColor:[UIColor yellowColor]];
}6.1 UILongPressGestureRecognizer长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[_longPressView addGestureRecognizer:longPress];
longPress.minimumPressDuration = 3.0; // 按3秒相应
[_view addGestureRecognizer:longPress];- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
[_longPressView setBackgroundColor:[UIColor yellowColor]];
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903