0

Possible Duplicate:
Tracing the exact location of the longpressgesture with CGPoint

It always gives NSLog for image pressed 0. I'm don't understand what it is exactly what I'm doing wrong. It never gives me the number of the image I'm pressing. Even when I try to save the image I tap on it always saves the very last image.

 - (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);          
   }
Community
  • 1
  • 1
user1452248
  • 767
  • 2
  • 11
  • 28

1 Answers1

1

Because you never set the tag for the view so it defaults to 0. And seriously, how many times are you going to ask the same question? You've been given quite a lot of good feedback, use it.

  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
Community
  • 1
  • 1
Adis
  • 4,512
  • 2
  • 33
  • 40