为什么它没有检测到我正在点击哪个图像

时间:2012-10-20 23:44:03

标签: iphone ios6 scrollview

  

可能重复:
  Tracing the exact location of the longpressgesture with 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);          
   }

1 个答案:

答案 0 :(得分:1)

因为您从未为视图设置标记,因此默认为0.严重的是,您要问多少次同样的问题?你已经得到了很多很好的反馈,使用它。

  1. Tracing the exact location of the longpressgesture with CGPoint
  2. contentoffset in scrollview
  3. Work out what image the scroll view is currently on
  4. detect image object user is viewing in scrollviewdidscroll