菜鸟教程小白 发表于 2022-12-13 10:06:08

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


                                            <p><p>我想为 ScrollView 中的每个图像添加背景图像 <code>bgImageView.image = ;</code>,但是 ScrollView 扩展而不是添加图像每张图片背后,我做错了什么?</p>

<p>编辑:添加了工作代码,感谢 ColdLogic。</p>

<p>这是我的旧 ScrollView </p>

<p> <img src="/image/t4mns.png" alt="enter image description here"/> </p>

<p>我想添加回形针</p>

<p> <img src="/image/kB1u2.png" alt="enter image description here"/> </p>

<p>但当前代码会这样做。</p>

<p> <img src="/image/350t9.png" alt="enter image description here"/> </p>

<p>代码:</p>

<pre><code>//init scrollview in location on screen
//scrollview = [ initWithFrame:CGRectMake(30, 100, 278, 290)];   
//scrollview.backgroundColor = ;
//pass image filenames
NSMutableArray *fileNames = Info.imageFiles; //[[ init] autorelease];
//setup the array of uiimageviews
NSMutableArray *imgArray = [ init];
NSMutableArray *bgImgArray = [ init];      
//loop through the array imgNames to add file names to imgArray
for (NSString *imageName in fileNames) {
    NSString *tempIMGName = ;
    NSLog(@&#34;tempIMG %@&#34;,tempIMGName);
    UIImageView *imageView = [ init];
    UIImageView *bgImageView = [ init];;
    imageView.image = ;
    bgImageView.image = ;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    CGAffineTransform newTransform = CGAffineTransformMakeRotation((CGFloat)(-3 * M_PI / 180.0));
    imageView.layer.affineTransform = newTransform;//CGAffineTransformMakeRotation(degreesToRadians(10));
    ;
    ;
    ;
    ;
}

CGSize pageSize = scrollview.frame.size;

NSUInteger page = 0;

for (UIView *bgViewForScrollView in bgImgArray) {

    ;

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


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

}

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

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

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

//add scroll view to view
;
;
scrollview.contentSize = CGSizeMake(pageSize.width * , pageSize.height);

//scrollview.contentSize = CGSizeMake(320 *viewcount + 20, 290 );
scrollview.showsHorizontalScrollIndicator =NO;
;
scrollview.delegate =self;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题是您在为背景 View 创建框架时使用 <code>page++</code>,然后在开始为图像创建框架时没有重置它。 page 将有一个值 3 进入 imgArray 的 for 循环,使其位于第 4 个索引处。</p>

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

//ADD THIS LINE OF CODE
page = 0;

for (UIView *viewForScrollView in imgArray) {
    //Code
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 如何在 ScrollView 图像中添加背景图像,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8359253/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8359253/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 如何在 ScrollView 图像中添加背景图像