1

我在 UImageView 上进行多点触控意味着放大和缩小图像视图。我正在使用以下代码,但效果不是很好。任何人都可以看看这段代码,

#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
}

它在设备上无法正常工作。谁能告诉我哪里错了。

4

1 回答 1

0

当您使用任何 CGAffinTransform...时,frame 属性的值将变为未定义。在您的代码中,您使用 frame.size.width 和 frame.size.height 来计算大小的变化。在 CGAffinTransformScale 的第一次迭代之后,您将无法获得正确的比例因子。根据文档 bounds 将是比例计算的正确属性。

于 2010-01-25T16:32:43.620 回答