Question

So, I iterate through a loop and create UIViews which contain UIImageViews (So that I can selectively show any given part). These UIViews are all stored in a UIScrollView.

I add gesture recognizers to the UIViews in the loop where I created them.

When I run the program, only the items initially visible within the UIScrollView have their gestures recognized. If I scroll over to previously hidden items and then tap on them, nothing happens at all (the gesture is never recognized or attempted to be).

Initialization code:

UITapGestureRecognizer* gestRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
  gestRec.delegate = self;

  [imageholder addGestureRecognizer:gestRec];

Code that deals with the gesture:

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
 float count = [self._imageHolders count];
 NSLog(@"handling gesture: %f",count);
 while(count--){
  UIView* object = (UIView*) [self._imageHolders objectAtIndex:count];
 // NSLog(@"Whats going on: %@, %@, %b",object,gestureRecognizer.view, object == gestureRecognizer.view);
  if(object == gestureRecognizer.view){
   object.alpha = .1;
   count = 0;
  }
 // [object release];
 }
}

Any ideas?

---- Update :

I've explored a variety of the available functions in scrollview, UIView and the gesture recognizer and have tried messing with the bounds in case something was cut off that way... Interestingly enough, if there is one item only partially visible and you move it over so it's completely visible, only the portion originally visible will recognize any gestures.

I don't know enough about how the gesture recognizer works within the UIKit architecture to understand this problem. The Apple example for a scrollview with gestures doesn't seem to have this problem, but I can't find any real differences, except that I am nesting my UIImageViews within their own UIViews

Was it helpful?

Solution

I had a similar problem and found it was caused by adding the sub-views to a top-level view then adding that top-level view to the scroll-view. The top-level view had to be sized to the same dimensions as the contentSize (not the bounds) of the scroll-view otherwise it wouldn't pass on touch events to its subviews even when they had scrolled into view.

OTHER TIPS

Try to set the cancelsTouchesInView property to NO.

UITapGestureRecognizer* gestRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
  gestRec.delegate = self;
  **gestRec.cancelsTouchesInView  = NO;**

  [imageholder addGestureRecognizer:gestRec];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top