iOS:TapGestureRecognizer问题 [英] iOS: TapGestureRecognizer Issues

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

问题描述

所以我的应用程序就像一个照片库,我正在实现用户删除图像的能力。这是设置:我有9个UIImageViews,imageView,imageView2等。我还有一个编辑按钮和一个tapGesture动作方法。我将Tap Gesture Recognizer拖到我在IB中的视图上,并将其附加到每个UIImageViews上。我还将tapGesture动作方法附加到每个UIImageViews。理想情况下,我希望该方法仅在按下编辑按钮时才变为活动状态。当用户点击编辑,然后点击他们想要删除的图片时,我想要一个UIAlertView出现,询问他们是否确定要删除它。以下是我正在使用的代码:

So I have an app that behaves like a photo gallery and I'm implementing the ability for the user to delete the images. Here is the setup: I have 9 UIImageViews, imageView, imageView2, etc. I also have an "Edit" button, and a tapGesture action method. I dragged a Tap Gesture Recognizer over onto my view in IB, and attached it to each one of the UIImageViews. I also attached the tapGesture action method to each of the UIImageViews. Ideally, I would like the method to only become active when the "Edit" button is pressed. When the user taps Edit, then taps on the picture they want to delete, I would like a UIAlertView to appear, asking if they are sure they want to delete it. Here is the code I'm using:

- (IBAction)editButtonPressed:(id)sender {
    editButton.hidden = YES;
    backToGalleryButton.hidden = NO;
    tapToDeleteLabel.hidden = NO;
}

- (IBAction)tapGesture:(UITapGestureRecognizer*)gesture
{

    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    if (buttonIndex != [alertView cancelButtonIndex]) {

        UIImageView *view = [self.UIImageView];
        if (view) {
            [self.array removeObject:view];
        }


        CGPoint tapLocation = [gesture locationInView: self.view];
        for (UIImageView *imageView in self.view.subviews) {
            if (CGRectContainsPoint(self.UIImageView.frame, tapLocation)) {
                ((UIImageView *)[self.view]).image =nil;
 }

}
        [self.user setObject:self.array forKey:@"images"];
}
}

此代码显然有很多错误:
在此行上使用未声明的标识符按钮索引: if(buttonIndex!= [alertView cancelButtonIndex])

This code is obviously riddled with errors: "Use of undeclared identifier button index" on this line: if (buttonIndex != [alertView cancelButtonIndex])

在类型PhotoViewController的对象上找不到属性UIImageView在这一行 UIImageView * view = [self.UIImageView];

"Property UIImageView not found on object of type PhotoViewController" on this line UIImageView *view = [self.UIImageView];

此行上的预期标识符((UIImageView *)[self.view])。image = nil;

我对编程非常陌生,我很惊讶我甚至做到了这一点。所以,我只是想弄清楚我需要如何编辑我的代码以便错误消失,并且只要轻触9个图像视图中的一个就可以使用它,并且这样方法只会在首先按下编辑按钮。我之前使用的是标签,效果很好,但我通过NSData保存图像,所以我不能再使用标签了。非常感谢任何帮助,谢谢!

I'm very new to programming, and I'm surprised that I even made it this far. So, I'm just trying to figure out how I need to edit my code so that the errors go away, and that it can be used whenever one of the 9 image views is tapped, and also so that this method only fires when the Edit button is pushed first. I was using tags earlier, and it worked great, but I save the images via NSData, so I can't use tags anymore. Any help is much appreciated, thanks!

推荐答案

首先,您不希望将点击手势附加到图像视图。此外,如果要包含9个以上的图像,则可能需要滚动视图或单独处理滚动。首先,删除手势识别器及其所有连接。

First, you don't want to attach the tap gesture to the image views. Also, if you are going to have more than 9 images, you may want a scroll view, or handle scrolling separately. First, remove that gesture recognizer and all its connections.

接下来,确定将用作图库画布的视图类型。一个简单的视图,或ScrollView,或者其他......它现在并不重要,只是让它工作。您想要将该视图拖动到您的界面定义中,因此它会删除视图的IBOutlet(这样您就可以在代码中引用它)。

Next, determine what type of view you will use as your gallery canvas. A simple View, or a ScrollView, or whatever... it doesn't really matter right now, just to get it working. You want to ctrl-drag that view into your interface definition, so it drops an IBOutlet for the view (that way you can reference it in code).

您将放置你的ImageViews我刚刚提到的视图。

You will place your ImageViews onto the view I just mentioned.

你可以为手势识别器设置一个内部标志,但它也有一个使用的属性可以启用/禁用它想。因此,您可以非常轻松地使其处于活动状态/非活动状态。

You can have an internal flag for the gesture recognizer, but it also has a property that use can enable/disable it whenever you want. Thus, you can have it active/inactive fairly easily.

您要做的就是将一个单击手势识别器放到控制器上,然后将其连接到控制器上。控制器的实现部分。它将生成用于处理识别器的存根。它将为控制器一般解释点击,并在视图上点击时调用您的代码。

All you want to do is drop a single tap-gesture-recognizer onto your controller, and connect it to the implementation section of the controller. It will generate a stub for handling the recognizer. It will interpret taps "generally" for the controller, and call your code whenever a tap is made on the view.

更多代码......

Some more code...

在滚动视图中为新图像创建一个框架。

Creates a frame for the "new" image in the scroll view.

- (CGRect)frameForData:(MyData*)data atIndex:(NSUInteger)idx
{
    CGPoint topLeft;
    int row = idx / 4;
    int col = idx % 4;
    topLeft.x = (col+1)*HORIZ_SPACING + THUMBNAIL_WIDTH * (col);
    topLeft.y = (row+1)*VERT_SPACING + THUMBNAIL_HEIGHT * (row);
    return CGRectMake(topLeft.x, topLeft.y, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
}

为每个元数据和一个小边框创建一个图像视图。 / p>

Creates an image view for each piece of metadata, and a small border.

- (UIImageView*)createImageViewFor:(MetaData*)metadata
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:metadata.lastImage];
    imageView.frame = CGRectMake(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);;
    imageView.layer.borderColor = [[UIColor blackColor] CGColor];
    imageView.layer.borderWidth = 2.0;
    imageView.userInteractionEnabled = YES;
    return imageView;
}

这是视图的创建位置并添加到父级...

This is where the views are created and added to the parent...

    imageView = [self createImageViewFor:metadata];
    //[data.imageView sizeToFit];

    // Make sure the scrollView contentSize represents the data
    CGRect lastFrame = [self frameForData:data atIndex:self.data.count-1];
    CGFloat newHeight = lastFrame.origin.y + lastFrame.size.height;
    if (self.bookshelfScrollView.contentSize.height < newHeight) {
        CGSize newSize = self.bookshelfScrollView.contentSize;
        newSize.height = newHeight;
        self.bookshelfScrollView.contentSize = newSize;
    }
    [self.bookshelfScrollView addSubview:data.imageView];

所以,你只需创建每一帧,将它们添加到视图中,并且你唯一拥有的就是do是启用用户交互,因为否则滚动视图不允许手势通过。

So, you just create each frame, add them to the view, and the only thing you have to do is enable user interaction on them, because otherwise the scroll view does not allow the gesture through.

确定...查看您发布的代码...因为你没有说出来有什么问题......很难说......下面是你的代码...我的评论是,好吧,评论......

OK... Looking at the code you posted... since you didn't say what was wrong with it... hard to say... The below is your code... My comments are, well, Comments...

- (IBAction)editButtonPressed:(id)sender {
    editButton.hidden = YES;
    backToGalleryButton.hidden = NO;
    tapToDeleteLabel.hidden = NO;
}
- (IBAction)tapGesture:(UITapGestureRecognizer*)gesture
{
    // I don't think I'd do this here, but it shouldn't cause "problems."
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    // In your controller, you have the main view, which is the view
    // on which you added your UIViews.  You need that view.  Add it as an IBOutlet
    // You should know how to do that...  ctrl-drag to the class INTERFACE source.
    // Assuming you name it "galleryView"

    // Take the tap location from the gesture, and make sure it is in the
    // coordinate space of the view.  Loop through all the imageViews and
    // find the one that contains the point where the finger was taped.
    // Then, "remove" that one from its superview...
    CGPoint tapLocation = [gesture locationInView: self.galleryView];
    for (UIImageView *imageView in self.galleryView.subviews) {
        if (CGRectContainsPoint(imageView.frame, tapLocation)) {
            [imageView removeFromSuperview];
        }
    }
}

这篇关于iOS:TapGestureRecognizer问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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