这就是问题所在。我想制作一个照片查看器应用程序,它为每个页面显示 6 张图像,并使用滚动视图页面控件。
但现在我有一个问题,因为添加到许多子视图会导致滞后。我不知道如何处理。
这是我的代码:
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
HUD.labelText = @"Loading...";
HUD.mode = MBProgressHUDAnimationFade;
[self.view addSubview:HUD];
self.navigationItem.leftBarButtonItem.enabled = NO;
const CGFloat itemWidth = 160.0f; //panjang
const CGFloat itemHeight = 127.0f;
const CGFloat buttonWidth = 157.0f; //panjang button
const CGFloat buttonHeight = 125.0f; //tinggi button
const CGFloat marginHorz = (itemWidth - buttonWidth)/3.0f;
const CGFloat marginVert = (itemHeight - buttonHeight)/2.0f;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:storyUrl];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
jsons = JSON;
NSString *urll;
int index = 0;
int row = 0;
int column = 0;
//start
for(id item in jsons){
//[self.button removeFromSuperview];
if ([item objectForKey:@"img_medium"] == [NSNull null]) {
urll=[item objectForKey:@"img_medium"];
}else{
urll=[item objectForKey:@"img2_medium"];
}
button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImageView *imgggg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth , buttonHeight)] ;
[imgggg setImageWithURL:[NSURL URLWithString:urll] placeholderImage:[UIImage imageNamed:@"no_photo.png"]];
[button addSubview:imgggg];
button.tag = index;
//button placing
button.frame = CGRectMake(column*itemWidth + marginHorz, row*itemHeight + marginVert, buttonWidth, buttonHeight);
[button addTarget:self
action:@selector(switchView:)
forControlEvents:UIControlEventTouchUpInside];
NSLog(@"offset but %f",column*itemWidth + marginHorz);
NSLog(@"column %d",column);
NSLog(@"row %d",row);
//UIImage *pic = [UIImage imageNamed:@"1.jpg"];
UILabel *labeling = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 157, 45)];
[labeling setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.45]];
[labeling setText:[item objectForKey:@"title"]];
[labeling setTextAlignment:NSTextAlignmentCenter];
labeling.numberOfLines = 0;
[labeling setTextColor:[UIColor whiteColor]];
[labeling setFont:[UIFont boldSystemFontOfSize:12.0]];
//UIImageView *img = [[UIImageView alloc] initWithImage:pic];
//[img setFrame:CGRectMake(0, 0, 50, 50)];
[button addSubview:labeling];
[scrollView addSubview:button];
index++;
row++;
if (row == 3) {
row = 0;
column++;
}
NSLog(@"index %d",index);
}
//end
[MBProgressHUD hideHUDForView:self.view animated:YES];
self.navigationItem.leftBarButtonItem.enabled = YES;
[self listSubviewsOfView:scrollView];
//end block ui
//[[UIApplication sharedApplication] endIgnoringInteractionEvents];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
[MBProgressHUD hideHUDForView:self.view animated:YES];
self.navigationItem.leftBarButtonItem.enabled = YES;
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Request Time Out" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[errorAlert show];
//end block ui
//[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
[operation start];