【问题标题】:collectionview with swipe in view controller在视图控制器中滑动的 collectionview
【发布时间】:2013-05-13 11:22:05
【问题描述】:

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

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

已经感谢您的帮助!

尼古拉斯。

这是我的实际代码:

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

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      • 好的,谢谢。如果我迷路了,我会试试这个并带着我的代码回来。非常感谢。
      • 很抱歉,我有一些事情要处理。我不明白如何通过 NSArray... 你能再帮帮我吗?
      • 如果你使用 seague.tehn 使用 seague 方法在下一个视图控制器中设置值
      • 我可以将选中项的名称发送给视图控制器,并用segue显示对应的图片。但是当我尝试发送一个 NSArray 时,我真的不明白这个过程。对不起,我是菜鸟。我把它放在我的 segue 中:[destViewController setPhotoArray:photosArray];这在我的视图控制器中: photoArray = [[NSArray alloc]initWithArray:photosArray];但它不起作用。感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多