따라서 크기가 조정 된 이미지를 배치하여 첫 번째 UIScrollview를 수행하고 있지만 첫 번째 결과 세트의 끝에 도달하고 두 번째 세트 등을 추가하면보기가 시작으로 재설정됩니다. x 값이 올바르게 계산되고 있기 때문에 이미지를 추가하는지 확실하지 않으므로 UIScrollview의 크기가 조정되어야하므로 이전 이미지 위에 다음 이미지 세트 만 배치한다고 가정 할 수 있습니다.
내가 여기서 뭘 잘못하고 있니?
-(void)addScrollViewImages {
// setup our UIScrollview
self.scrollView.delegate = self;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,self.scrollView.frame.size.height);
// hit our server for the data
NSString *encodedKeywords =[self.keyword stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
self.results = [self fetchImageResults:encodedKeywords];
// loop over some image results
for (id image in self.results)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(self.x, 0, self.screenWidth, self.screenHight)];
NSString *imageUrl = image[@"url"];
NSData *image = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
// resize the image to the device
[imageView setImage:[self imageScaleCropToSize:[UIImage imageWithData:image]]];
[self.scrollView addSubview: imageView];
self.x = (self.x + imageView.frame.size.width);
// resize frame width
if(self.x > self.view.frame.size.width) self.scrollView.contentSize = CGSizeMake(self.x, self.scrollView.frame.size.height);
}
}
// This just gets the json data and processes it a little
-(NSArray*)fetchImageResults:(NSString*)keyword {
//This should be fairly quick
NSError *error = nil;
NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/api/v1/search?q=%@", keyword]];
NSData *jsonData = [NSData dataWithContentsOfURL:apiURL];
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
return result[@"results"];
}
// Scroll index management
- (void)scrollViewDidScroll:(UIScrollView *)sender {
float indexNum = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
if(floor(indexNum) == indexNum) {
int num = (int)indexNum;
NSLog(@"viewing %@",self.results[num]);
// if we are at the end, load more
if(num == (self.results.count-1) ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self addScrollViewImages];
});
}
}
}
나는 그것을 알아.
우선 나는 viewDidLoad 메서드에서 처음에 결과 NSMutableArray를 시작하지 않았습니다.
self.results = [[NSMutableArray alloc] init];
일단 변경하면 스크롤 방법을 업데이트하기 만하면됩니다.
// Scroll index management
- (void)scrollViewDidScroll:(UIScrollView *)sender {
float indexNum = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
float currentPosition = self.scrollView.contentOffset.x;
if(floor(indexNum) == indexNum) {
int num = (int)indexNum;
// if we are at the end, load more
if(num == ([self.results count]-1)) {
dispatch_async(dispatch_get_main_queue(), ^{
[self addScrollViewImages];
[self.scrollView setContentOffset:CGPointMake(currentPosition, 0.0)];
});
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다