0

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

我尝试了一些东西:

视图控制器.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

视图控制器.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

当我运行该应用程序时,我仍然无法在图像上滑动。难道我做错了什么?在这方面需要一些指导。谢谢..

4

2 回答 2

1

此错误表明您在 Interface Builder 中有一个连接,而该连接在IBOutlet您的视图控制器类中不存在。

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

于 2012-10-24T07:30:26.210 回答
0

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

,scrollView.frame.size.height)

修剪后执行此操作它会滑动

于 2012-10-24T07:45:39.540 回答