gpt4 book ai didi

objective-c - iOS:TapGestureRecognizer 问题

转载 作者:行者123 更新时间:2023-12-01 19:21:44 26 4
gpt4 key购买 nike

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

- (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])
此行 UIImageView *view = [self.UIImageView]; 上的“在 PhotoViewController 类型的对象上找不到属性 UIImageView”

以及此行上的“预期标识符” ((UIImageView *)[self.view]).image =nil;
我对编程很陌生,我很惊讶我能做到这一点。所以,我只是想弄清楚我需要如何编辑我的代码以使错误消失,并且只要点击 9 个 ImageView 之一就可以使用它,并且只有在首先按下编辑按钮。我之前使用标签,效果很好,但是我通过 NSData 保存图像,所以我不能再使用标签了。非常感谢任何帮助,谢谢!

最佳答案

首先,您不想将点击手势附加到 ImageView 。此外,如果您将拥有超过 9 个图像,您可能需要 ScrollView ,或单独处理滚动。首先,删除该手势识别器及其所有连接。

接下来,确定您将用作画廊 Canvas 的 View 类型。一个简单的 View ,或者一个 ScrollView ,或者其他什么......现在并不重要,只是为了让它工作。您想将该 View 按住 ctrl 并拖动到您的界面定义中,因此它会为该 View 放置一个 IBOutlet(这样您就可以在代码中引用它)。

你将把你的 ImageViews 放到我刚才提到的 View 上。

您可以为手势识别器设置一个内部标志,但它也有一个属性,可以随时启用/禁用它。因此,您可以相当轻松地使其处于事件/非事件状态。

您要做的就是将一个轻击手势识别器放到您的 Controller 上,并将其连接到 Controller 的实现部分。它将生成一个用于处理识别器的 stub 。它将“一般”解释 Controller 的点击,并在 View 上进行点击时调用您的代码。

还有一些代码...

在 ScrollView 中为"new"图像创建一个框架。

- (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);
}

为每条元数据创建一个 ImageView 和一个小边框。
- (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;
}

这是创建 View 并将其添加到父级的地方...
    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];

因此,您只需创建每个框架,将它们添加到 View 中,您唯一需要做的就是在它们上启用用户交互,否则 ScrollView 不允许手势通过。

好的......看看你发布的代码......因为你没有说它有什么问题......很难说......下面是你的代码......我的评论是,嗯,评论...... .
- (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];
}
}
}

关于objective-c - iOS:TapGestureRecognizer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10097855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com