iphone开发:关于触屏事件的一些操作

iphone ipad开发: 关于触屏事件的一些操作

  1. //轻击:  
  2.   
  3. //需要在你的ViewController里重写几个方法:  
  4.   
  5. //开始触摸的方法  
  6.   
  7. - (void)touchesBegan:(NSSet *)touches   
  8.   
  9.            withEvent:(UIEvent *)event   
  10.   
  11. {  
  12.   
  13. messageLabel.text = @”Touches Began”;  
  14.   
  15. [self updateLabelsFromTouches:touches];  
  16.   
  17. }  
  18.   
  19. //触摸取消的方法  
  20.   
  21. - (void)touchesCancelled:(NSSet *)touches   
  22.   
  23.                withEvent:(UIEvent *)event  
  24.   
  25. {  
  26.   
  27. messageLabel.text = @”Touches Cancelled”;  
  28.   
  29. [self updateLabelsFromTouches:touches];  
  30.   
  31. }  
  32.   
  33. //触摸结束的方法  
  34.   
  35. - (void)touchesEnded:(NSSet *)touches   
  36.   
  37.            withEvent:(UIEvent *)event   
  38.   
  39. {  
  40.   
  41. messageLabel.text = @”Touches Stopped.”;  
  42.   
  43. [self updateLabelsFromTouches:touches];  
  44.   
  45. }  
  46.   
  47. //触摸移动的方法  
  48.   
  49. - (void)touchesMoved:(NSSet *)touches   
  50.   
  51.            withEvent:(UIEvent *)event   
  52.   
  53. {  
  54.   
  55. messageLabel.text = @”Drag Detected”;  
  56.   
  57. [self updateLabelsFromTouches:touches];  
  58.   
  59. }  
  60.   
  61. //触摸-清扫:  
  62.   
  63. //开始触摸  
  64.   
  65. - (void)touchesBegan:(NSSet *)touches   
  66.   
  67.            withEvent:(UIEvent *)event   
  68.   
  69. {  
  70.   
  71. UITouch *touch = [touches anyObject];  
  72.   
  73. gestureStartPoint = [touch locationInView:self.view];  
  74.   
  75. }  
  76.   
  77. //kMinimumGestureLength 最小移动长度 kMaximumVariance最大偏移长度  
  78.   
  79. - (void)touchesMoved:(NSSet *)touches   
  80.   
  81.            withEvent:(UIEvent *)event   
  82.   
  83. {  
  84.   
  85. UITouch *touch = [touches anyObject];  
  86.   
  87. CGPoint currentPosition = [touchlocationInView:self.view];  
  88.   
  89. CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);  
  90.   
  91. CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);  
  92.   
  93. if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance)   
  94.   
  95. {  
  96.   
  97. label.text = @”Horizontal swipe detected”;  
  98.   
  99. [self performSelector:@selector(eraseText)  
  100.   
  101.  withObject:nilafterDelay:2];  
  102.   
  103. }  
  104.   
  105. else if (deltaY >= kMinimumGestureLength &&  
  106.   
  107. deltaX <= kMaximumVariance)  
  108.   
  109. {  
  110.   
  111. label.text = @”Vertical swipe detected”;  
  112.   
  113. [selfperformSelector:@selector(eraseText)withObject:nilafterDelay:2];  
  114.   
  115. }  
  116.   
  117. }  
  118.   
  119. //多次轻击判断,比如双击,三击等:  
  120.   
  121. //单击动作响应事件  
  122.   
  123. - (void)singleTap   
  124.   
  125. {  
  126.   
  127. singleLabel.text = @”Single Tap Detected”;  
  128.   
  129. [selfperformSelector:@selector(eraseMe:)  
  130.   
  131.           withObject:singleLabel   
  132.   
  133.           afterDelay:1.6f];//1.6秒后执行eraseMe方法  
  134.   
  135. }  
  136.   
  137. //双击  
  138.   
  139. - (void)doubleTap  
  140.   
  141. {  
  142.   
  143. doubleLabel.text = @”Double Tap Detected”;  
  144.   
  145. [selfperformSelector:@selector(eraseMe:)  
  146.   
  147.           withObject:doubleLabel   
  148.   
  149.           afterDelay:1.6f];  
  150.   
  151. }  
  152.   
  153. //三击  
  154.   
  155. - (void)tripleTap   
  156.   
  157. {  
  158.   
  159. tripleLabel.text = @”Triple Tap Detected”;  
  160.   
  161. [selfperformSelector:@selector(eraseMe:)  
  162.   
  163.   withObject:tripleLabel   
  164.   
  165.   afterDelay:1.6f];  
  166.   
  167. }  
  168.   
  169. //四击  
  170.   
  171. - (void)quadrupleTap   
  172.   
  173. {  
  174.   
  175. quadrupleLabel.text = @”Quadruple Tap Detected”;  
  176.   
  177. [selfperformSelector:@selector(eraseMe:)  
  178.   
  179.   withObject:quadrupleLabel   
  180.   
  181.   afterDelay:1.6f];  
  182.   
  183. }  
  184.   
  185. - (void)eraseMe:(UITextField *)textField   
  186.   
  187. {  
  188.   
  189. textField.text = @"";  
  190.   
  191. }  
  192.   
  193. - (void)touchesBegan:(NSSet *)touches   
  194.   
  195.   withEvent:(UIEvent *)event   
  196.   
  197. {  
  198.   
  199. UITouch *touch = [touches anyObject];   //实例一个uitouch  
  200.   
  201. NSUInteger tapCount = [touch tapCount]; //计算touch的tapCount次数  
  202.   
  203. switch (tapCount)  
  204.   
  205. {  
  206.   
  207. case 1:  
  208.   
  209. [selfsingleTap];  
  210.   
  211. break;  
  212.   
  213. case 2:  
  214.   
  215. [selfdoubleTap];  
  216.   
  217. break;  
  218.   
  219. case 3:  
  220.   
  221. [selftripleTap];  
  222.   
  223. break;  
  224.   
  225. case 4:  
  226.   
  227. [selfquadrupleTap];  
  228.   
  229. break;  
  230.   
  231. default:  
  232.   
  233. break;  
  234.   
  235. }  
  236.   
  237. }  
  238.   
  239. //[self performSelector:@selector(eraseMe:)withObject:singleLabel afterDelay:1.6f]中  
  240.   
  241. //performSelector:@selector(eraseMe:)withObject:singleLabel afterDelay:1.6f方法为将来afterDelay1.6秒之后调用eraseMe方法  
  242.   
  243. //同样的[NSObect  cancelPreviousPerformSelector:withObject :afterDelay:];  
  244.   
  245. //方法为取消这些将来的调用  
  246.   
  247. //捏合操作:  
  248.   
  249. - (void)eraseLabel   
  250.   
  251. //清空lable  
  252.   
  253. label.text = @"";  
  254.   
  255. }  
  256.   
  257. //开始触碰  
  258.   
  259. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event   
  260.   
  261. {  
  262.   
  263. if([touches count] ==2)  
  264.   
  265. //检测是否为两个手指触点  
  266.   
  267. NSArray *twoTouches = [touchesallObjects];  
  268.   
  269. UITouch *first = [twoTouchesobjectAtIndex:0];  
  270.   
  271. UITouch *second = [twoTouchesobjectAtIndex:1];  
  272.   
  273. initialDistance = distanceBetweenPoints(  
  274.   
  275. [first locationInView:self.view], [secondlocationInView:self.view]);  
  276.   
  277. }  
  278.   
  279. }  
  280.   
  281. //移动手指  
  282.   
  283. - (void)touchesMoved:(NSSet *)touches   
  284.   
  285.   withEvent:(UIEvent *)event   
  286.   
  287. {  
  288.   
  289. if ([touches count] == 2)   
  290.   
  291. {  
  292.   
  293. NSArray *twoTouches = [touchesallObjects];  
  294.   
  295. UITouch *first = [twoTouchesobjectAtIndex:0];  
  296.   
  297. UITouch *second = [twoTouchesobjectAtIndex:1];  
  298.   
  299. CGFloat currentDistance =distanceBetweenPoints(  
  300.   
  301. [first locationInView:self.view],[secondlocationInView:self.view]);  
  302.   
  303. if (initialDistance ==0)  
  304.   
  305. {  
  306.   
  307. initialDistance = currentDistance;//根据移动前后的坐标距离差检测是捏合的手势还是打开的手势  
  308.   
  309. }  
  310.   
  311. else if (currentDistance - initialDistance > kMinimumPinchDelta)   
  312.   
  313. //检测是否大于最小移动值kMinimumPinchDelta  
  314.   
  315. label.text = @”Outward Pinch”;  
  316.   
  317. [selfperformSelector:@selector(eraseLabel)  
  318.   
  319.  withObject:nil  
  320.   
  321.   afterDelay:1.6f];  
  322.   
  323. }  
  324.   
  325. else if (initialDistance - currentDistance > kMinimumPinchDelta)   
  326.   
  327. {  
  328.   
  329. label.text = @”Inward Pinch”;  
  330.   
  331. [selfperformSelector:@selector(eraseLabel)  
  332.   
  333.  withObject:nil  
  334.   
  335.   afterDelay:1.6f];  
  336.   
  337. }  
  338.   
  339. }  
  340.   
  341. }  
  342.   
  343. //触碰结束  
  344.   
  345. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event   
  346.   
  347. {  
  348.   
  349. initialDistance = 0;  
  350.   
  351. }  
  352.   
  353. //自定义手势“√”:  
  354.   
  355. - (void)eraseLabel   
  356.   
  357. {  
  358.   
  359. label.text = @"";  
  360.   
  361. }  
  362.   
  363. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event   
  364.   
  365. {  
  366.   
  367. UITouch *touch = [touches anyObject];  
  368.   
  369. CGPoint point = [touch locationInView:self.view];  
  370.   
  371. lastPreviousPoint = point;  
  372.   
  373. lastCurrentPoint = point;  
  374.   
  375. lineLengthSoFar = 0.0f;  
  376.   
  377. }  
  378.   
  379. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event   
  380.   
  381. {  
  382.   
  383. UITouch *touch = [touches anyObject];  
  384.   
  385. CGPoint previousPoint = [touchpreviousLocationInView:self.view];  
  386.   
  387. CGPoint currentPoint = [touch locationInView:self.view];  
  388.   
  389. //计算两条线之间的角度  
  390.   
  391. CGFloat angle = angleBetweenLines(lastPreviousPoint,   
  392.   
  393.           lastCurrentPoint,previousPoint,currentPoint);  
  394.   
  395. //检测手势被承认的条件 kMinimumCheckMarkAngle最小角度  
  396.   
  397. //kMaximumCheckMarkAngle最大角度  
  398.   
  399. //kMinimumCheckMarkLength画线最小长度  
  400.   
  401. if (angle >= kMinimumCheckMarkAngle &&   
  402.   
  403. angle <= kMaximumCheckMarkAngle &&   
  404.   
  405. lineLengthSoFar > kMinimumCheckMarkLength)   
  406.   
  407. {  
  408.   
  409. label.text = @”Checkmark”;  
  410.   
  411. [selfperformSelector:@selector(eraseLabel)  
  412.   
  413.  withObject:nil   
  414.   
  415.   afterDelay:1.6];  
  416.   
  417. }  
  418.   
  419. //lineLengthSoFar,lastPreviousPoint,lastCurrentPoint重新赋值  
  420.   
  421. lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint);  
  422.   
  423. lastPreviousPoint = previousPoint;  
  424.   
  425. lastCurrentPoint = currentPoint;  
  426.   
  427. }  
  428.   
  429. //这里用到一个判断两条直线的角度的方法  
  430.   
  431. //angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPointlin2End);  
  432.   
  433. //给一个几何方法集的接口方法:  
  434.   
  435. //CGPointUtils.h 头文件  
  436.   
  437. #import <CoreGraphics/CoreGraphics.h>  
  438.   
  439. CGFloat distanceBetweenPoints (CGPoint first,CGPoint second);  
  440.   
  441. CGFloat angleBetweenPoints(CGPoint first, CGPoint second);  
  442.   
  443. CGFloat angleBetweenLines(CGPoint line1Start,CGPoint line1End, CGPoint line2Start,CGPoint lin2End);  
  444.   
  445. //.c文件 CGPointUtils.c  
  446.   
  447. #include ”CGPointUtils.h”  
  448.   
  449. #include <math.h>  
  450.   
  451. #define pi 3.14159265358979323846  
  452.   
  453. #define degreesToRadian(x) (pi * x / 180.0)  
  454.   
  455. #define radiansToDegrees(x) (180.0 * x / pi)  
  456.   
  457. CGFloat distanceBetweenPoints (CGPoint first,CGPoint second)   
  458.   
  459. {  
  460.   
  461. CGFloat deltaX = second.x - first.x;  
  462.   
  463. CGFloat deltaY = second.y - first.y;  
  464.   
  465. return sqrt(deltaX*deltaX + deltaY*deltaY );  
  466.   
  467. };  
  468.   
  469. CGFloat angleBetweenPoints(CGPoint first, CGPoint second)  
  470.   
  471. {  
  472.   
  473. CGFloat height = second.y - first.y;  
  474.   
  475. CGFloat width = first.x - second.x;  
  476.   
  477. CGFloat rads = atan(height/width);  
  478.   
  479. returnradiansToDegrees(rads);  
  480.   
  481. //degs = degrees(atan((top – bottom)/(right – left)))  
  482.   
  483. }  
  484.   
  485. CGFloat angleBetweenLines(CGPoint line1Start,CGPoint line1End, CGPoint line2Start,CGPoint line2End)   
  486.   
  487. {  
  488.   
  489. CGFloat a = line1End.x - line1Start.x;  
  490.   
  491. CGFloat b = line1End.y - line1Start.y;  
  492.   
  493. CGFloat c = line2End.x - line2Start.x;  
  494.   
  495. CGFloat d = line2End.y - line2Start.y;  
  496.   
  497. CGFloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));  
  498.   
  499. returnradiansToDegrees(rads);  
  500.   
  501. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值