2013-12-10 5 views
-4

사용자가 UIScrollView에 넣을 이미지가 5 개 있으므로 사용자가 스크롤 할 수 있습니다.UIScrollView에 5 개의 이미지 추가

어떻게하면됩니까?

+2

예를 들어, "UIScrollView tutorial"을 검색 할 수 있습니다. –

답변

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 Image,를보다 쉽게 ​​구현할 수있는 더 복잡한 버전이 나온 후 매우 단순한 버전입니다.등

더 많은 질문이 있으면 새로운 기능을 구현하는 방법을 알고 싶습니다.

관련 문제