0

我正在尝试使用 CATiledLayer 和 UIScrollView 显示一个大而高质量的可缩放图像。我的代码基于iOS 大图像缩小示例代码

我的 UIScrollView 的层次结构是这样的:

  • UIScrollView
    • UIImageView : 代表大图的第一个缩略图
    • 旧平铺视图
    • 平铺视图

我希望滚动视图中的图像可以移动。

我尝试了我在这个StackOverflow 主题上找到的内容,并且它有效。但是在第一次放大图像后,就无法移动图像了。我不知道为什么?

这是我的代码:

-(id)initWithFrame:(CGRect)frame image:(UIImage*)_image {
    if((self = [super initWithFrame:frame])) {      
    // Set up the UIScrollView
        // Piece of code

        self.canCancelContentTouches = NO;

        UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture setMinimumNumberOfTouches:1];
        [panGesture setMaximumNumberOfTouches:1];
        [panGesture setDelegate:self];
        [frontTiledView addGestureRecognizer:panGesture];
        frontTiledView.exclusiveTouch = YES;

        UIPanGestureRecognizer* panGesture2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture2 setMinimumNumberOfTouches:1];
        [panGesture2 setMaximumNumberOfTouches:1];
        [panGesture2 setDelegate:self];
        [backgroundImageView addGestureRecognizer:panGesture2];
        backgroundImageView.exclusiveTouch = YES;

        UIPanGestureRecognizer* panGesture3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture3 setMinimumNumberOfTouches:1];
        [panGesture3 setMaximumNumberOfTouches:1];
        [panGesture3 setDelegate:self];
        [backTiledView addGestureRecognizer:panGesture3];
        backTiledView.exclusiveTouch = YES;
    }
    return self;
}

- (void)move:(UIGestureRecognizer*)sender {
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:frontTiledView];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        positionDeplacement = frontTiledView.center;
    }

    translatedPoint = CGPointMake(positionDeplacement.x+translatedPoint.x, positionDeplacement.y+translatedPoint.y);
    frontTiledView.center = translatedPoint;
    backTiledView.center = translatedPoint;
    backgroundImageView.center = translatedPoint;
}

谢谢。

4

1 回答 1

0

我找到了解决我的问题的方法。我只需要在 scrollViewDidEndZooming 委托方法中再次添加手势,它就像一个魅力:

这是我的代码:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    // set the new scale factor for the TiledImageView
    imageScale *=scale;
    CGRect imageRect = CGRectMake(0.0f,0.0f,CGImageGetWidth(image.CGImage) * imageScale,CGImageGetHeight(image.CGImage) * imageScale);

    // Create a new TiledImageView based on new frame and scaling.
    frontTiledView = [[TiledImageView alloc] initWithFrame:imageRect image:image scale:imageScale]; 
    [self addSubview:frontTiledView];
    [frontTiledView release];

    if (imageRect.size.width < 320 || imageRect.size.height < 460)
        [self initGestures];
}

- (void)initGestures {
    self.canCancelContentTouches = NO;
    frontTiledView.exclusiveTouch = YES;
    backgroundImageView.exclusiveTouch = YES;
    backTiledView.exclusiveTouch = YES;

    UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture setMinimumNumberOfTouches:1];
    [panGesture setMaximumNumberOfTouches:1];
    [panGesture setDelegate:self];
    [frontTiledView addGestureRecognizer:panGesture];

    UIPanGestureRecognizer* panGesture2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture2 setMinimumNumberOfTouches:1];
    [panGesture2 setMaximumNumberOfTouches:1];
    [panGesture2 setDelegate:self];
    [backgroundImageView addGestureRecognizer:panGesture2];

    UIPanGestureRecognizer* panGesture3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture3 setMinimumNumberOfTouches:1];
    [panGesture3 setMaximumNumberOfTouches:1];
    [panGesture3 setDelegate:self];
    [backTiledView addGestureRecognizer:panGesture3];
}
于 2012-11-24T13:47:56.070 回答