IOS设备滑动事件

时间:2023-12-29 20:47:50

只要手指触摸屏幕,滑动,从屏幕离开,系统都会产生UIEvent对象类型的事件---当然包括UITouch事件 – touchesBegan:withEvent:   当用户触摸到屏幕时调用方法 – touchesMoved:withEvent:  当用户触摸到屏幕并移动时调用此方法 – touchesEnded:withEvent:  当触摸离开屏幕时调用此方法

– touchesCancelled:withEvent:  当触摸被取消时调用此方法

例子如下:

  1. //滑动开始事件
  2. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  3. {
  4. UITouch *touch = [touches anyObject];
  5. CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点
  6. self.startPoint  = pointone;
  7. }
  8. //滑动移动事件
  9. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  10. {
  11. UITouch *touch = [touches anyObject];
  12. //imgViewTop是滑动后最后接触的View
  13. CGPoint pointtwo = [touch locationInView:imgViewTop];  //获得滑动后最后接触屏幕的点
  14. if(fabs(pointtwo.x-startPoint.x)>100)
  15. {  //判断两点间的距离
  16. bMove = YES;
  17. }
  18. }
  19. //滑动结束处理事件
  20. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  21. {
  22. UITouch *touch = [touches anyObject];
  23. CGPoint pointtwo = [touch locationInView:self.view];  //获得滑动后最后接触屏幕的点
  24. if((fabs(pointtwo.x-startPoint.x)>50)&&(bMove))
  25. {
  26. //判断点的位置关系 左滑动
  27. if(pointtwo.x-startPoint.x>0)
  28. {   //左滑动业务处理
  29. if (clicks>0) {
  30. clicks--;
  31. if ([wyKPI.sortZbs count]>1) {
  32. [btnRight setEnabled:YES];
  33. if(clicks==0)
  34. {
  35. [btnLeft setEnabled:NO];
  36. [btnRight setEnabled:YES];
  37. }
  38. [labTitle setText:[wyKPI.sortZbs objectAtIndex:(NSUInteger)clicks]];
  39. [labTitle setFont:[UIFont systemFontOfSize:14.0f]];
  40. [tabWyKPI reloadData];
  41. }
  42. }
  43. }
  44. //判断点的位置关系 右滑动
  45. else
  46. {  //右滑动业务处理
  47. if (clicks<[wyKPI.sortZbs count]-1) {
  48. clicks++;
  49. if ([wyKPI.sortZbs count]>1) {
  50. [btnLeft setEnabled:YES];
  51. if(clicks==[wyKPI.sortZbs count]-1)
  52. {
  53. [btnLeft setEnabled:YES];
  54. [btnRight setEnabled:NO];
  55. }
  56. [labTitle setText:[wyKPI.sortZbs  objectAtIndex:clicks]];
  57. [labTitle setFont:[UIFont systemFontOfSize:14.0f]];
  58. [tabWyKPI reloadData];
  59. }
  60. }
  61. }
  62. }
  63. }
//滑动开始事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点
self.startPoint = pointone;
}
//滑动移动事件
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//imgViewTop是滑动后最后接触的View
CGPoint pointtwo = [touch locationInView:imgViewTop]; //获得滑动后最后接触屏幕的点 if(fabs(pointtwo.x-startPoint.x)>100)
{ //判断两点间的距离
bMove = YES;
}
}
//滑动结束处理事件
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pointtwo = [touch locationInView:self.view]; //获得滑动后最后接触屏幕的点
if((fabs(pointtwo.x-startPoint.x)>50)&&(bMove))
{
//判断点的位置关系 左滑动
if(pointtwo.x-startPoint.x>0)
{ //左滑动业务处理
if (clicks>0) {
clicks--;
if ([wyKPI.sortZbs count]>1) {
[btnRight setEnabled:YES];
if(clicks==0)
{
[btnLeft setEnabled:NO];
[btnRight setEnabled:YES];
}
[labTitle setText:[wyKPI.sortZbs objectAtIndex:(NSUInteger)clicks]];
[labTitle setFont:[UIFont systemFontOfSize:14.0f]];
[tabWyKPI reloadData];
}
}
}
//判断点的位置关系 右滑动
else
{ //右滑动业务处理
if (clicks<[wyKPI.sortZbs count]-1) {
clicks++;
if ([wyKPI.sortZbs count]>1) {
[btnLeft setEnabled:YES];
if(clicks==[wyKPI.sortZbs count]-1)
{
[btnLeft setEnabled:YES];
[btnRight setEnabled:NO];
}
[labTitle setText:[wyKPI.sortZbs objectAtIndex:clicks]];
[labTitle setFont:[UIFont systemFontOfSize:14.0f]];
[tabWyKPI reloadData];
}
}
}
}
}