2009-10-19 1 views
1

UImageView에서 멀티 터치를하고 있는데 이미지 뷰를 확대/축소하는 것을 의미합니다. followng 코드를 사용하고 있지만 잘 작동하지 않습니다. 누구든지이 코드를 볼 수 있습니까?UIImageView에서 멀티 터치가 완벽하게 작동하지 않습니다.

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

장치에서 제대로 작동하지 않습니다. 아무도 내가 틀린 곳이라고 말할 수 있습니까?

+0

"잘 작동하지 않습니다." 그게 무슨 뜻 이죠? 천천히? 전혀 작동하지 않습니까? 그것은 부서 지는가? lazily 다른 앱이 대신 작동하는 것을 보면서 앉아 있니? 코드를 살펴 보려면 조사 할 내용을 알려줘야합니다. – mahboudz

+0

안녕하세요, 그 시간을 축소하거나 확대 할 때 이미지를 뒤집을 때가 있습니다. 이미지가 약간 늘어나거나, 너무 커지거나하는 경우가 있습니다. 때로는 멀티 터치 동작의 영향을받지 않습니다. – iphonemobdev

답변

0

CGAffinTransform을 사용하면 frame 속성의 값이 정의되지 않게됩니다. 코드에서 frame.size.width 및 frame.size.height를 사용하여 크기의 변화를 계산합니다. CGAffinTransformScale의 첫 번째 반복 이후에 올바른 배율 인수를 얻지 못합니다. 문서에 따르면 경계는 규모 계산에 적합한 속성입니다.