关于触屏事件的一些操作

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