我希望将5张图像放入UIScrollView中,以便用户可以在它们之间滚动。
我该怎么做?
答案 0 :(得分:2)
最好使用UICollectionView
并将每张图片放在自己的单元格中。然后为您处理所有contentSize
,定位和滚动废话。
答案 1 :(得分: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 Image
,Remove Image
等
如果您还有其他问题想知道如何实施新功能,请询问。