iPhone - Catching an uiimageview touch events from a custom cell
i would like to build a custom table view cell and put in him a imageview and when the user click the imageview to popup a view on the viewcontroller-and the view will kn开发者_StackOverflow中文版ow which cell the image was pressed.
thx
Use UITapGestureRecognizer
gesture to detect touches on any view inherited from UIView
.
Use as below.....
Add Tap gesture UITapGestureRecognizer
to myImageView
view (type of UIImageView
).
UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTapEvent:)];
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:myTapGesture];
[myTapGesture release];
Implement gestureTapEvent:
method to receive the touch event.
-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture
{
}
UPDATED:
you could access view with you attached the gesture using view
property of UIGestureRecognizer
.
@property(nonatomic, readonly) UIView *view;
So your gestureTapEvent: method should be like as below .
-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture
{
UIImageView* myImageView = (UIImageView*)gesture.view ;
}
You have multiple possibilities for that:
- Use UIGestureRecognizer to add a
UITapGestureRecognizer
on your imageView (I would recommend this solution) - Subclass UIImageView to override
touchesBegan:withEvent:
methods, etc. - Add a UIButton (of type Custom, to avoid the frame of the button to be drawn) over your UIImageView
Example:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClicked:)];
[cell.yourImageView addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
[EDIT]
Then when you implement your imageViewClicked
method, you can get the tapped ImageView using the view
property of the GestureRecognizer. Starting from that, you can for example:
- use the tag of your imageView (if you affected it in your
tableView:cellForRowAtIndexPath:
method) to retrieve the tag and do whatever you want with it (depending on what you affected it to, for example you may have setimageView.tag = indexPath.row
intableView:cellForRowAtIndexPath:
and get that indexPath row back then) - Go thru the superviews of the imageView up to the UITableViewCell, then ask for its indexPath to get it back and do whatever you want with it.
Example:
-(void)imageViewClicked:(UITapGestureRecognizer*)gestRecognizer
{
UIImageView* iv = (UIImageView*)gestRecognizer.view;
NSInteger tag = iv.tag; // then do what you want with this
// or get the cell by going up thru the superviews until you find it
UIView* cellView = iv;
while(cellView && ![cellView isKindOfClass:[UITableViewCell class]])
cellView = cellView.superview; // go up until you find a cell
// Then get its indexPath
UITableViewCell* cell = (UITableViewCell*)cellView;
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
}
Instead of UIImageView you can take custom UIButton and assign same image to button and you will get touchupInside event of that button. Just assign button.tag = indexpath.row so you can get which cell's Button pressed.
-(IBAction)ButtonPressed:(id)sender{
UIButton *btnSelected = sender;
switch(btnSelected.tag){
case: 0
///Cell 0's Button Pressed.
break;
......
......
}
Hope this help.
To add a touch event to a UIImageView use the following format in your .m
UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newTapMethod)];
[_imageButtonName setUserInteractionEnabled:YES];
[_imageButtonName addGestureRecognizer:newTap];
then create the newTapMethod (or what ever you name it)
-(void)newTapMethod{
//...
}
hope that helps.
精彩评论