使用iphone中的UIScrollView滚动图像(第2部分)

时间:2012-10-24 08:56:22

标签: iphone ios uiscrollview scrollview

我是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;

@end

viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize imgView,scrollView;

- (void)viewDidLoad
{
    NSArray *images = [[NSArray alloc] initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg", nil];
    for (int i = 0; i < images.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        imgView = [images objectAtIndex:i];
        imgView.frame = frame;
        [scrollView addSubview:imgView];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);

    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.scrollView = nil;
}



@end

我仍无法滚动图片。不确定我的错误在哪里?而且,它现在给了我这个例外:

例外:

[__NSCFConstantString setFrame:]: unrecognized selector sent to instance 0x49b8
2012-10-24 16:49:42.411 SwipeImages[3330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setFrame:]: unrecognized selector sent to instance 0x49b8'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1d184bd 0x1c7cbbc 0x1c7c94e 0x2718 0xf3817 0xf3882 0x42a25 0x42dbf 0x42f55 0x4bf67 0x213b 0xf7b7 0xfda7 0x10fab 0x22315 0x2324b 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x107da 0x1265c 0x1d7d 0x1ca5)
libc++abi.dylib: terminate called throwing an exception

1 个答案:

答案 0 :(得分:1)

您将字符串放入数组放置图像对象中。即。

NSArray *images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpeg"],[UIImage imageNamed:@"2.jpeg"],[UIImage imageNamed:@"3.jpeg"], nil];

这里也是一个建议。使用ping图像而不是jpeg。当你这样做时告诉我。

编辑:

另一件事在这里

imgView = [images objectAtIndex:i];

您正在将图像传递给imageview对象,该对象实际上现在变为图像对象。并且图像没有框架属性。这样做

 imgView.image = [images objectAtIndex:i];