2011-07-29 4 views
4

UIImagesUIImageViewに追加しようとしており、UIScrollViewでスクロールできるようにしています。 UIImageViewの下にさまざまな画像を追加する方法がわかりません。 以下はUIImageViewに画像を追加し、スクロール可能にする私のコードです。UIScrollViewで10種類のUIImagesを追加する

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    UIImage *image = [UIImage imageNamed:@"ae.jpg"]; 
    imageView = [[UIImageView alloc]initWithImage:image]; 
    imageView.frame = [[UIScreen mainScreen] bounds]; 
    imageView.contentMode = (UIViewContentModeScaleAspectFit); 
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
    imageView.backgroundColor = [UIColor clearColor]; 



    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    scrollView.contentMode = (UIViewContentModeScaleAspectFit); 

    scrollView.contentSize = CGSizeMake(image.size.width,960); 
    scrollView.pagingEnabled = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.alwaysBounceVertical = NO; 
    scrollView.alwaysBounceHorizontal = NO; 
    scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
    scrollView.maximumZoomScale = 2.5; 
    scrollView.minimumZoomScale = 1; 
    scrollView.clipsToBounds = YES; 
    [scrollView addSubview:imageView]; 
    [image release]; 
    [imageView release]; 
    [self.view addSubview:scrollView]; 
} 
+0

同じUIScrollViewで複数のUIImageViewを使用したいと思うかもしれません。しかし、あなたがしたいことが正確には不明です。 – jtbandes

+0

「ae.png」の下に別の画像を追加してスクロールできるように、さまざまな画像を上下に追加したいとします。現在、画像は1つしかありませんが、UIIImageViewで画像の配列を追加したい場合は可能ですか? – lifemoveson

+0

複数のUIImageViewを使用するだけです。 – jtbandes

答えて

8

基本的には簡単です。 3つの画像をUIScrollViewに配置したいとします。 各画像は300x300です。この場合、あなたはフレームとスクロールビューがあるでしょう:あなたはそれが適切なフレームとUIImageViewだ持っている必要がありますすべての画像については

scrollView.contentSize = CGSizeMake(image.size.width,900); 

imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)]; 
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)]; 
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)]; 
imgView1.image = [UIImage imageNamed:@"ProperName.png"]; 
... 

を()(yOriginにCGRectMakeで2番目に値を注意を払います) 、あなたが行ったように、その後:もちろん

[scrollView addSubview:imgView1]; 
[scrollView addSubview:imgView2]; 
[scrollView addSubview:imgView3]; 
[imgView1 release]; 
[imgView2 release]; 
[imgView3 release]; 

を、それは簡単なコードですが、あなたはそれを最適化します;)

+0

どのような最適化をお勧めしますか? UUmageview(シャドウとボーダー)と2つのUILabels(テキストエフェクトなし)を含む20のuiviewで、私のUIScrollViewはスムーズにスクロールしません。 – David

+0

まあ、私はこの種の問題にぶつかりませんでしたが、私はいくつかのページ再利用アルゴリズムを実装しようとします(UITableViewはこの方法をセルの再利用に使用します)。そのようにすれば、常に限られた量のページ(およびそれに応じたリソース)が得られます。明らかに、スクロールするときにコンテンツを動的に埋めなければなりません。あなたは私の答えを読むことができます(http://stackoverflow.com/questions/8374907/uiscrollview-selection-like-uipickerview答えの部分n.2を見てください)。そこでは、メモリを節約する方法の考え方の1つを得るでしょう。がんばろう。 – makaron

関連する問題