2010-01-25 64 views
4

我一直在试图使用CGAffinTransformScale(使用CGAffinTransformScale(计划使用旋转,因此不能指望框架的缩放和添加子视图,因此UIScrollView将实现捏缩放/进出更复杂,我认为)。无论如何,这个概念很容易掌握,并且代码很快就会聚集在一起......从那以后,我一直试图用三种不同的方法解决相同的两个(相关的!)问题,并且无法做到这一点: 1-我的代码以某种方式失去了在缩放中间触摸计数的轨迹,从count = 2到count = 1并返回到iPhone,但不是模拟器。 2-触摸点1和2在每次移动中都会来回跳动几个像素,从而导致图像依次快速缩小和放大,即使总体效果是缩小或缩放为用户所期望的效果( iPhone和模拟器)。iPhone UIImageView pinch zoom

下面的代码:

#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 

日志样本:

^^^^^^^^^^^^^^^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!!!!!!! 

我开始怀疑,我失去的轨道,因为CGAffinTransformScale的触摸点;然而,我还没有发现任何网上任何事情表明这是一个问题。任何线索(包括'阅读xyz文档'),将不胜感激!

在此先感谢。

回答

6

一般来说,无论何时执行连续的用户界面行为,都需要针对不改变的东西进行测量。

因此,如果您的触摸导致视图转换发生变化,您应该测量触及不会改变的东西 - 例如您的父视图。因此,而不是,美其名曰:

[touch locationInView:self] 

你应该使用

[touch locationInView:[self superview]] 

我不知道这是否会解决您的问题,但它会消除你的烦恼的一个可能原因。

+0

就是这样!非常感谢你。 – cameron 2010-01-25 22:14:15

17

从左外野也许是一个答案,但另一种可能是把一个UIScrollView内的UIImageView,定义你的滚动视图代表一个viewForZoomingInScrollView:方法,并设置maximumZoomScale/minimumZoomScale属性和你有你捏缩放而不需要执行计算来自己设置变换?我刚刚在最近的一个项目上完成了这项工作,并且运行良好。

+0

是......大约比其他方式容易上千倍。谢谢! – glenc 2010-11-26 21:58:10

+0

苹果有这方面的一些很好的文档:http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html – Stickley 2012-06-03 00:25:01