如何轻松设置 uiscroll 上的 uiimageview?

发布于 2024-11-18 00:39:17 字数 976 浏览 3 评论 0原文

我使用以下代码在滚动上添加更多图像

UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:@"images0" ofType:@"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(0, 0, 890, 430)];

UIImageView* imageshow1=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright1 = [[NSBundle mainBundle] pathForResource:@"images1" ofType:@"png"];
imageshow1.image = [UIImage imageWithContentsOfFile:pathright1];
[imageshow1 setFrame:CGRectMake(890, 0, 890, 430)];

UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*2,430)];
scrollViewright.pagingEnabled = YES;
[scrollViewright addSubview:imageshow];
[scrollViewright addSubview:imageshow1];
[self.view addSubview:scrollViewright];

,但是如果我有更多图像,例如我有图像,我应该添加更多代码,那么我可以使用循环(for?)来执行此功能吗?谢谢

i use the following code for adding more images on scroll

UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:@"images0" ofType:@"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(0, 0, 890, 430)];

UIImageView* imageshow1=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright1 = [[NSBundle mainBundle] pathForResource:@"images1" ofType:@"png"];
imageshow1.image = [UIImage imageWithContentsOfFile:pathright1];
[imageshow1 setFrame:CGRectMake(890, 0, 890, 430)];

UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*2,430)];
scrollViewright.pagingEnabled = YES;
[scrollViewright addSubview:imageshow];
[scrollViewright addSubview:imageshow1];
[self.view addSubview:scrollViewright];

but if i have more images,such as i have imagei should add more code ,so can i use loop (for?) to do this function? thanks

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

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

发布评论

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

评论(1

黑凤梨 2024-11-25 00:39:17

尝试这样的事情:

NSUInteger tot = 5;

UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*tot,430)];
scrollViewright.pagingEnabled = YES;

for (NSUInteger i = 0; i < tot; ++i) {
    UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
    NSString* pathright = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"images%d", i + 1] ofType:@"png"];
    imageshow.image = [UIImage imageWithContentsOfFile:pathright];
    [imageshow setFrame:CGRectMake(i * 890, 0, 890 * (i + 1), 430)];

    [scrollViewright addSubview:imageshow];
    [imageshow release];
}

[self.view addSubview:scrollViewright];

Try something like this:

NSUInteger tot = 5;

UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*tot,430)];
scrollViewright.pagingEnabled = YES;

for (NSUInteger i = 0; i < tot; ++i) {
    UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
    NSString* pathright = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"images%d", i + 1] ofType:@"png"];
    imageshow.image = [UIImage imageWithContentsOfFile:pathright];
    [imageshow setFrame:CGRectMake(i * 890, 0, 890 * (i + 1), 430)];

    [scrollViewright addSubview:imageshow];
    [imageshow release];
}

[self.view addSubview:scrollViewright];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文