TouchImageView_Private.m.svn-base
上传用户:huachun008
上传日期:2009-10-03
资源大小:1927k
文件大小:4k
源码类别:

MacOS编程

开发平台:

Objective-C

  1. #import "TouchImageView_Private.h"
  2. @implementation UITouch (TouchSorting)
  3. - (NSComparisonResult)compareAddress:(id)obj
  4. {
  5.     if ((void *)self < (void *)obj) {
  6.         return NSOrderedAscending;
  7.     } else if ((void *)self == (void *)obj) {
  8.         return NSOrderedSame;
  9.     } else {
  10.         return NSOrderedDescending;
  11.     }
  12. }
  13. @end
  14. @implementation TouchImageView (Private)
  15. - (CGAffineTransform)incrementalTransformWithTouches:(NSSet *)touches
  16. {
  17.     NSArray *sortedTouches = [[touches allObjects] sortedArrayUsingSelector:@selector(compareAddress:)];
  18.     NSInteger numTouches = [sortedTouches count];
  19.     
  20. // No touches
  21. if (numTouches == 0) {
  22.         return CGAffineTransformIdentity;
  23.     }
  24. // Single touch
  25. if (numTouches == 1) {
  26.         UITouch *touch = [sortedTouches objectAtIndex:0];
  27.         CGPoint beginPoint = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
  28.         CGPoint currentPoint = [touch locationInView:self.superview];
  29. return CGAffineTransformMakeTranslation(currentPoint.x - beginPoint.x, currentPoint.y - beginPoint.y);
  30. }
  31. // If two or more touches, go with the first two (sorted by address)
  32. UITouch *touch1 = [sortedTouches objectAtIndex:0];
  33. UITouch *touch2 = [sortedTouches objectAtIndex:1];
  34.     CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);
  35.     CGPoint currentPoint1 = [touch1 locationInView:self.superview];
  36.     CGPoint beginPoint2 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch2);
  37.     CGPoint currentPoint2 = [touch2 locationInView:self.superview];
  38. double layerX = self.center.x;
  39. double layerY = self.center.y;
  40. double x1 = beginPoint1.x - layerX;
  41. double y1 = beginPoint1.y - layerY;
  42. double x2 = beginPoint2.x - layerX;
  43. double y2 = beginPoint2.y - layerY;
  44. double x3 = currentPoint1.x - layerX;
  45. double y3 = currentPoint1.y - layerY;
  46. double x4 = currentPoint2.x - layerX;
  47. double y4 = currentPoint2.y - layerY;
  48. // Solve the system:
  49. //   [a b t1, -b a t2, 0 0 1] * [x1, y1, 1] = [x3, y3, 1]
  50. //   [a b t1, -b a t2, 0 0 1] * [x2, y2, 1] = [x4, y4, 1]
  51. double D = (y1-y2)*(y1-y2) + (x1-x2)*(x1-x2);
  52. if (D < 0.1) {
  53.         return CGAffineTransformMakeTranslation(x3-x1, y3-y1);
  54.     }
  55. double a = (y1-y2)*(y3-y4) + (x1-x2)*(x3-x4);
  56. double b = (y1-y2)*(x3-x4) - (x1-x2)*(y3-y4);
  57. double tx = (y1*x2 - x1*y2)*(y4-y3) - (x1*x2 + y1*y2)*(x3+x4) + x3*(y2*y2 + x2*x2) + x4*(y1*y1 + x1*x1);
  58. double ty = (x1*x2 + y1*y2)*(-y4-y3) + (y1*x2 - x1*y2)*(x3-x4) + y3*(y2*y2 + x2*x2) + y4*(y1*y1 + x1*x1);
  59.     return CGAffineTransformMake(a/D, -b/D, b/D, a/D, tx/D, ty/D);
  60. }
  61. - (void)updateOriginalTransformForTouches:(NSSet *)touches
  62. {
  63.     if ([touches count] > 0) {
  64.         CGAffineTransform incrementalTransform = [self incrementalTransformWithTouches:touches];
  65.         self.transform = CGAffineTransformConcat(originalTransform, incrementalTransform);
  66.         originalTransform = self.transform;
  67.     }
  68. }
  69. - (void)cacheBeginPointForTouches:(NSSet *)touches
  70. {
  71.     if ([touches count] > 0) {
  72.         for (UITouch *touch in touches) {
  73.             CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
  74.             if (point == NULL) {
  75.                 point = (CGPoint *)malloc(sizeof(CGPoint));
  76.                 CFDictionarySetValue(touchBeginPoints, touch, point);
  77.             }
  78.             *point = [touch locationInView:self.superview];
  79.         }
  80.     }
  81. }
  82. - (void)removeTouchesFromCache:(NSSet *)touches
  83. {
  84.     for (UITouch *touch in touches) {
  85.         CGPoint *point = (CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
  86.         if (point != NULL) {
  87.             free((void *)CFDictionaryGetValue(touchBeginPoints, touch));
  88.             CFDictionaryRemoveValue(touchBeginPoints, touch);
  89.         }
  90.     }
  91. }
  92. @end