1

我正在做一个简单的图片库,其中包含一个列出我的图片的集合视图。图像名称存储在一个数组中。当我按下图像时,它会在带有图像视图的视图控制器中显示该图片。

现在我希望能够通过滑动从一个图像切换到下一个和上一个图像,而不是返回到集合视图来更改图片。我是否选择了错误的方式来实现我的项目,或者是否有一种简单的方法可以做到这一点?

已经感谢您的帮助!

尼古拉斯。

这是我的实际代码:

PhotosCollectionViewController.h

进口

@interface PhotosCollectionViewController : UICollectionViewController
{
    NSArray *photosArray;
}
@end

PhotosCollectionViewController.m

#import "PhotosCollectionViewController.h"
#import "PhotoDetailViewController.h"

@interface PhotosCollectionViewController ()

@end

@implementation PhotosCollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    photosArray = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"egg_benedict.jpg", @"full_breakfast.jpg", @"green_tea.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"starbucks_coffee.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", @"white_chocolate_donut.jpg", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return photosArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *photosImageView = (UIImageView *)[cell viewWithTag:100];
    photosImageView.image = [UIImage imageNamed:[photosArray objectAtIndex:indexPath.row]];

    return cell;

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushPhoto"]) {
        NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
        PhotoDetailViewController *destViewController = segue.destinationViewController;
        NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
        destViewController.photoName = [photosArray objectAtIndex:indexPath.row];
    }
}

@end

PhotoDetailViewController.h

#import <UIKit/UIKit.h>

@interface PhotoDetailViewController : UIViewController


@property (strong, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) NSString *photoName;
@end

PhotoDetailViewController.m

#import "PhotoDetailViewController.h"

@interface PhotoDetailViewController ()

@end

@implementation PhotoDetailViewController
@synthesize photoImageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.photoImageView.image = [UIImage imageNamed:self.photoName];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

2 回答 2

0

你做对了。传递图像数组(或 url)并在下一个和上一个按钮操作(或手势识别器)上,从数组加载图像并显示在视图控制器中。

在 DetailsedImageVC 类中创建一个数组实例“imageArray”,stackedImages 是图像可用的数组

DetailedImageVC *vc=[[DetailedImageVC alloc]initWithNibName:@"DetailedImageVC" bundle:[NSBundle mainBundle]];
    vc.imageArray=self.stackedImages ;
    [self.navigationController pushViewController:vc animated:YES];

现在您在DetailedImageVC中拥有了数组,现在开始,当下一个图像需要操作时,只需从数组中获取图像并加载

于 2013-05-13T11:25:46.830 回答
0

只需为您的 启用用户交互UIImageView,附加一个UISwipeGestureRecognizer配置为您想要的方向的图像,然后UIImageView在您在手势识别器中设置的选择器中更改图像。

于 2013-05-13T11:25:53.703 回答