简体   繁体   English

Scrollview不加载其他图像

[英]Scrollview not loading additional Images

I have a UIView that I am using as a simple onboarding view. 我有一个UIView用作简单的入门视图。 I simply shows n images, that the user can swipe through. 我只显示了n张图像,用户可以在其中滑动。

The only image that loads is the very first image "OnBoard-1". 唯一加载的图像是第一个图像“ OnBoard-1”。 The other images are there when I debug the what is being added to the image view. 当我调试添加到图像视图的内容时,其他图像在那里。

在此处输入图片说明

What am I doing wrong? 我究竟做错了什么?

.h 。H

#import <UIKit/UIKit.h>
@interface OnBoardingView : UIView 
- (void)setImages:(NSArray *)newImages;
@end

Here is the .m file 这是.m文件

#import "OnBoardingView.h"

@interface OnBoardingView () <UIScrollViewDelegate>
{
    UIPageControl *pageControl;
    NSArray *contentImages;
}
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSArray       *contentImages;
@end



@implementation OnBoardingView


@synthesize pageControl;
@synthesize contentImages;


- (id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) { }
    return self;
}

#pragma mark - Override contentImages setter

- (void)setImages:(NSArray *)newImages {
    if (newImages != self.contentImages) {
        self.contentImages = newImages;
        [self setup];
    }
}

#pragma mark - Carousel setup

- (void)setup {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    [scrollView setDelegate:self];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setPagingEnabled:YES];
    [scrollView setBounces:NO];

    CGSize scrollViewSize = scrollView.frame.size;

    for (NSInteger i = 0; i < [self.contentImages count]; i++) {
        CGRect slideRect = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width, scrollViewSize.height);

        UIView *slide = [[UIView alloc] initWithFrame:slideRect];
        [slide setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
        [imageView setImage:[UIImage imageNamed:[self.contentImages objectAtIndex:i]]];
        NSLog(@"Image named: %@", [self.contentImages objectAtIndex:i]);
        [slide addSubview:imageView];

        [scrollView addSubview:slide];

    }

    UIPageControl *tempPageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollViewSize.height - 20, scrollViewSize.width, 20)];
    [self setPageControl:tempPageControll];

    [self.pageControl setNumberOfPages:[self.contentImages count]];
    [scrollView setContentSize:CGSizeMake(scrollViewSize.width * [self.contentImages count], scrollViewSize.height)];

    [self addSubview:scrollView];

    [self addSubview:self.pageControl];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    [self.pageControl setCurrentPage:page];
}

@end

You initialise the imageView with the frame of the scrollView , that's in any case not right and may be the cause of your problem. 您初始化imageView与的框架scrollView ,这不是正确的是在任何情况下,可能是你的问题的原因。

BTW: 顺便说一句:

Your property handling looks a bit strange (why the synthesizing instead of just using a normal property only?), and why do you compare the arrays by pointer ( newImages != self.contentImages )? 您的属性处理看起来有些奇怪(为什么要合成而不是仅使用常规属性?),为什么newImages != self.contentImages按指针比较数组( newImages != self.contentImages )?

如果要直接在分页中显示图像,则可以分配页数计数,然后将滑动手势(左右)应用于imageview。基于左右滑动,可以更改imageview的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM