0

可能重复:
使用 CGPoint 跟踪长按手势的确切位置

它总是给NSLog图像按下0。我不明白我做错了什么。它从来没有给我我正在按下的图像的编号。即使当我尝试保存图像时,我点击它总是会保存最后一张图像。

 - (void)viewDidLoad

{

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {

    CGFloat xOrigin = i * self.view.frame.size.width;

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

    myButton.frame = CGRectMake(xOrigin, 10, 60, 35);


    [myButton.layer setMasksToBounds:YES];

    [myButton.layer setCornerRadius:10.0f];

    myButton.layer.borderWidth = 2;

    myButton.layer.borderColor = [[UIColor whiteColor] CGColor];

    [myButton setTitle:@"Done" forState:UIControlStateNormal];

    myButton.backgroundColor = [UIColor clearColor];

    NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];

    _image = [UIImage imageNamed:imageName];

    _imageView = [[[UIImageView alloc] initWithImage:_image]autorelease];

    _imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);




    UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
                                                       initWithTarget:self
                                                       action:@selector(handleLongPress:)];

    imageScrollView.userInteractionEnabled = YES;

    [imageScrollView addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.delegate = self;
    [gestureRecognizer release];

    [imageScrollView addSubview:_imageView];

    [imageScrollView addSubview:myButton];

}

imageScrollView.contentSize = CGSizeMake(_imageView.frame.size.width * 61 , _imageView.frame.size.height);

[self.view addSubview:imageScrollView];

 }

- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{

if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
   [actionSheet showInView:self.view];
    [actionSheet release];

}}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0:

        [self performSelector:@selector(LongPress:) withObject:nil];

       break;

    default:
        break;

}}


 - (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{


UIView *view  = gestureRecognizer.view;
int index = view.tag;

//UIImageWriteToSavedPhotosAlbum(index, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
  NSLog(@"image pressed %i", index);          
   }
4

1 回答 1

1

因为你从来没有为视图设置标签,所以它默认为 0。说真的,你会问多少次同样的问题?你已经得到了很多很好的反馈,使用它。

  1. 用 CGPoint 追踪长按手势的准确位置
  2. 滚动视图中的内容偏移
  3. 找出滚动视图当前在哪个图像上
  4. 检测用户在 scrollviewdidscroll 中查看的图像对象
于 2012-10-21T01:14:25.257 回答