• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 如何在 ScrollView 图像中添加背景图像

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:06:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想为 ScrollView 中的每个图像添加背景图像 bgImageView.image = [UIImage imageNamed"frame_trans"];,但是 ScrollView 扩展而不是添加图像每张图片背后,我做错了什么?

编辑:添加了工作代码,感谢 ColdLogic。

这是我的旧 ScrollView

enter image description here

我想添加回形针

enter image description here

但当前代码会这样做。

enter image description here

代码:

//init scrollview in location on screen
//scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 100, 278, 290)];   
//scrollview.backgroundColor = [UIColor redColor];
//pass image filenames
NSMutableArray *fileNames = Info.imageFiles; //[[[NSMutableArray alloc] init] autorelease];
//setup the array of uiimageviews
NSMutableArray *imgArray = [[NSMutableArray alloc] init];
NSMutableArray *bgImgArray = [[NSMutableArray alloc] init];       
//loop through the array imgNames to add file names to imgArray
for (NSString *imageName in fileNames) {
    NSString *tempIMGName = [NSString stringWithFormat"data/%i/%@",Info.ID, imageName];
    NSLog(@"tempIMG %@",tempIMGName);
    UIImageView *imageView = [[UIImageView alloc] init];
    UIImageView *bgImageView = [[UIImageView alloc] init];;
    imageView.image = [UIImage imageNamed:tempIMGName];
    bgImageView.image = [UIImage imageNamed"frame_trans"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    CGAffineTransform newTransform = CGAffineTransformMakeRotation((CGFloat)(-3 * M_PI / 180.0));
    imageView.layer.affineTransform = newTransform;//CGAffineTransformMakeRotation(degreesToRadians(10));
    [imgArray addObject:imageView];
    [bgImgArray addObject:bgImageView];
    [imageView release];
    [bgImageView release];
}

CGSize pageSize = scrollview.frame.size; 

NSUInteger page = 0;

for (UIView *bgViewForScrollView in bgImgArray) {

    [scrollview addSubview:bgViewForScrollView];

    bgViewForScrollView.frame = CGRectMake(pageSize.width * page++ +10, 0, pageSize.width -20 , pageSize.height);


    // making use of the scrollView's frame size (pageSize) so we need to;
    // +10 to left offset of image pos (1/2 the gap)
    // -20 for UIImageView's width (to leave 10 gap at left and right)

}

NSUInteger page1 = 0;
for (UIView *viewForScrollView in imgArray) {

    [scrollview addSubview:viewForScrollView];
    viewForScrollView.frame = CGRectMake(pageSize.width * page1++ +10, 0, pageSize.width -20 , pageSize.height);

    // making use of the scrollView's frame size (pageSize) so we need to;
    // +10 to left offset of image pos (1/2 the gap)
    // -20 for UIImageView's width (to leave 10 gap at left and right)
}

//add scroll view to view
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
scrollview.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);

//scrollview.contentSize = CGSizeMake(320 *viewcount + 20, 290 );
scrollview.showsHorizontalScrollIndicator =NO;
[scrollview setPagingEnabled:YES];
scrollview.delegate =self;



Best Answer-推荐答案


问题是您在为背景 View 创建框架时使用 page++,然后在开始为图像创建框架时没有重置它。 page 将有一个值 3 进入 imgArray 的 for 循环,使其位于第 4 个索引处。

for (UIView *bgViewForScrollView in bgImgArray) {
    //code
}

//ADD THIS LINE OF CODE
page = 0;

for (UIView *viewForScrollView in imgArray) {
    //Code
}

关于iphone - 如何在 ScrollView 图像中添加背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8359253/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap