Scrollview无法加载其他图像

时间:2015-12-17 22:10:17

标签: ios objective-c uiview uiscrollview

我有一个UIView,我正在使用它作为一个简单的入门视图。我只是显示用户可以滑动的n张图片。

加载的唯一图像是第一张图像“OnBoard-1”。当我调试添加到图像视图的内容时,其他图像就在那里。

enter image description here

我做错了什么?

·H

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

这是.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

2 个答案:

答案 0 :(得分:-1)

您使用user.rb:2: warning: method redefined; discarding old initialize user.rb:3: warning: previous definition of initialize was here 的框架初始化imageView,这在任何情况下都不正确,可能是您遇到问题的原因。

BTW:

您的属性处理看起来有点奇怪(为什么要合成而不仅仅使用普通属性?),为什么要用指针(scrollView)比较数组?

答案 1 :(得分:-1)

如果要直接在分页中显示图像,可以指定页数。将滑动手势(左右)应用于imageview。根据左右滑动,可以更改imageview的图像