0

CATiledLayerとUIScrollViewを使用して、大きくて高品質のズーム可能な画像を表示しようとしています。私のコードは、 iOSラージイメージのダウンサイジングサンプルコードに基づいています。

私のUIScrollViewの階層は次のようなものです:

  • UIScrollView
    • UIImageView:大きな画像の最初のサムネイルを表します
    • Old TiledView
    • TiledView

スクロールビューへの画像が移動可能になるようにしたい。

この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 に答える