2013-12-10 115 views

回答

2

你最好使用UICollectionView並讓每個圖像都在它自己的單元格中。然後,所有的contentSize,定位和滾動廢話是爲您處理。

0

我會建議使用一個UICollectionView,但如果你堅持使用UIScrollView你可以做類似如下:

.h文件中

@interface MyViewController : UIViewController 

@property (nonatomic, strong) UIScrollView *scv; 

@end 

.M

@interface MyViewController() 

@property (nonatomic, strong) NSMutableArray *imageArray; 

@end 

@implementation MyViewController 

@synthesize scv = _scv, imageArray = _imageArray; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _arrayOfImages = [[NSMutableArray alloc] init]; 
    [_arrayOfImages addObject:[UIImage imageNamed:@"yourImageName.png"]]; // Do this for as many UIImages you want to add. 
    [self configureView]; 
} 

- (void)configureView 
{ 
    _scv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    float yposition = 0; 
    for(int i = 0; i < [_arrayOfImages count]; i++) { 
     UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, yposition, self.view.frame.size.width-20, 100)]; 
     [img setImage:[_arrayOfImages objectAtIndex:i]; 
     [img setTag:i]; 
     [_scv addSubview:img]; 
     yposition =+ 100 + 10; // get the current yposition = yposition + img height + an extra 10 to add a gap. 
    }  
    [_scv setContentSize:CGSizeMake(self.view.frame.size.height, yposition)]; // Set the width to match the screen size, and set the height as the final yposition. 
    [[self view] addSubview:_scv]; 
} 

@end 

這是一個非常簡單的版本,你有什麼更復雜的版本,可以更容易地實現Add New ImageRemove Image

如果您還有其他問題想知道如何實現新功能,只需詢問。

相關問題