2

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

@end

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

2 回答 2

1

您正在将字符串放入数组中,将图像对象放入其中。IE

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];
于 2012-10-24T09:00:06.163 回答
0

对于 Swift 2.2...我使用下面的代码使图像(imageView)在 scrollView 内滚动...

scrollView 是 scrollView 的 IBOutlet

 override func viewDidLoad() {
    super.viewDidLoad()

    scrollLeftOrRight()
    // Do any additional setup after loading the view.
}

func scrollLeftOrRight(){
    var i: CGFloat = 0

    for (i = 0 ;i < CGFloat(arrayOfImages.count) ;i++){

        //make frame for the image of imageView
        var frame = CGRect()
        frame.origin.x = self.scrollView.frame.size.width * i
        frame.origin.y = 0
        frame.size = self.scrollView.frame.size

        //make imageView then add imageView as the subView of scrollView
        let imageView = UIImageView(frame: frame)
        imageView.image = UIImage(named: arrayOfImages[Int(i)])
        imageView.contentMode = .ScaleAspectFit
        scrollView.addSubview(imageView)

        scrollView.pagingEnabled = true

}

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(arrayOfImages.count), self.scrollView.frame.size.height)

 }

scrollView.contentSize 行更重要......如果你不正确,那么滚动视图的内容宽度将是只有“一个”图像的宽度......为了让我们所有的图像滚动......

我们必须将scrollView的宽度乘以arrayImage的计数......并且高度将是图像的高度,因为我们不需要垂直滚动图像,对吗?

于 2016-05-12T12:11:34.693 回答