在 UICollectionView 中处理点击手势 [英] Handling Tap Gesture in a UICollectionView

查看:26
本文介绍了在 UICollectionView 中处理点击手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我无法使用任何框架来创建相册,因此我尝试使用集合视图创建自己的相册,但一开始就卡住了.

since I couldn't use any framework to create an photo album, I'm trying to create my own using Collection View, but I got stuck right at the beginning.

我的目标是将来自我的网络服务的所有图像显示到我的收藏视图中,因为所有图像都显示出来,下一步是当有人点击任何单元格时,我可以在新视图中打开它并在所有图像之间导航.

My goal is to display all images from my web service into my collection view, since all displayed, the next step is when someone taps on any cell, I can open it in a new view and also navigate between all.

这是我创建的基本代码:

here is the basic code that I created:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [collectionController reloadData];
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:nil action:@selector(touched)];

    tapGesture.numberOfTapsRequired = 1;


}

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

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"Cell";

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    [cell.imgCollection addGestureRecognizer:tapGesture];

    return cell;
}

-(void)touched:(UIGestureRecognizer *)tap{

    NSLog(@"the touch happened");
}

谢谢各位.

推荐答案

您的代码中有几处不正确:

A couple of things are not right in your code:

首先initWithTarget:action: 不应为目标传递 nil 值.来自 文档 :

First, initWithTarget:action: should not be passed a nil value for target. From the docs :

目标

一个对象,它是接收者在识别手势时发送的动作消息的接收者.nil 不是有效值.

An object that is the recipient of action messages sent by the receiver when it recognizes a gesture. nil is not a valid value.

在您的情况下,您应该将 self 作为目标传递,因为您想将消息 touched: 发送到类的当前实例.

In your case you should pass self as a target because you want to sent the message touched: to the current instance of your class.

第二,你传递给initWithTarget:action:的选择器是错误的.您使用了 @selector(touched) 但您的方法实现是 - (void)touched:(UIGestureRecognizer *)tap;,其中选择器是 @selector(touched:)(注意 :).

Second, the selector you passed to initWithTarget:action: is wrong. You used @selector(touched) but your method implementation is - (void)touched:(UIGestureRecognizer *)tap;, which selector is @selector(touched:) (mind the :).

如果您感到困惑,我建议您阅读这个关于选择器的问题.

I'd recommend reading this question on selectors if your are confused.

第三,您不能将单个 UIGestureRecognizer 附加到多个视图(请参阅 这个问题).

Third, you cannot attach a single UIGestureRecognizer to multiple view (see this SO question).

因此,要使其工作,您可以为每个集合单元(可能在子类中)创建一个 UITapGestureRecognizer.或者更好的是,实现您的 UICollectionViewDelegate 方法 collectionView:didSelectItemAtIndexPath:.

So to make it work, you could create one UITapGestureRecognizer per collection cell (maybe in a subclass). Or better yet, implement your UICollectionViewDelegate method collectionView:didSelectItemAtIndexPath:.

EDIT - 如何实现 collectionView:didSelectItemAtIndexPath::

EDIT - How to implement collectionView:didSelectItemAtIndexPath::

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Bind the collectionView's delegate to your view controller
    // This could also be set without code, in your storyboard
    self.collectionView.delegate = self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;
}

// I implemented didSelectItemAtIndexPath:, but you could use willSelectItemAtIndexPath: depending on what you intend to do. See the docs of these two methods for the differences.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // If you need to use the touched cell, you can retrieve it like so
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    NSLog(@"touched cell %@ at indexPath %@", cell, indexPath);
}

这篇关于在 UICollectionView 中处理点击手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆