我正在做一个简单的图片库,其中包含一个列出我的图片的集合视图。图像名称存储在一个数组中。当我按下图像时,它会在带有图像视图的视图控制器中显示该图片。
现在我希望能够通过滑动从一个图像切换到下一个和上一个图像,而不是返回到集合视图来更改图片。我是否选择了错误的方式来实现我的项目,或者是否有一种简单的方法可以做到这一点?
已经感谢您的帮助!
尼古拉斯。
这是我的实际代码:
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