views:

659

answers:

1

Hi,

I am doing multi touch on UImageView means zoom in and zoom out on image view. I am using followng code but it doesn't work very well. Can anyone look at this code,

#import "ZoomingImageView.h"
@implementation ZoomingImageView

@synthesize zoomed;
@synthesize moved;

define HORIZ_SWIPE_DRAG_MIN    24
define VERT_SWIPE_DRAG_MAX     24
define TAP_MIN_DRAG            10

CGPoint startTouchPosition;
CGFloat initialDistance;

- (id)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
    // Initialization code
    moved = NO;
    zoomed = NO;
}
return self;
}

- (void)drawRect:(CGRect)rect {
// Drawing code
}


- (void)dealloc {
if([timer isValid])
    [timer invalidate];
[super dealloc];
}

- (void) setImage: (UIImage*)img
{
    zoomed = NO;
moved = NO;
self.transform = CGAffineTransformIdentity;
[super setImage:img];
}

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {

float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;

return sqrt(x * x + y * y);
}

- (CGFloat)scaleAmount: (CGFloat)delta {
CGFloat pix = sqrt(self.frame.size.width * self.frame.size.height);
CGFloat scale = 1.0 + (delta / pix);
return scale;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([timer isValid])
    [timer invalidate];

moved = NO;
switch ([touches count]) {
    case 1:
    {
        // single touch
        UITouch * touch = [touches anyObject];
        startTouchPosition = [touch locationInView:self];
        initialDistance = -1;
        break;
    }
    default:
    {
        // multi touch
        UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
     UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
        initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self] 
                                                 toPoint:[touch2 locationInView:self]];
        break;
    }

  }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
   UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
   if([timer isValid])
      [timer invalidate];

   /*if ([touches count] == 1) {
    CGPoint pos = [touch1 locationInView:self];
    self.transform = CGAffineTransformTranslate(self.transform, pos.x -   startTouchPosition.x, pos.y - startTouchPosition.y);
    moved = YES;
    return;
}****/

if ((initialDistance > 0) && ([touches count] > 1)) {
    UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
    CGFloat currentDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self] 
                                                     toPoint:[touch2 locationInView:self]];
    CGFloat movement = currentDistance - initialDistance;
    NSLog(@"Touch moved: %f", movement);
             CGFloat scale = [self scaleAmount: movement];
        self.transform = CGAffineTransformScale(self.transform, scale, scale);
   // }
   }
 }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
 if ([touches count] == 1) {
    // double tap to reset to default size
    if ([touch1 tapCount] > 1) {
        if (zoomed) {
            self.transform = CGAffineTransformIdentity;
            moved = NO;
            zoomed = NO;
        }
        return;
    }
 }
 else {
    // multi-touch
    UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
    CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self] 
                                                   toPoint:[touch2 locationInView:self]];
    CGFloat movement = finalDistance - initialDistance;
    NSLog(@"Final Distance: %f, movement=%f",finalDistance,movement);
    if (movement != 0) {
        CGFloat scale = [self scaleAmount: movement];
        self.transform = CGAffineTransformScale(self.transform, scale, scale);
        NSLog(@"Scaling: %f", scale);
        zoomed = YES;
    }
  }
}

- (void)singleTap: (NSTimer*)theTimer {
// must override
}

- (void)animateSwipe: (int) direction {
 // must override
}

It is not working fine on device. can anyone tell that where i am wrong.

A: 

When you use any CGAffinTransform.... the value of the frame property becomes undefined. In your code you are using the frame.size.width and frame.size.height to calculate the change in size. After the first iteration of CGAffinTransformScale you would not get the right scale factor. According to the documentation bounds would be the right property for scale calculations.

cameron