iPhone UIImageView powiększenie szczypta

Próbowałem zaimplementować pinch zoom / in-out dla PhotoView (instancja UIImageView) przy użyciu CGAffinTransformScale (planując użycie rotacji, aby nie można było liczyć na ramki dla powiększenia i dodawałbym widoki, więc UIScrollView byłby bardziej skomplikowany). Tak czy inaczej, koncepcja była łatwa do uchwycenia, a kod zebrał się bardzo szybko ... Od tego czasu próbuję rozwiązać te same dwa (powiązane ?!) problemy, używając trzech różnych podejść i nie mogę tego zrobić : 1- Mój kod w jakiś sposób traci ślad liczby zetknięć w środku zoomu, od liczby = 2 do zliczania = 1 i z powrotem na iPhone, ale nie na symulatorze. 2- Punkty dotykowe jeden i dwa przeskakują do tyłu i do przodu o kilka pikseli przy każdym ruchu, powodując, że obraz kurczy się i powiększa sukcesywnie i szybko, mimo że ogólnie efektem jest zmniejszenie lub powiększenie zgodnie z zamierzeniami użytkownika (oba iPhone i symulator).

oto kod:

#import "PhotoView.h"


@implementation PhotoView;
@synthesize originalCenter, distance, zooming;
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    // Initialization code
        self.userInteractionEnabled = YES;
        self.multipleTouchEnabled = YES;
        zooming = NO;
    }
    return self;
}

float distanceBetweenTwoPoints(CGPoint point1, CGPoint point2)
{
 NSLog(@"point1 x: %5.2f point 2 x: %5.2f ---- point 1 y: %5.2f  point 2 y:    %5.2f",point1.x,point2.x,point1.y,point2.y);
    return (sqrt(pow(point1.x -point2.x,2) + pow(point1.y - point2.y,2)));
}
-(void) touchesBegan: (NSSet *) touches withEvent:(UIEvent *) event {

    if ([touches count] > 1) {

      NSLog(@"^^^^^^^^^^^^^^^Tocuhes began with double touch!");

      distance = distanceBetweenTwoPoints([[[touches allObjects] objectAtIndex:0] locationInView:self], 
        [[[touches allObjects] objectAtIndex:1] locationInView:self]);
      zooming = YES;
    }
    else {
       zooming = NO;
       origianlCenter = [[[touches allObjects] objectAtIndex:0] locationInView:self];
       NSLog(@">>>>>>>>>>>>Touches began with single touch");
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if (zooming) NSLog(@"!!!!!!!!!end zoom!!!!!!!");

    zooming = NO;
    if ([[touches anyObject] tapCount] == 2) {
        UITouch *thisTouch = [touches anyObject];
        CGPoint thisPoint = [thisTouch locationInView:self];
    }
}

- (void) touchesMoved: (NSSet *) touches withEvent:(UIEvent *) event {
    if ([touches count] > 1 && zooming) { // ignore if user added a second finger touch
        float distanceNew = distanceBetweenTwoPoints([[[touches allObjects] objectAtIndex:0]     locationInView:self], 
                 [[[touches allObjects] objectAtIndex:1] locationInView:self]);  
        if (distance <= 0.f) {    // should never be true - but it is sometimes!!!
           distance = distanceNew;
        }  
        float delta = 1.0f + ((distanceNew-distance)/distance);
        self.transform = CGAffineTransformScale(self.transform, delta, delta);
        distance = distanceNew;
    }
    else {
       if (zooming) {
          NSLog(@"*************shouldn't be here********* %d",[touches count]);
          return;
       }
       CGPoint thisPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self];
       self.transform = CGAffineTransformTranslate(self.transform, thisPoint.x-originalCenter.x, thisPoint.y-originalCenter.y);

    }
}
- (void)dealloc {
    [super dealloc];
}
@end

Próbka dziennika:

^^^^^^^^^^^^^^^Tocuhes began with double touch!
point1 x: 87.33 point 2 x: 235.63 ---- point 1 y: 322.30  point 2 y: 117.09
point1 x: 90.76 point 2 x: 232.02 ---- point 1 y: 318.29  point 2 y: 123.51
point1 x: 86.22 point 2 x: 236.71 ---- point 1 y: 323.30  point 2 y: 117.42
point1 x: 89.51 point 2 x: 232.38 ---- point 1 y: 319.47  point 2 y: 123.47
point1 x: 84.97 point 2 x: 237.02 ---- point 1 y: 324.48  point 2 y: 116.56
*************shouldn't be here********* 1
point1 x: 88.49 point 2 x: 232.52 ---- point 1 y: 321.27  point 2 y: 122.91
*************shouldn't be here********* 1
point1 x: 83.95 point 2 x: 237.11 ---- point 1 y: 327.21  point 2 y: 116.96
!!!!!!!!!end zoom!!!!!!!

Zaczynam podejrzewać, że tracę punkty dotykowe z powodu CGAffinTransformScale; jednak nie znalazłem niczego online, co sugerowałoby, że to problem. Wszelkie wskazówki (w tym „przeczytaj dokumentację na xyz”) będą mile widziane!

Z góry dziękuję.

questionAnswers(2)

yourAnswerToTheQuestion