一个 UILongPressGestureRecognizer 被添加到我的 imageView 中,并带有动作句柄LongPressOnPhotos。最相关的代码如下:
- (IBAction)handleLongPressOnPhotos:(UILongPressGestureRecognizer *)sender
{
self.imageWillBeSaved = (UIImageView *)sender.view; //breakPoint1
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save the photo" otherButtonTitles: @"Go to the Original photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; //breakPoint2
NSLog( @"actionSheet addr when created is %p", actionSheet );//breakPoint3
[actionSheet release];//breakPoint4
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(self.imageWillBeSaved.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
//[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; i have tried to use this method here, but it didn't work.
break;
default:
break;
}
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL)
{
// handle error
}
else
{
// handle ok status
}
}
单击“保存照片”按钮后,操作表将不会被关闭。如果我再次单击该按钮,则操作表将被取消,并且照片会保存两次。代码中有问题吗?提前致谢!
附言。imageView 是 scrollView 的子视图,scrollView 在 tableViewCell中。
- (IBAction)handleLongPressOnPhotos:(UILongPressGestureRecognizer *)sender
{
self.imageWillBeSaved = (UIImageView *)sender.view; //breakPoint1
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save the photo" otherButtonTitles: @"Go to the Original photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; //breakPoint2
NSLog( @"actionSheet addr when created is %p", actionSheet );//breakPoint3
[actionSheet release];//breakPoint4
}
我在“handleLongPressOnPhotos:”方法中设置了两个断点作为breakPoint1和breakPoint1。在 imageView 长按后,我按照代码的步骤进行操作。步骤顺序是:breakPoint1 -> breakPoint2 ->breakPoint1 ->breakPoint2 -> breakPoint3 -> breakPoint4 -> breakPoint3 -> breakPoint4,然后出去。很明显,actionSheet 已经出现了两次,这导致了问题。这很奇怪,我不知道原因并避免这种情况。
问题在另一个问题中解决了 UILongPressGestureRecognizer 在按下时被调用两次
感谢@Laddu、@MichaelDautermann、@sreecharan