UIScrollView用于图像页面不滚动到下一页

时间:2015-11-10 05:40:17

标签: ios objective-c uiscrollview

我有一个UIImageView,其中包含四个ImageView作为子视图。每个ImageView充当用户可以向前滚动并转到下一页(图像)的页面。但是,我无法实现此滚动到下一页。它只显示第一张图片。这是我的实现

TutorialViewController.h

#import <UIKit/UIKit.h>

@interface TutorialViewController : UIViewController{   
}

@property (nonatomic, retain) IBOutlet UIScrollView *tutorialScrollView;

@end

TutorialViewController.m

#import "TutorialViewController.h"

@interface TutorialViewController () <UIScrollViewDelegate>

@end

@implementation TutorialViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tutorialScrollView.pagingEnabled = YES;
    self.tutorialScrollView.showsHorizontalScrollIndicator = NO;
    self.tutorialScrollView.scrollEnabled = YES;
    self.tutorialScrollView.delegate = self;

}

-(void)viewDidAppear:(BOOL)animated
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    UIImageView *page1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];

    page1.image = [UIImage imageNamed:@"tutorial1.png"];

    UIImageView *page2 = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth * 2,0,screenWidth,screenHeight)];
    page2.image = [UIImage imageNamed:@"tutorial2.png"];


    UIImageView *page3 = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth * 3,0,screenWidth,                                                        screenHeight)];
    page3.image = [UIImage imageNamed:@"tutorial3.png"];


    UIImageView *page4 = [[UIImageView alloc] initWithFrame:CGRectMake(screenWidth * 4,0,screenWidth,                                                                         screenHeight)];
    page4.image = [UIImage imageNamed:@"tutorial4.png"];

    [self.tutorialScrollView addSubview:page1];
    [self.tutorialScrollView addSubview:page2];
    [self.tutorialScrollView addSubview:page3];
    [self.tutorialScrollView addSubview:page4];
}

@end

2 个答案:

答案 0 :(得分:0)

您应为contentSize设置UIScrollView

[self.tutorialScrollView setContentSize:CGSizeMake(screenWidth*numOfPages, screenHeight)];

答案 1 :(得分:0)

您需要设置ContentSize的{​​{1}}。

scrollView

这将在水平方向滚动self.tutorialScrollView.contentSize = CGSizeMake(imageView.frame.size.width * numberOfImages, self.tutorialScrollView.frame.size.height);