iOS事件机制,以及不同手势使用touchesBegan等表现形式

iOS事件机制,以及不同⼿势使⽤touchesBegan等表现形式事件处理⽅法
UIResponder中定义了⼀系列对事件的处理⽅法,他们分别是:
–(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
–(void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
–(void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
–(void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
从⽅法名字可以知道,他们分别对应了屏幕事件的开始、移动、结束和取消⼏个阶段,前三个阶段理解都没问题,最后⼀个取消事件的触发时机是在诸如突然来电话或是系统杀进程时调⽤。这些⽅法的第⼀个参数定义了UITouch对象的⼀个集合(NSSet),它的数量表⽰了这次事件是⼏个⼿指的操作,⽬前iOS设备⽀持的多点操作⼿指数最多是5。第⼆个参数是当前的UIEvent对象。下图展⽰了⼀个UIEvent对象与多个UITouch对象之间的关系。
⼀、点击事件
⾸先,新建⼀个⾃定义的View继承于UIView,并实现上述提到的事件处理⽅法,我们可以通过判断UITouch的tapCount属性来决定响应单击、双击或是多次点击事件。
成瘾POMyView.m
1
2
3
4
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34#import "MyView.h"
@implementation MyView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *aTouch in touches) {
if (aTouch.tapCount == 2) {
// 处理双击事件
[self respondToDoubleTapGesture];
}
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void)respondToDoubleTapGesture
{
NSLog(@"respondToDoubleTapGesture");
}
@end
⼆、滑动事件
中国科技馆新馆影院滑动事件⼀般包括上下滑动和左右滑动,判断是否是⼀次成功的滑动事件需要考虑⼀些问题,⽐如⼤部分情况下,⽤户进⾏⼀次滑动操作,这次滑动是否是在⼀条直线上?或者是否是基本能保持⼀条直线的滑动轨迹。或者判断是上下滑动还是左右滑动等。另外,滑动⼿势⼀般有⼀个起点和⼀个终点,
期间是在屏幕上画出的⼀个轨迹,所以需要对这两个点进⾏判断。我们修改上述的MyView.m的代码来实现⼀次左右滑动的事件响应操作。
MyView.m
1
2
3
4
5
6 7 8#import "MyView.h"
#define HORIZ_SWIPE_DRAG_MIN  12    //⽔平滑动最⼩间距#define VERT_SWIPE_DRAG_MAX    4    //垂直⽅向最⼤偏移量
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4
1 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56#define VERT_SWIPE_DRAG_MAX    4    //垂直⽅向最⼤偏移量
@implementation MyView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
// startTouchPosition是⼀个CGPoint类型的属性,⽤来存储当前touch事件的位置
self.startTouchPosition = [aTouch locationInView:self];
}
食品注册管理办法-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint currentTouchPosition = [aTouch locationInView:self];
//  判断⽔平滑动的距离是否达到了设置的最⼩距离,并且是否是在接近直线的路线上滑动(y轴偏移量)    if (fabsf(self.startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN &&
fabsf(self.startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX)
{
// 满⾜if条件则认为是⼀次成功的滑动事件,根据x坐标变化判断是左滑还是右滑
if (self.startTouchPosition.x < currentTouchPosition.x) {
[self rightSwipe];//右滑响应⽅法
} else {
[self leftSwipe];//左滑响应⽅法
}
//重置开始点坐标值
self.startTouchPosition = CGPointZero;
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//当事件因某些原因取消时,重置开始点坐标值
self.startTouchPosition = CGPointZero;
}
-(void)rightSwipe
{
NSLog(@"rightSwipe");
}
-(void)leftSwipe
{
NSLog(@"leftSwipe");
}
@end
三、拖拽事件
在屏幕上我们可以拖动某⼀个控件(View)进⾏移动,这种事件成为拖拽事件,其实现原理就是在不改变View的⼤⼩尺⼨的前提下改变View 的显⽰坐标值,为了达到动态移动的效果,我们可以在move阶段的⽅法中进⾏坐标值的动态更改,还是重写MyView.m的事件处理⽅法,这次在touchesMove⽅法中进⾏处理。
MyView.m
1
2
3
4
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25#import "MyView.h"
@implementation MyView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *aTouch = [touches anyObject];
//获取当前触摸操作的位置坐标
CGPoint loc = [aTouch locationInView:self];
//获取上⼀个触摸点的位置坐标
CGPoint prevloc = [aTouch previousLocationInView:self];
CGRect myFrame = self.frame;
//改变View的x、y坐标值
float deltaX = loc.x - prevloc.x;
float deltaY = loc.y - prevloc.y;
//重新设置View的显⽰位置
[self setFrame:myFrame];
}
硫酸银
25 26 27 28 29 30 31 32 33 34 35 36 37}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
@end
四、双指缩放
之前提到过UIEvent包含了⼀系列的UITouch对象构成⼀次事件,当设计多点触控操作时,可与对UIEvent对象内的UITouch对象进⾏处理,⽐如实现⼀个双指缩放的功能。
MyView.m
1
2
3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62#import "MyView.h"
@implementation MyView
{
BOOL pinchZoom;
CGFloat previousDistance;
CGFloat zoomFactor;
}
-(id)init
{
self = [super init];
if (self) {
pinchZoom = NO;
//缩放前两个触摸点间的距离
previousDistance = 0.0f;
zoomFactor = 1.0f;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(unt == 2) {
pinchZoom = YES;
NSArray *touches = [event.allTouches allObjects];
//接收两个⼿指的触摸操作
CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
武警学院CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
//计算出缩放前后两个⼿指间的距离绝对值(勾股定理)
previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
} else {
pinchZoom = NO;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(YES == pinchZoom && unt == 2) {
NSArray *touches = [event.allTouches allObjects];
局面的理解与判断CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
//两个⼿指移动过程中,两点之间距离
CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
//换算出缩放⽐例
zoomFactor += (distance - previousDistance) / previousDistance;
zoomFactor = fabs(zoomFactor);
previousDistance = distance;
//缩放
ansform = CATransform3DMakeScale(zoomFactor, zoomFactor, 1.0f);    }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(unt != 2) {
pinchZoom = NO;
previousDistance = 0.0f;
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
62 63 64 65 66 67 68 69{
}
@end
转⾃:wwwblogs/zhw511006/p/3517249.html
上⾯实现的⽅式有⼀点不⾜之处就是必须两个⼿指同时触摸按下才能达到缩放的效果,并不能达到相册⾥⾯那样⼀个⼿指触摸后,另⼀个⼿指按下也可以缩放。如果需要达到和相册照⽚缩放的效果,需要同时控制begin、move、end⼏个阶段的事件处理。这个不⾜就留给感兴趣的同学⾃⼰去实现了。

本文发布于:2024-09-23 10:31:04,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/124710.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:事件   滑动   对象   缩放   达到   操作
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议