Reputation: 129
I have a UITableView with Custom Cells, when you touch the cell it adds a subview of a UIImageView.
I want the image to disappear after the user lifts there finger and i have a touchesEnded method on the UIImageVIew Subview but its never called.
It is only called if you lift your finger then press it down again and release it.
How do I get the method to be called on the original touch ended.
Im kinda going for what Snapchat does when you view images.
Upvotes: 2
Views: 223
Reputation: 726479
The reason the subview does not get the touchesEnded
event is that it has not received the touchesBegan
event: these two come in pairs - whichever view gets the touches began is going to get the touches ended. Your UIImageView
could not get touchesEnded
because it wasn't there at the time; it gets touchesEnded
the second time around when you press down and release because it's there for both events.
There are several ways around this problem:
UIImageView
- when you add the subview, store a __weak
reference to it in a separate variable. When the view that added the UIImageView
gets the touchesEnded
event, go to that variable, and remove subview.UIImageView
there, but control its transparency - rather than adding and removing the subvuew, start it as fully transparent, then make it opaque on touch, then make it transparent again on release.UIImageView
at all, use CALayer
instead - it looks like you are adding the image view simply to host an image in it for a short time. There is a simpler way of doing it that's much lighter-way - using CALayer
. This approach should be easier to implement, because the layer does not participate in handling of touch events.Upvotes: 3
Reputation: 939
When you touch that view and raise your finger, you should not drag your finger. If you did like that, then touchesCancelled: method will get called. So I think your view is too small to touch. If yes, then make a big View and try it again. It will work for you then.
Upvotes: 0
Reputation: 4373
Presumably the touchBegan method is on the Cell. That's when the subview gets added.
The touch has already been recorded by the Cell. The Cell's touchesEnded is what's going to be called when you lift up your finger. So that's where you need to handle removing the subview from the screen. Save a reference to the subview in the cell class, and if it is not equal to nil
, remove it on touchesEnded. Simple as that.
The touch began on the Cell, before the subview existed. That same touch is going to end on the cell. You can't transfer the touch to another view while it's in progress.
Upvotes: 0