【问题标题】:How to add in an background image in a scroll view image如何在滚动视图图像中添加背景图像
【发布时间】:2011-12-02 16:14:45
【问题描述】:

我想为滚动视图中的每个图像添加背景图像bgImageView.image = [UIImage imageNamed:@"frame_trans"];,但是滚动视图扩展而不是在每个图像后面添加图像,我做错了什么?

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

这是我的旧滚动视图

我想添加回形针

但是当前的代码是这样做的。

代码:

//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;

【问题讨论】:

  • 我不明白你的问题。请尝试澄清。
  • 感谢coldlogic的回复,加图澄清

标签: iphone ios uiscrollview uiimageview


【解决方案1】:

问题是您在为背景视图创建框架时使用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
}

【讨论】:

    【解决方案2】:

    您应该将背景编号图像添加到滚动视图,并为每个图像添加回形针图像的子视图。

    [bgImageView addSubview:imageView];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2012-12-29
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多