tags:

views:

959

answers:

3

Hi, I want to get notifications when user single tap or double tap on imageView of UITableViewCell. I have been try to add a subview to imageView, and use touchesBegan:withEvent: in subview controller, but can't trigger the event I expected. How can I do the job ?

A: 

In my project I have subclassed UIImageView to add user interaction, if it can be useful you can simply have a look at the Apple Sample Code, project "TapToZoom". In shorts, you have to enable user interaction, and only after that the UIImageView will start to answer to touches. This is done by setting userInteractionEnabled property:

[self setUserInteractionEnabled:YES];

then you can customize other touches property by, for example:

[self setMultipleTouchEnabled:YES];
twoFingerTapIsPossible = YES;

Otherwise, you could be implementing a sort of thumbnails view like the photo app, let's say. Depending on your objective, you can also think about using a UIButton, where you can set a background image. I have a project where I mimic the photo thumbnail view of the original photo app simply by putting programmatically four UIButtons in each tableView row, and it works fine. Cheers

Andy
A: 

Thanks for the answers above, the job has been done.

My solution: 
  subclass a UIView, override touchesBegan:withEvent: in it's implementation file
  add the UIView(as subview) to cell.view and cover the position of imageView, then make the subview nearest transparent(alpha=0.015f)
  done.
  it works fine

Note: if alpha number is too small(0.01f), there will not trigger the touch event, anybody knows why?

bandw
+1  A: 

One more solution:

Add a category to UIImageView to handle touch events. And then send out a notification for each touch event and subscribe to notifications in the view controller (or cell controller).

If you are feeling frisky, you could also forward the method calls to the view controller.

Then enable touch events for the imageView of the cell:

[self setUserInteractionEnabled:YES];

Now all touches will be sent where you want to handle them, without subclassing. And it is re-usable.

Corey Floyd
I tried a couple/few different solutions but this solution worked and was the easiest and cleanest way to do it to me. I could take advantage of the existing UITableViewCell styles to layout the cells (i.e., I didn't have to add subviews to the cell and lay them out), or subclass anything. Works great...thanks!
yabada