欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS触摸手势——UITouch

程序员文章站 2022-04-28 13:09:45
uitouch,主要是重写四个方法(触摸开始、触摸移动、触摸结束、触摸退出)以实现触摸响应方法 1、触摸开始- (void)touchesbegan:(nsset *)touches witheve...

uitouch,主要是重写四个方法(触摸开始、触摸移动、触摸结束、触摸退出)以实现触摸响应方法

1、触摸开始- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { }

2、触摸移动- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { }

3、触摸结束- (void)touchesended:(nsset *)touches withevent:(uievent *)event { }

4、触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)- (void)touchescancelled:(nsset *)touches withevent:(uievent *)event { }

使用注意事项:

1、触摸开始方法中,一般实现的属性有:

1-1、获取触摸对象,如:

uitouch*touch=touches.anyobject;

1-2、触摸次数,以便判断实现对应方法,如:

nsintegerclicknumber=touch.tapcount;

nslog(@"clicknumber=%@",@(clicknumber));

1-3、触摸位置是否用户想要处理的子视图,如:

cgpointcurrentpoint=[touchlocationinview:self.view];

if(cgrectcontainspoint(self.label.frame,currentpoint))

{

//改变标题

self.label.text=@"触摸位置在当前label";

}

2、触摸移动方法中,一般实现的属性有:

2-1、获取触摸对象,如:

uitouch *touch = touches.anyobject;

2-2、获取当前点击位置,以及上一个点击位置,如:

cgpointcurrentpoint=[touchlocationinview:self.view];

cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

注:可通过触摸移动改变子视图的位置,如:

floatmovex=currentpoint.x-previouspoint.x;

floatmovey=currentpoint.y-previouspoint.y;

cgfloatcenterx=self.label.center.x+movex;

cgfloatcentery=self.label.center.y+movey;

self.label.center=cgpointmake(centerx,centery);

//触摸开始

-(void)touchesbegan:(nsset*)toucheswithevent:(uievent*)event

{

//self.view.backgroundcolor=[uicolorcolorwithred:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

nslog(@"触摸手势开始");

//获取触摸对象

uitouch*touch=touches.anyobject;

//获取触摸次数

nsintegerclicknumber=touch.tapcount;

nslog(@"clicknumber=%@",@(clicknumber));

if(2==clicknumber)

{

self.label.backgroundcolor=[uicolorredcolor];

}

elseif(3==clicknumber)

{

self.label.backgroundcolor=[uicolorgreencolor];

}

//获取触摸状态

uitouchphasestate=touch.phase;

nslog(@"state=%ld",state);

//获取响应视图对象

uiview*view=touch.view;

nslog(@"view=%@",view);

//获取当前触摸位置

cgpointcurrentpoint=[touchlocationinview:self.view];

nslog(@"touch.locationinview={%2.3f,%2.3f}",currentpoint.x,currentpoint.y);

//获取当前触摸的前一个位置

cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

nslog(@"touch.previouslocationinview={%2.3f,%2.3f}",previouspoint.x,previouspoint.y);

//获取触摸区域的子视图

//方法1

//for(uiview*oneviewinself.view.subviews)

//{

//if([oneviewiskindofclass:[uilabelclass]])

//{

////是否选中图标(当前坐标是否包含所选图标)

//if(cgrectcontainspoint(oneview.frame,currentpoint))

//{

////获取当前被选择图标

//uilabel*selectedview=(uilabel*)oneview;

////改变标题

//selectedview.text=@"触摸位置在当前label";

//}

//}

//}

//方法2

if(cgrectcontainspoint(self.label.frame,currentpoint))

{

//改变标题

self.label.text=@"触摸位置在当前label";

}

}

//触摸移动

-(void)touchesmoved:(nsset*)toucheswithevent:(uievent*)event

{

//self.view.backgroundcolor=[uicolorcolorwithred:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

nslog(@"触摸手势移动");

//移动ui视图控件

//方法1获得触摸点的集合,可以判断多点触摸事件

for(uitouch*touchinevent.alltouches)

{

//获取当前触摸坐标

cgpointcurrentpoint=[touchlocationinview:self.view];

nslog(@"touch.locationinview={%2.3f,%2.3f}",currentpoint.x,currentpoint.y);

//获取当前触摸前一个坐标

cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

nslog(@"touch.previouslocationinview={%2.3f,%2.3f}",previouspoint.x,previouspoint.y);

//坐标偏移量(效果异常??)

floatmovex=currentpoint.x-previouspoint.x;

floatmovey=currentpoint.y-previouspoint.y;

cgfloatcenterx=self.label.center.x+movex;

cgfloatcentery=self.label.center.y+movey;

self.label.center=cgpointmake(centerx,centery);

}

//方法2

//uitouch*touch=touches.anyobject;

////获取当前触摸坐标

//cgpointcurrentpoint=[touchlocationinview:self.view];

//nslog(@"touch.locationinview={%2.3f,%2.3f}",currentpoint.x,currentpoint.y);

////获取当前触摸前一个坐标

//cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

//nslog(@"touch.previouslocationinview={%2.3f,%2.3f}",previouspoint.x,previouspoint.y);

////坐标偏移量

//floatmovex=currentpoint.x-previouspoint.x;

//floatmovey=currentpoint.y-previouspoint.y;

//cgfloatcenterx=self.label.center.x+movex;

//cgfloatcentery=self.label.center.y+movey;

//self.label.center=cgpointmake(centerx,centery);

}

//触摸结束

-(void)touchesended:(nsset*)toucheswithevent:(uievent*)event

{

//self.view.backgroundcolor=[uicolorwhitecolor];

nslog(@"触摸手势结束");

self.label.text=@"touch触摸手势";

}

//触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)

-(void)touchescancelled:(nsset*)toucheswithevent:(uievent*)event

{

nslog(@"触摸手势退出");

}

效果图

 

iOS触摸手势——UITouch
iOS触摸手势——UITouch
iOS触摸手势——UITouchitouch,主要是重写四个方法(触摸开始、触摸移动、触摸结束、触摸退出)以实现触摸响应方法

1、触摸开始- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { }

2、触摸移动- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { }

3、触摸结束- (void)touchesended:(nsset *)touches withevent:(uievent *)event { }

4、触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)- (void)touchescancelled:(nsset *)touches withevent:(uievent *)event { }

使用注意事项:

1、触摸开始方法中,一般实现的属性有:

1-1、获取触摸对象,如:

uitouch*touch=touches.anyobject;

1-2、触摸次数,以便判断实现对应方法,如:

nsintegerclicknumber=touch.tapcount;

nslog(@"clicknumber=%@",@(clicknumber));

1-3、触摸位置是否用户想要处理的子视图,如:

cgpointcurrentpoint=[touchlocationinview:self.view];

if(cgrectcontainspoint(self.label.frame,currentpoint))

{

//改变标题

self.label.text=@"触摸位置在当前label";

}

2、触摸移动方法中,一般实现的属性有:

2-1、获取触摸对象,如:

uitouch *touch = touches.anyobject;

2-2、获取当前点击位置,以及上一个点击位置,如:

cgpointcurrentpoint=[touchlocationinview:self.view];

cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

注:可通过触摸移动改变子视图的位置,如:

floatmovex=currentpoint.x-previouspoint.x;

floatmovey=currentpoint.y-previouspoint.y;

cgfloatcenterx=self.label.center.x+movex;

cgfloatcentery=self.label.center.y+movey;

self.label.center=cgpointmake(centerx,centery);

//触摸开始

-(void)touchesbegan:(nsset*)toucheswithevent:(uievent*)event

{

//self.view.backgroundcolor=[uicolorcolorwithred:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

nslog(@"触摸手势开始");

//获取触摸对象

uitouch*touch=touches.anyobject;

//获取触摸次数

nsintegerclicknumber=touch.tapcount;

nslog(@"clicknumber=%@",@(clicknumber));

if(2==clicknumber)

{

self.label.backgroundcolor=[uicolorredcolor];

}

elseif(3==clicknumber)

{

self.label.backgroundcolor=[uicolorgreencolor];

}

//获取触摸状态

uitouchphasestate=touch.phase;

nslog(@"state=%ld",state);

//获取响应视图对象

uiview*view=touch.view;

nslog(@"view=%@",view);

//获取当前触摸位置

cgpointcurrentpoint=[touchlocationinview:self.view];

nslog(@"touch.locationinview={%2.3f,%2.3f}",currentpoint.x,currentpoint.y);

//获取当前触摸的前一个位置

cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

nslog(@"touch.previouslocationinview={%2.3f,%2.3f}",previouspoint.x,previouspoint.y);

//获取触摸区域的子视图

//方法1

//for(uiview*oneviewinself.view.subviews)

//{

//if([oneviewiskindofclass:[uilabelclass]])

//{

////是否选中图标(当前坐标是否包含所选图标)

//if(cgrectcontainspoint(oneview.frame,currentpoint))

//{

////获取当前被选择图标

//uilabel*selectedview=(uilabel*)oneview;

////改变标题

//selectedview.text=@"触摸位置在当前label";

//}

//}

//}

//方法2

if(cgrectcontainspoint(self.label.frame,currentpoint))

{

//改变标题

self.label.text=@"触摸位置在当前label";

}

}

//触摸移动

-(void)touchesmoved:(nsset*)toucheswithevent:(uievent*)event

{

//self.view.backgroundcolor=[uicolorcolorwithred:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1.0];

nslog(@"触摸手势移动");

//移动ui视图控件

//方法1获得触摸点的集合,可以判断多点触摸事件

for(uitouch*touchinevent.alltouches)

{

//获取当前触摸坐标

cgpointcurrentpoint=[touchlocationinview:self.view];

nslog(@"touch.locationinview={%2.3f,%2.3f}",currentpoint.x,currentpoint.y);

//获取当前触摸前一个坐标

cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

nslog(@"touch.previouslocationinview={%2.3f,%2.3f}",previouspoint.x,previouspoint.y);

//坐标偏移量(效果异常??)

floatmovex=currentpoint.x-previouspoint.x;

floatmovey=currentpoint.y-previouspoint.y;

cgfloatcenterx=self.label.center.x+movex;

cgfloatcentery=self.label.center.y+movey;

self.label.center=cgpointmake(centerx,centery);

}

//方法2

//uitouch*touch=touches.anyobject;

////获取当前触摸坐标

//cgpointcurrentpoint=[touchlocationinview:self.view];

//nslog(@"touch.locationinview={%2.3f,%2.3f}",currentpoint.x,currentpoint.y);

////获取当前触摸前一个坐标

//cgpointpreviouspoint=[touchpreviouslocationinview:self.view];

//nslog(@"touch.previouslocationinview={%2.3f,%2.3f}",previouspoint.x,previouspoint.y);

////坐标偏移量

//floatmovex=currentpoint.x-previouspoint.x;

//floatmovey=currentpoint.y-previouspoint.y;

//cgfloatcenterx=self.label.center.x+movex;

//cgfloatcentery=self.label.center.y+movey;

//self.label.center=cgpointmake(centerx,centery);

}

//触摸结束

-(void)touchesended:(nsset*)toucheswithevent:(uievent*)event

{

//self.view.backgroundcolor=[uicolorwhitecolor];

nslog(@"触摸手势结束");

self.label.text=@"touch触摸手势";

}

//触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)

-(void)touchescancelled:(nsset*)toucheswithevent:(uievent*)event

{

nslog(@"触摸手势退出");

}

效果图

 

iOS触摸手势——UITouch
iOS触摸手势——UITouch
iOS触摸手势——UITouch