0

According to this question, the UIGestureRecognizer has a view property which refers to the view the gesture is attached to. I used this in my code like this:

//Code for the 1st UIScrollView
UIImageView *bookCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(downloadBookTapped:)];
[bookCover addGestureRecognizer:singleTap];

[bookCover release];
[singleTap release];

//Code for the second UIScrollView
UIImageView *fileCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)];
UITapGestureRecognizer *singleFileTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(downloadFileTapped:)];
[fileCover addGestureRecognizer:singleFileTap];

[fileCover release];
[singleFileTap release];

And here is where I user the view property:

- (void)downloadBookTapped:(UITapGestureRecognizer *)sender
{
  UIImageView *imgView = (UIImageView *)sender.view;

  CGRect rect = [imgView frame];

  UIImageView *images = [[UIImageView alloc]initWithFrame:rect];

  //rest of code here...
}

- (void)downloadFileTapped:(UITapGestureRecognizer *)sender
{
  UIImageView *imgView = (UIImageView *)sender.view;

  CGRect rect = [imgView frame];

  UIImageView *images = [[UIImageView alloc]initWithFrame:rect];

  //rest of code here...
}

The problem here is that I have two scrollView and each scrollview holds multiple books. When I select a book at the 1st scrollView, the images is displayed correctly. But when I select a book inside the 2nd scrollView, the images is displayed incorrectly. Can anyone explain why this happens? Thanks.

---ADDITIONAL INFO---

The two scrollViews have the same width and height. The difference, of course, is there placement. The first scrollView is placed at (0, 0), while the second is at (0, 350). You can imagine the two as "shelves", the first one being the top shelf and the second one being the bottom shelf.

To specify the problem, say that I selected a book inside the second scrollView. The images will then be displayed as if I selected a book in the 1st scrollView. Meaning, the images is displayed in the 1st scrollView instead of the second scrollView.

Community
  • 1
  • 1
Anna Fortuna
  • 1,071
  • 2
  • 17
  • 33

2 Answers2

1

Because the gestureRecognizer is bound to the first UIImageView and not the second.

[bookCover addGestureRecognizer:singleTap];

Do this for your other UIImageView and you will get the results you want.

deleted_user
  • 3,817
  • 1
  • 18
  • 27
0

I know now what I did wrong! Instead of adding the images as the subview of the scrollViews, I did this:

[self.view addSubView:images];

That's why It keeps appearing on the top side. It should be like this:

[scrollBook addSubview:images];
[scrollFile addSubView:files];
Anna Fortuna
  • 1,071
  • 2
  • 17
  • 33