在iphone中使用UIScrollView滚动图像

时间:2012-10-24 07:16:17

标签: iphone ios uiscrollview

我是iphone开发的新手,我正在做一个简单的练习应用程序。在该应用程序中,只有一个视图上有图像。我希望能够向左或向右滑动,让第一张图像离开视图,第二张图像进入。图像不同,我希望它看起来像连接。我需要它能够更改图像或在一系列滑动后重新启动。我这样做只使用ScrollView。

我尝试了一些东西:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
    UIImageView *imgView;
    UIScrollView *scrollView;
}

@property (nonatomic,retain)IBOutlet UIImageView *imgView;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;

//- (id)initWithPageNumber:(int)page;
- (IBAction)changePage:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize imgView,scrollView;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
- (id)initWithPageNumber:(int)page{
    if (self = [super initWithNibName:@"MyView" bundle:nil])
    {
        pageNumber = page;
    }
    return self;
}
*/



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview
{
    scrollView.contentOffset = CGPointMake(scrollview.bounds.size.width, 0);
}

- (IBAction)changePage:(id)sender
{

    NSArray *images = [[NSArray alloc] initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg", nil];
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];
    CGRect cRect = scrollView.bounds;

    for (int i = 0; i < images.count; i++){
        imgView = [images objectAtIndex:i];
        imgView.frame = cRect;
        [scrollView addSubview:imgView];
        cRect.origin.x += cRect.size.width;
    }

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * images.count, scrollView.bounds.size.height);
    scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);

}

@end

当我运行应用程序时,我仍然无法扫描图像。难道我做错了什么?需要一些指导。感谢..

2 个答案:

答案 0 :(得分:1)

此错误表示您在Interface Builder中与视图控制器类中不存在的IBOutlet建立了连接。

确保您的IBOutlet命名正确,并且已删除IB中的所有过时连接。

答案 1 :(得分:0)

scrollView.ContentSize =(scrollView.frame.size.width * images.count

,scrollView.frame.size.height)

在砍掉之后执行此操作会滑动