My reusable cell contains a view with all its informations (UIImageView, UILabel, etc.) with a frame of 0,0,320,63, named mainView
I also have another subview, added programmatically, with a frame of -160,0,160,63, named leftView, contained in mainView.
I added a UISwipeGestureRecognizer to the cell, so when you swipe, I change the frame of mainView to 160,0,320,63. It works perfectly.
I just have a problem, in leftView, I have some UIImageView with userInteractionEnabled set to YES, with a gesture recognizer on it. But this gesture recognizer is never fired, it still calls the -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.
I looked at this SO answer, but it does not help me.
Would somebody help me with that ?
Thank you !!
EDIT : Here is my gesture recognizer instantiation
// Selector is valid, I checked it out. _viewController too.
UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setImage:someImage];
[imageView setUserInteractionEnabled:YES];
[_leftView addSubview:imageView];
UITapGestureRecognizer * gr = [[UITapGestureRecognizer alloc] initWithTarget:_viewController action:selector];
[imageView addGestureRecognizer:gr];
Without seeing the gestureRecognizer's initialization, it could be a number of things that are the problem. Two main things to look into however:
1.) Have you set the cancelsTouchesInView flag? Normally this would be messing with your tableView touches, but you should look into it though.
2.) Have you verified the subview layout inside of storyboard? Is there something blocking part of your imageView?
-Also you've actually allocated the gestureRecognizer correct?
Have you tried to set table cell selectable to No? in this case your swipe gesture should still work, but table deligate method for cell selection shouldn't.
I figured it out myself.
The problem was because the leftView frame was outside of the mainView frame (-160 in x).
To solve my problem, I changed mainView in storyboard, so that it also starts at -160. And instead of adding leftView at -160 in x, I add it at 0.
Thanks anyway !
Related
I have many SubViews in my UIView, and many of them have UIButtons in them. One of the subviews- _bottomView (Coordinates- (0,519,320,49)) has an error. It does not recognise the click events on the buttons placed inside it.
I tried placing a UIButton covering the entire _bottomView and the click event from that Button (testButton) is not being recognised either.
I tried adding a tapRecogniser to the code and the tap from every point, EXCEPT the points within the _bottomView are recognised. TapRecogniser Code below
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
_bottomView.userInteractionEnabled=true;
-(void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.view];
NSLog(#"got a tap in the region i care about");
}
I tried [self.view addSubview:_bottomView]; and that didn't help either. What could be the issue?
If your view is not receiving touches
Make sure that your UIView has userInteractionEnabled set to YES
Make sure that the view is within the bounds of its superview, and the superview is within the bounds of window.You can print the frames of views using NSLog(#"%#",NSStringFromCGRect(view.frame));
Nothing helped. I had to delete the entire View Controller and redo the whole thing. That obviously solved the problem.
I've searched through other questions similar to this on SO already, and came up empty with a solution to my problem. For some reason, my UIImageView (which I've added to a UIView in storyboard) just won't recognize a tap gesture I've created. User Interaction is enabled, as is multiple touch. My selector method just never gets called.
self.image is my imageView property that I've added onto a UIView.
- (void)viewDidLoad {
[super viewDidLoad];
self.image.image = [UIImage imageNamed:#"mountainWallpaper.png"];
[self gesture];
}
-(void)gesture {
UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(imageTapped:)];
//imageTap.numberOfTapsRequired = 1;
//imageTap.numberOfTouchesRequired = 1;
imageTap.delegate = self;
[self.image addGestureRecognizer:imageTap];
}
-(IBAction3)imageTapped:(UIGestureRecognizer *)tap {
NSLog(#"test");
}
Any suggestions? I've looked through most other questions I found and tried out various solutions with no such luck. Many answers said to verify that user interaction is enabled, to make sure the delegate is set...I've tried much of what has been suggested already.
I've also experimented with first adding the UIView as the tap gesture target, adding the imageView itself as the target, adding the gesture to both the UIView and the imageView...nothing.
In storyboard, I have my UIImageView connected to my viewController as both the view and the image. I generally do things programmatically so I have to get used to connecting objects in IB, I'm pretty bad at it.
Check for following points may be it will helpful for you :
Make sure your UIImageView User interaction is enable .
Image View priority should be on top over super view .
make sure super view of Image view userIntration is also enable .
have you added UI gesture Delegate ?
It looks like you have a spelling mistake. Try changing IBAction3 to IBAction or void:
-(void)imageTapped:(UIGestureRecognizer *)tap {
NSLog(#"test");
}
It may seem like duplicate, but I can't actually find any good answer to my concrete situation.
I have some button with background image (1pt-wide, streched), icon-like image inside along with the text label. It's height is 33pt, I need to make it's hittest area 44pt-high.
I saw two solutions, but neither of them works for me.
First solution is to enlarge frame and adjust image so it would have some padding. But that is not acceptable for me, because I have both background image and image inside the button.
Second solution is to subclass UIButton (which is absolutely acceptable) and override - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event. I did that, when I put some breakpoints in this method and tried to tap in desired area nothing happened (breakpoints worked when tapped inside the button frame).
Are there any other solutions? What could be possibly wrong with the second solution?
If you create the view programmatically, you can instantiate a UIView that is 44px high, add a tap gesture recognizer to it, and add your UIButton as a subview inside this view:
CGRect bigFrame = CGRectMake(0,0,100,44);
UIView *big = [[UIView alloc] initWithFrame:bigFrame];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doPress)];
[big addGestureRecognizer:tapRecognizer];
UIButton *yourButton = ...
[big addSubview:yourButton];
[self.view addSubview:big];
Then implement the selector as:
- (void)doPress {
[yourButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}
In that way, the outer view taps will be interpreted as button presses for your button.
I have a UIViewController that has UIPageViewController's view added to it. That UIPageViewController has a page with a subview that has a UITableView added as a subview. I am using iOS 6 and the method didSelectRowAtIndexPath is not being called when I click on the cell. There is a weird "bug" though... If I turn the page halfway and then come back to the page I was on (without completing the page turn), I'm then able to select the cell. I assume this has something to do with gesture recognizers, but I can't figure it out. I tried removing the gesture recognizers from the instantiation of the UIPageViewController, but was unable to get that to work.
I remove the tap gesture recognizers from the UIPageViewController like so, but still the buggy behavior exists...
for (UIGestureRecognizer *recognizer in pageViewController.gestureRecognizers) {
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
recognizer.enabled = NO;
}
}
Any suggestions?
Subclass UIView with UITableView. let say myUIView.
Implement the UITableView delegates in myUIView.
Add myUIView as a subview of UIPageControl.
UITableView delegates in myUIView will trigger.
It turned out to be something very simple. On my UIViewController I had the autoresizingMask set to FlexibleHeight. Changing it to None for some reason fixed my problem.
// self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Changed to :
self.contentView.autoresizingMask = UIViewAutoresizingNone;
If anyone can shed some light onto the reasons why this is the case, I'd appreciate it.
I have an UITableView in my class with a custom UITableViewCell with an UIImageView inside.
I need to detect the touch on this UIImageView to handle a custom method(handleSingleTouch:)
and I need to call the -didSelectRowAtIndexPath when the cell is selected.
So i added a UIGestureRecognizer to my TableView in this way:
UIGestureRecognizer *GEST=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTouch:)];
[self.myTable addGestureRecognizer:GEST];
GEST.delegate=self;
[GEST release];
[GEST setCancelsTouchesInView:NO];
[GEST setDelaysTouchesEnded:NO];
There is the conflict:
every time the UIImageView is touched the tableview the GestureRecognizer start the method but also the didSelectRowAtIndexPath is called.
Someone know the way to call the gestureRecognizerMethod without call the didSelectRowAtIndexPath?
Try adding the gesture recognizer to the image rather than the table. See this answer https://stackoverflow.com/a/7760402
I can't think of a way to do that, as you need to be touch responsive in the whole cell. But what you can do is tag your UIImageView and then check at the beginning of -didSelectRowAtIndexPath, for that UIImageView and return.