我可以重新编码循环 UIImageView 以获得更短的代码吗?

发布于 2024-11-02 16:36:23 字数 1385 浏览 4 评论 0原文

我可以让这段代码比这个短吗?

- (void) setupFeature
{   NSArray *numbers = [NSArray arrayWithObjects: @"01", @"02", @"03",@"04",@"05",@"06", nil];
    position = CGRectMake(7, position.origin.y+20, 72, 72);

    int j=0;
    NSString *pic;
    UIImageView *a_pic;
    NSMutableArray *C_Pic = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<[numbers count]; i++) 
    { 
        UIImageView *picture = [[UIImageView alloc] init];
        [C_Pic addObject:picture];
        [picture release];
        a_pic = [C_Pic objectAtIndex:i];
        pic = [NSString stringWithFormat:@"iconD%@",[numbers objectAtIndex:i]];
        a_pic.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:pic ofType:@"png"]];
        if(j<4)
        {   
            a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
            j++;
        }
        else
        {   
            j=0;
            position = CGRectMake(7, position.origin.y+77, 72, 72);
            NSLog(@"Pic%i position %@",i, NSStringFromCGRect(self.position));
            a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
        }
    }
}

如果可以再短一点吗??请帮助我该怎么做?

Can I make this code shorter than this ?

- (void) setupFeature
{   NSArray *numbers = [NSArray arrayWithObjects: @"01", @"02", @"03",@"04",@"05",@"06", nil];
    position = CGRectMake(7, position.origin.y+20, 72, 72);

    int j=0;
    NSString *pic;
    UIImageView *a_pic;
    NSMutableArray *C_Pic = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<[numbers count]; i++) 
    { 
        UIImageView *picture = [[UIImageView alloc] init];
        [C_Pic addObject:picture];
        [picture release];
        a_pic = [C_Pic objectAtIndex:i];
        pic = [NSString stringWithFormat:@"iconD%@",[numbers objectAtIndex:i]];
        a_pic.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:pic ofType:@"png"]];
        if(j<4)
        {   
            a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
            j++;
        }
        else
        {   
            j=0;
            position = CGRectMake(7, position.origin.y+77, 72, 72);
            NSLog(@"Pic%i position %@",i, NSStringFromCGRect(self.position));
            a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
        }
    }
}

If it can be shorter ?? please , help me how to do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2024-11-09 16:36:23

是的,可以更短,看看这个

- (void) setupFeature
{   
NSArray *numbers = [NSArray arrayWithObjects: @"01", @"02", @"03",@"04",@"05",@"06", nil];
    position = CGRectMake(7, position.origin.y+20, 72, 72);

    int j=0;
    NSString *pic;
    UIImageView *a_pic;
    NSMutableArray *C_Pic = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<[numbers count]; i++) 
    { 
        UIImageView *picture = [[UIImageView alloc] init];
        [C_Pic addObject:picture];
        [picture release];
        a_pic = [C_Pic objectAtIndex:i];
        pic = [NSString stringWithFormat:@"iconD%@",[numbers objectAtIndex:i]];
        a_pic.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]      pathForResource:pic ofType:@"png"]];
        if(j<4)
        {   

            j++;
        }
        else
        {   
            j=0;
            position = CGRectMake(7, position.origin.y+77, 72, 72);
            NSLog(@"Pic%i position %@",i, NSStringFromCGRect(self.position));

        }
    //No need to repeat this code
             a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
    }
}

Yes it can be shorter see this

- (void) setupFeature
{   
NSArray *numbers = [NSArray arrayWithObjects: @"01", @"02", @"03",@"04",@"05",@"06", nil];
    position = CGRectMake(7, position.origin.y+20, 72, 72);

    int j=0;
    NSString *pic;
    UIImageView *a_pic;
    NSMutableArray *C_Pic = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<[numbers count]; i++) 
    { 
        UIImageView *picture = [[UIImageView alloc] init];
        [C_Pic addObject:picture];
        [picture release];
        a_pic = [C_Pic objectAtIndex:i];
        pic = [NSString stringWithFormat:@"iconD%@",[numbers objectAtIndex:i]];
        a_pic.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]      pathForResource:pic ofType:@"png"]];
        if(j<4)
        {   

            j++;
        }
        else
        {   
            j=0;
            position = CGRectMake(7, position.origin.y+77, 72, 72);
            NSLog(@"Pic%i position %@",i, NSStringFromCGRect(self.position));

        }
    //No need to repeat this code
             a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文