5

私はiPhone開発者の初心者ですが、

画像をスクロールしたいのですが、

これが私のコードスニペットです。

- (void)viewDidLoad {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

 [self centerScrollViewContents];
}


- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.imageView.frame = contentsFrame;
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}

またbg.png、私の背景に適用されていません。

どんな助けでも大歓迎です。

4

8 に答える 8

4

答えは、実際には他の多くの答えとコメントの組み合わせと、その他の調整です。

  1. Pan が言ったように、scrollView デリゲートを設定する必要があります。そうしないと、viewForZoomingInScrollView: デリゲートの実装が呼び出されません。この呼び出しを viewDidLoad: で提供しましたが、これをインターフェイス ビルダーまたはストーリーボードに接続することもできます。
  2. Teodor Carstea が元の投稿へのコメントで述べたように、元のコードでは、あなた.imageView.sizeとあなた .scrollView.contentsSizeをまったく同じものに設定しています。.imageView.frame. の一部として発生する を設定するコードは必要ありませんinitWithImage:。これにより、サイズが異なります。
  3. .scrollView.contentSizeを に設定する必要がありますimage.size。どこでこれを行うかは、少なくともズームスケールを設定する前に行う必要があるという点で重要です。
  4. Kunal Balani が述べたように、ストーリーボードまたはインターフェイス ビルダーで設定できますが、scrollingEnabled が必要です。
  5. MidhunMP が言ったように、必要がありますが.userInteractionEnabled = YES、一般的に、これらはストーリーボード/Interface Builder で設定されるデフォルトであるため、以下のコード変更には含めません。
  6. あなたのメソッドcenterScrollViewContentsは、 imageViewフレームを再形成しているので、実際にはコンテンツを中央に配置していません。完全に不要だと思うので、呼び出しをコメントアウトしただけです。代わりに、画像を左上隅から開始するための簡単な呼び出しを行いました。
  7. スクロールするだけの場合はzoomScaleminimumZoomScaleと を 設定するmaximumZoomScale必要はありません。ピンチズームも行う場合は、これら 3 つすべてを適切な場所に設定する必要があります。
  8. ビューがコンテンツの境界よりも小さい場合は、ビューを引き伸ばしたいようです。これが本当に問題である場合 (つまり、bg.png が小さすぎて引き伸ばす必要があることがわかっている場合)、contentFrame と画像フレームの幅の比率と高さの比率を別々に計算し、これらのいずれか大きい方が新しいものになります。 zoomScale を使用すると、反対方向にスクロールできます。または、そのスケール セットをどのように使用するかを決定する他の方法があります。必要に応じてズームスケールを設定するための他の回答については、stackoverflow.com を検索してください。

これらの項目を適用した後のコードは次のとおりです。

(void)viewDidLoad {
    self.scrollView.delegate = self;         // can be set in storyboard
    self.scrollView.scrollingEnabled = YES;  // can be set in storyboard
    NSString* bgPng
      = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"];
    UIImage *image1 = [UIImage imageWithContentsOfFile:bgPng];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
//  self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

    // set zoom scale here if desired.

    // if zoom is implemented, the following call should be made
    // after setting .zoomScale, .minimumZoomScale & .maximumZoomScale
    self.scrollView.contentOffset = CGPointZero;

//  [self centerScrollViewContents];
}


//- (void)centerScrollViewContents {
//    CGSize boundsSize = self.scrollView.bounds.size;
//    CGRect contentsFrame = self.imageView.frame;
//
//    if (contentsFrame.size.width < boundsSize.width) {
//        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
//    } else {
//        contentsFrame.origin.x = 0.0f;
//    }
//
//    if (contentsFrame.size.height < boundsSize.height) {
//        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
//    } else {
//        contentsFrame.origin.y = 0.0f;
//    }
//  
//    self.imageView.frame = contentsFrame;
//}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}
于 2012-07-31T18:43:20.657 に答える
1

まず、 scrollview frame を確認してください。スクロールは、フレーム サイズが contentsize より小さい場合にのみ有効になります。すべてのコンテンツがフレーム内に表示されている場合、なぜスクロールが必要なのですか??

これを試して :

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
                 self.view.frame.size.width, self.view.frame.size.height)];
    //どんなフレームでも初期化

  scroll.scrollEnabled = YES // これは imp です ... これを無視しないでください

   //コードが何であれ

    self.scrollView.contentSize = image.size;// これは間違っています

    //contentSize プロパティをスクロール可能なコンテンツのサイズに設定します。スクロール可能領域のサイズを指定します。
    // これを行う
    self.scrollView.contentSize = imageView.frame.size;

于 2012-07-25T22:33:14.647 に答える
1

さて、これを試してください:

UIImageView *tempImgv = [[[UIImageView alloc]init] autorelease];
    [tempImgv setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/bg.png", [[NSBundle mainBundle]resourcePath]]]];
    [tempImgv setFrame:your_desired_frame];
    self.imageView = tempImgv;
    [self.scrollWiew setContentSize: size_greater_than_scroll_frame];
    [self.scrollWiew addSubview:self.imageView];
于 2012-07-19T11:31:01.007 に答える
0

スクロールビューデリゲートを別の場所に設定しましたか?
このような:self.scrollView.delegate = self;

于 2012-07-31T15:22:33.733 に答える
0

上記のコードでは、最小および最大のズーム スケールを設定する必要があります。

http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
于 2012-07-31T10:24:06.920 に答える
0

画像のスクロール

scroll.userinteractionEnable = YES;

その他、

self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.png"]]];
于 2012-07-30T18:12:18.243 に答える
0

上記のコードでは、

    UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
    self.imageView = [[UIImageView alloc] initWithImage:image1];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = image.size;

設定している scrollView のコンテンツ サイズは、画像のサイズ、つまり上記の太字のコードと同じです。次のコードで試してください。

CGSize sizeOfScroll = image.size;
sizeOfScroll.width *= 1.5f;
sizeOfScroll.height *= 1.5f;

self.scrollView.contentSize = sizeOfScroll;
于 2012-07-25T05:16:38.957 に答える