我正在构建一个简单的应用程序,它显示带有这样照片的用户联系人 -
每个联系人都是一个定位子视图,其中另一个子视图包含一个 UIImageView 以生成带圆角的图像和一个 UILabel。
然而,即使只有 6-7 个联系人,iPhone 4/4s 和 iPhone 5 的滚动也变得相当不稳定。此外,波涛汹涌似乎是恒定的。无论他们有 8 个联系人还是 500 个联系人,都不会变得更糟或更好。
有人知道为什么会这样吗?生成此网格的算法如下所示。UIScrollView 上是否有我尚未设置的特定属性等?
- (void) generateContactGrid
{
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(self.addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(self.addressBook);
int peopleInARow = 0;
int maxPeopleInARow = 3;
int positionFromTop = 20;
int IMAGE_SIZE = 70;
float animationOffset = .5;
float animationOffsetChange = .3;
float animationDuration = .5;
int scaleOffset = 40;
int screenWidth = 320;
int startingPositionFromLeft = 26;
int positionFromLeft = startingPositionFromLeft;
int topOffset = 40;
int leftOffset = 26;
int numberOfRows = 0;
UIView *contactContainer;
UIImage* image;
CALayer *l;
NSString *name;
NSString *lastName;
NSString *firstName;
UIImageView *newimageview;
UILabel *label;
UIView *contactImageContainer;
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);
lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
if(ABPersonHasImageData(person)){
NSLog(@"Current Contact Being Generated: %@", name);
image = [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)];
NSLog(@"Contact's Position From Left: %i", positionFromLeft);
newimageview = [[UIImageView alloc] initWithFrame:CGRectMake(-scaleOffset/2, -scaleOffset/2, IMAGE_SIZE+scaleOffset, IMAGE_SIZE+scaleOffset)];
newimageview.contentMode = UIViewContentModeScaleAspectFit;
[newimageview setImage: image];
contactContainer = [[UIView alloc] initWithFrame:CGRectMake(positionFromLeft, positionFromTop + 20, IMAGE_SIZE, 200)];
contactImageContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_SIZE, IMAGE_SIZE)];
contactImageContainer.clipsToBounds = YES;
l = [contactImageContainer layer];
[l setMasksToBounds:YES];
[l setCornerRadius:IMAGE_SIZE/2];
[l setBorderWidth:0.0];
[l setBorderColor:[[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:.6] CGColor]];
[contactImageContainer addSubview:newimageview];
[contactContainer addSubview:contactImageContainer];
label = [[UILabel alloc] initWithFrame: CGRectMake(0, IMAGE_SIZE + 10, IMAGE_SIZE, 20)];
label.text = firstName;
label.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
label.textColor = [UIColor whiteColor];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
[contactContainer addSubview:label];
contactContainer.alpha = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationDelay:animationOffset];
animationOffset+= animationOffsetChange;
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
contactContainer.alpha = 1;
CGRect temp = contactContainer.frame;
temp.origin.y = positionFromTop;
contactContainer.frame = temp;
[UIView commitAnimations];
if(peopleInARow >= 2){
positionFromTop += IMAGE_SIZE + topOffset;
peopleInARow = 0;
positionFromLeft = startingPositionFromLeft;
numberOfRows++;
} else {
peopleInARow += 1;
positionFromLeft += IMAGE_SIZE + leftOffset;
}
[self.scrollView addSubview:contactContainer];
[self.scrollView bringSubviewToFront:contactContainer];
}
}
NSLog(@"%i", numberOfRows);
self.scrollView.contentSize = CGSizeMake(screenWidth, 150 * numberOfRows);
}