从左到右移动数组图像

时间:2014-04-17 13:08:01

标签: iphone objective-c ios7

我有一个八个阵列图像和两个按钮左按钮,右按钮。当我单击右键时,它应该移动到下一个图像。单击左键移动到上一个图像。

UIImage *image1 = [UIImage imageNamed:@"a1.png"];
UIImage *image2 = [UIImage imageNamed:@"a7.png"];
UIImage *image3 = [UIImage imageNamed:@"a8.png"];
UIImage *image4 = [UIImage imageNamed:@"a3.png"];
UIImage *image5 = [UIImage imageNamed:@"a6.png"];
UIImage *image6 = [UIImage imageNamed:@"a2.png"];
UIImage *image7 = [UIImage imageNamed:@"a5.png"];
UIImage *image8 = [UIImage imageNamed:@"a9.png"];


NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,nil];


 numberOfViews = 8;
for (int i = 0; i < numberOfViews; i++) {

      [scroll setContentOffset:CGPointMake(numberOfViews * 31,0) animated:YES];
}

以上编码我只是在尝试。当我单击按钮时,只有一个图像在移动。我该怎么办。有人帮我。

3 个答案:

答案 0 :(得分:0)

你在使用故事板吗?如果我理解你在问什么。我想说只需创建一个名为displayImage的uiimageview,以及两个按钮都可以访问的int count = 0。在应用程序启动时将它加载到图像[0]处。然后,当单击右键时,如果它是&lt; = images.length,则int count +1。然后将图像[count]加载到displayImage。然后,当单击左键时,如果它是&gt; = 0,则int count -1然后将images [count]加载到displayImage。然后只对所有八个图像执行此操作。如果你没有得到它让我知道,我可以为你做代码。

答案 1 :(得分:0)

试试这个。

采取全局int

在左按钮方法中,写i - ;

在右键按钮方法中,编写i ++;

if(i<0){

     i = 0;

   yourImageView.image = [UIImage imageNamed:[yourImagesArray objectAtIndex:i]];
}
else if(i>yourImagesArray.count)
{
       i = youImagesArray.count;

    yourImageView.image = [UIImage imageNamed:[yourImagesArray objectAtIndex:i]];

}
 else 
 {
    yourImageView.image = [UIImage imageNamed:[yourImagesArray objectAtIndex:i]];

 }

答案 2 :(得分:0)

将属性添加到受控制的视图中:

@property (nonatomic, strong) UIScrollView *sv;

然后在viewDidLoad中,以下代码创建一个滚动视图并向其添加9个图像,如您的示例中所示。它还在滚动视图下方添加了两个按钮,以转到上一个/下一个图像:

CGFloat width = 290;
CGFloat height = 290;
NSInteger imageCount = 9;

self.sv = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, width, height)];
self.sv.backgroundColor = [UIColor redColor];
self.sv.pagingEnabled = YES;
self.sv.contentSize = CGSizeMake(width*imageCount, height);
[self.view addSubview:self.sv];

for (int i = 1; i <= imageCount; i++) {
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"a%d.png", i]]];
    imgView.backgroundColor = [UIColor blueColor];
    CGRect iFrame = imgView.frame;
    iFrame.origin.x = i * width;
    iFrame.origin.y = 0;
    iFrame.size.width = self.sv.frame.size.width;
    iFrame.size.height = self.sv.frame.size.height;
    imgView.frame = iFrame;
    [self.sv addSubview:imgView];
}


UIButton *previous = [UIButton buttonWithType:UIButtonTypeSystem];
[previous setTitle:@"Previous" forState:UIControlStateNormal];
[previous addTarget:self action:@selector(previousImage:) forControlEvents:UIControlEventTouchUpInside];
[previous sizeToFit];
CGRect pFrame = previous.frame;
pFrame.origin.x = self.sv.frame.origin.x;
pFrame.origin.y = self.sv.frame.origin.y + self.sv.frame.size.height + 10;
previous.frame = pFrame;

UIButton *next = [UIButton buttonWithType:UIButtonTypeSystem];
[next setTitle:@"Next" forState:UIControlStateNormal];
[next addTarget:self action:@selector(nextImage:) forControlEvents:UIControlEventTouchUpInside];
[next sizeToFit];
CGRect nFrame = next.frame;
nFrame.origin.x = self.sv.frame.origin.x + self.sv.frame.size.width - nFrame.size.width;
nFrame.origin.y = self.sv.frame.origin.y + self.sv.frame.size.height + 10;
next.frame = nFrame;

[self.view addSubview:previous];
[self.view addSubview:next];

然后添加以下方法使按钮执行某些操作:

- (void)nextImage:(UIButton *)sender {
    CGPoint p = self.sv.contentOffset;
    if (p.x + self.sv.frame.size.width <= self.sv.contentSize.width) {
        p.x += self.sv.frame.size.width;
        self.sv.contentOffset = p;
    }

}

- (void)previousImage:(UIButton *)sender {
    CGPoint p = self.sv.contentOffset;
    if (p.x - self.sv.frame.size.width >= 0) {
        p.x -= self.sv.frame.size.width;
        self.sv.contentOffset = p;
    }

}