在视图控制器中滑动的collectionview

时间:2013-05-13 11:22:05

标签: iphone objective-c

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

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

谢谢你的帮助!

尼古拉斯。

这是我的实际代码:

PhotosCollectionViewController.h

import

@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

2 个答案:

答案 0 :(得分:0)

你做得对。通过图像(或网址)数组以及下一个和上一个按钮动作(或手势识别器),从数组中加载图像并显示在视图控制器中。

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

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

现在你有了FinishImageVC中的数组,现在开启了,当下一个图像需要动作时,只需从数组中获取图像nd load

答案 1 :(得分:0)

只需为您的UIImageView启用用户互动,为UISwipeGestureRecognizer添加配置方向的UIImageView,然后在您设置的选择器中更改{{1}}中的图片手势识别器。