Objective c GCD滚动视图加载问题
我正在使用GCD为我的滚动视图加载图像,但它没有跟随我创建的帧,而是只生成一个帧,其中图像只是在其中闪烁。这是我的密码:Objective c GCD滚动视图加载问题,objective-c,uiscrollview,grand-central-dispatch,Objective C,Uiscrollview,Grand Central Dispatch,我正在使用GCD为我的滚动视图加载图像,但它没有跟随我创建的帧,而是只生成一个帧,其中图像只是在其中闪烁。这是我的密码: //Create the array -(void)awakeFromNib { images = [NSMutableArray arrayWithObjects: [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageOne" ofType:@"png"]],
//Create the array
-(void)awakeFromNib {
images = [NSMutableArray arrayWithObjects: [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageOne" ofType:@"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageTwo" ofType:@"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageThree" ofType:@"png"]],nil];
}
//Populate scrollview
-(void)populate {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
scrollview.pagingEnabled = YES;
scrollview.backgroundColor = [UIColor clearColor];
scrollview.clipsToBounds = YES;
scrollview.delegate = self;
[scrollview setContentSize:CGSizeMake(2 * scrollview.frame.size.width, scrollview.frame.size.height)];
int number = [images count] ;
NSLog(@"%i",images);
int rowTwoInt = 0;
int rowThreeInt = 0;
int rowFourInt = 0;
for (int i = 0; i < number; i++) {
if (i <= 3) {
finalFrame = CGRectMake( i*(80), 12, 80, 80);
}
if (i == 4) {
finalFrame = CGRectMake(0, 95, 80, 80);
}
if ((i >4) && (i <= 7)) {
rowTwoInt = rowTwoInt + 80;
finalFrame = CGRectMake(rowTwoInt, 95, 80, 80);
}
if (i == 8) {
finalFrame = CGRectMake(0, 178, 80, 80);
}
if ((i > 8) && (i <= 11)) {
rowThreeInt = rowThreeInt + 80;
finalFrame = CGRectMake(rowThreeInt, 178, 80, 80);
}
if (i == 12) {
finalFrame = CGRectMake(0, 261, 80, 80);
}
if ((i > 12) && (i <= 15)) {
rowFourInt = rowFourInt + 80;
finalFrame = CGRectMake(rowFourInt, 261, 80, 80);
}
IconButton = [UIButton buttonWithType:UIButtonTypeCustom];
IconButton.frame = CGRectMake(0, 0, 57, 57);
IconButton.center = CGPointMake(40, 29);
[IconButton addTarget:self action:@selector(Selected:) forControlEvents:UIControlEventTouchUpInside];
IconButton.tag = i;
dispatch_async(queue, ^{
UIImage *icon = [images objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[IconButton setImage:icon forState:UIControlStateNormal];
});
});
[cell addSubview:IconButton];
cell = [[UIView alloc] initWithFrame:finalFrame];
[scrollview addSubview:cell];
//创建数组
-(无效)从NIB中唤醒{
images=[NSMutableArray Array WithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“png”类型的“imageOne”,[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“png”类型的“imageTwo”],[UIImage imageWithContentsOfFile:[NSBundle mainBundle]pathForResource:@“png”类型的“imageTwo:@“png”],无];
}
//填充滚动视图
-(空)填充{
调度队列=调度获取全局队列(调度队列优先级高,0ul);
scrollview.PaginEnabled=是;
scrollview.backgroundColor=[UIColor clearColor];
scrollview.clipsToBounds=是;
scrollview.delegate=self;
[scrollview setContentSize:CGSizeMake(2*scrollview.frame.size.width,scrollview.frame.size.height)];
整数=[图像计数];
NSLog(@“%i”,图像);
int rowTwoInt=0;
int-rowtreeint=0;
int rowFourInt=0;
for(int i=0;i 如果(I4)&(I8)&(I12)&(i我怀疑您已将IconButton
声明为实例变量、static
变量或全局变量
每次通过循环时,您都会更改IconButton
的值。在十二个队列块执行时,IconButton
被设置为其最终值-对您创建的最后一个按钮的引用。所有块都使用实例(或static
或global)IconButton
变量,因此所有块更新相同的最终按钮
创建一个局部变量以引用按钮,并在循环中使用该局部变量:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
IconButton = button;
IconButton.frame = CGRectMake(0, 0, 57, 57);
...
dispatch_async(queue, ^{
UIImage *icon = [images objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[button setImage:icon forState:UIControlStateNormal];
});
});
每个块将在创建块时捕获局部变量的值,因此每个块将捕获对不同按钮的引用。请将代码正确缩进。另外,向我们展示如何声明IconButton
。