0

私は大量の画像 (つまり約 50) をロードする必要がある iPhone アプリケーションを持っています。この画像には (サムネイルの少し上) のような小さな四角形があり、各行に 3 つ、1 ページに 4 つの列があり、水平方向にスクロールする必要があります。次の 12 枚の画像などをロードします。また、その下にページ番号を1、2などとして表示する必要があります。添付画像のように

スクロールビューでこれを達成するために、体は私を正しい方向に導くことができますか?私は本当に混乱しています.このようなスクロールビューにイメージビューを追加する方法,また、各イメージビューには独自のボタンもあります.

4

2 に答える 2

0

私がこの問題に取り組む方法は次のとおりです:(それらはあなたの特定のニーズと要件に依存しているので、私は多くの詳細を提供するつもりはありません)。

.hファイル:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (unsafe_unretained, nonatomic) IBOutlet UIScrollView *scrollView;//assuming the scrollView is connected in a xib file

-(id)initWithImages:(NSArray*)images;//that could be whatever format you get your images(UIImage, urls to download, file names or custom views. Here we assume that the array contains instances of UIImage)

@end

.mファイル:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *images;
@property (nonatomic, assign) CGFloat pageWidth;
@property (nonatomic, assign) int currentPage;

@end

@implementation ViewController


-(id)initWithImages:(NSArray*)images{

    self = [super init];

    if(self){

        self.images = images;

    }
    return self;

}

-(void)viewDidLoad{
    [super viewDidLoad];

    [self.scrollView setDelegate:self];
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.imageURLsArray.count,
                                               self.scrollView.frame.size.height)];
    [self.scrollView setPagingEnabled:YES];
    [self.scrollView setShowsVerticalScrollIndicator:NO];

    self.pageWidth = self.scrollView.frame.size.width;


    for(int i = 0; i < self.images.count; i++){

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width*i,
                                                                              self.scrollView.frame.origin.y,
                                                                              self.scrollView.frame.size.width,
                                                                              self.scrollView.frame.size.height)];
        imageView.image = [self.images objectAtIndex:i];
        [self.scrollView addSubview:imageView];

    }

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = floor(scrollView.contentOffset.x - self.pageWidth/2)/self.pageWidth + 1;
    if(self.currentPage != page){
        NSLog(@"Scrolled to new page: %d", page);
        self.currentPage = page;
//do additional things based on the fact that you have scrolled to a new page (like changing the page number)
    }
 }
@end
于 2013-03-01T10:24:47.843 に答える
0

UItableview を使用して、2 つのイメージビューを含むカスタム セルを作成し、そのカスタム セルを indexpath のセルにロードしてから、nsarray を作成し、画像名をその配列に渡し、その画像をデータ形式に変換します。

nsdata *data = [NSdata dataWithcontents of url [index:indexpath.row の配列オブジェクト]]; cell.imageview.image = [uiimage imagewithdata:データ]; このためには、カスタム セルを作成する方法を知っておく必要があります。

于 2013-03-01T12:42:56.057 に答える