I am using UITapGestureRecognizer for detecting which UIView was tapped on my screen but for some reason it only detects the parent view tap, for example below code logs only parent views tag. How do i detect subview taps which are present on main view. Please suggest.
Inside View did load :-
UITapGestureRecognizer *viewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionForViewTapped:)];
[self.view addGestureRecognizer:viewTapRecognizer];
-(void) actionForViewTapped:(UITapGestureRecognizer*)sender {
NSLog(#"view tapped");
UIView *view = sender.view;
NSLog(#"view tag is %lu", view.tag); //Always prints parent view tag.
if(view.tag == 10){
NSLog(#"tag1 tapped"); // not called
}
if(view.tag == 20){
NSLog(#"tag 2 tapped"); // not called
}
}
You can detect subview using following code, I just tested creating sample project.
-(void) actionForViewTapped:(UITapGestureRecognizer*)sender {
NSLog(#"view tapped");
UIView *view = sender.view;
CGPoint loc = [sender locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
NSLog(#"view tag is %lu", subview.tag); //will print Subview tag.
if(view.tag == 10){
NSLog(#"tag1 tapped");
}
if(view.tag == 20){
NSLog(#"tag 2 tapped");
}
}
A UIGestureRecognizer is to be used with a single view.
If you want to use UIGestureRecognizer you will have to create one for each view, calling the same method.
Related
I am using UITapGestureRecognizer for detecting which UIView was tapped on my screen but for some reason it only detects the parent view tap, for example below code logs only parent views tag. How do i detect subview taps which are present on main view. Please suggest.
Inside View did load :-
UITapGestureRecognizer *viewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionForViewTapped:)];
[self.view addGestureRecognizer:viewTapRecognizer];
Method outside view did load.
-(void) actionForViewTapped:(UITapGestureRecognizer*)sender {
NSLog(#"view tapped");
UIView *view = sender.view;
NSLog(#"view tag is %lu", view.tag); //Always prints parent view tag.
if(view.tag == 10){
NSLog(#"tag1 tapped"); //Not called
}
if(view.tag == 20){
NSLog(#"tag 2 tapped"); //Not called
}
}
We have more options to find detecting on sub view by tap gesture
CHOICE 1:Directly tap to SubView
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapSubView)];
tapGesture.numberOfTapsRequired = 1;
[subView addGestureRecognizer:tapGesture];
CHOICE 2:Finding tap on SubView through Parent View
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapSubView)];
tapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapGesture];
-(void)tapSubView:(UITapGestureRecognizer *)sender
{
UIView* view = sender.view;
CGPoint loc = [sender locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
//OR
CGPoint point = [sender locationInView:sender.view];
UIView *viewTouched = [sender.view hitTest:point withEvent:nil];
if ([viewTouched isKindOfClass:[self.view class]])
{
NSLog(#"the subView is called");
}
else
{
NSLog(#"the subView is not called");
}
}
Printed Output is
the subView is called
CHOICE 3:Find Tap Detection using Delegate methods of Gesture
First You have to add the GestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if([touch.view isKindOfClass: [self.view class]] == YES)
{
return YES; // return YES (the default) to allow the gesture recognizer to examine the touch object
}
else {
return NO; //NO to prevent the gesture recognizer from seeing this touch object.
}
}
The gesture recognizer is only associated with one specific view, which means it will only recognize touches on the view it is added to. If you want to know which subview was touched, then you will need to do a couple of things:
Set userInteractionEnabled = false for each subview. This will make it so that every touch on a subview is passed up to the parent view, and the touch will be recognized by the gesture recognizer.
There isn't enough information on your view hierarchy or layout to know exactly how to proceed from here, but you can use one or some of these methods to determine which view was touched: UIView.hitTest(_:with:), UIView.point(inside:with:), CGRectContainsPoint() or UIGestureRecognizer.location(in:). For example, if the subviews do not overlap each other, you could use the following code snippet to test if the touch was in a particular view:
let location = tapGesture.locationInView(parentView)
if CGRectContainsPoint(subview1, location) {
// subview1 was touched
}
I want to touch on UIImageView and that image display in another view in full size using navigate the view. same as gallery in mobile.
just creating demo programmatically. because I'm new in iphone developing.
Here is my code.
- (void)viewDidLoad
{
NSLog(#"Enter in viewDidLoad method");
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);
[self.view setUserInteractionEnabled:YES];
[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
int x=5;
int y=5;
int hei=100;
int wid=100;
int k=1;
for (int i=0; i<3; i++)
{
for(int j=0;j<50;j++)
{
imageView = [[UIImageView alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
[imageView setImage:[UIImage imageNamed:#"Motocross.png"]];
[imageView setUserInteractionEnabled:YES];
scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
[imageView setTag:k];
[scroll addSubview:imageView];
NSLog(#"The tag is == %d",k++);
NSLog(#" (%d,%d)x == %d",i,j,(x+wid)*i+5);
NSLog(#" (%d,%d)y == %d",i,j,(y+hei)*j+5);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Enter in touchesBeganWithEvent method");
UITouch *touch = [touches anyObject];
if ([touch view] == imageView)
{
NSLog(#"Enter in if condition of touch");
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.imageD = imageView.image;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
NSLog(#"Exit from if condition of touch");
}
NSLog(#"Exit from touchesBeganWithEvent method");
}
I want to tap on imageView and view will navigate to detailViewController and display that image in big size. but this touchesBegan method not called when i tap on imageView. In this i'm not creating array of UIImageView just alloc init in loop.. Thank you!
Rather than using touchesBegan: on the view, consider adding a UITapGestureRecognizer to each image view. When the image view is tapped (you are setting userInteractionEnabled already which is good) then you can get the view that was tapped with sender.view.
Note that you create a new instance of UITapGestureRecognizer for each image view that you have, but that the target/action of each gesture can be the same.
The problem with your current code is that this check:
if ([touch view] == imageView)
verifies the touch only against the last image view that you added to the scroll view. So, touching the last one should work, but all the others won't.
As per the comment from #vin, the correct solution to that issue is to verify the class type of the view that was touched:
if ([[touch view] isKindOfClass:[UIImageView class]]) {
(though the scroll view may have other image view subclasses than just the ones you add...)
Try bellow code or its better to use Collection view
- (void)viewDidLoad
{
NSLog(#"Enter in viewDidLoad method");
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);
[self.view setUserInteractionEnabled:YES];
[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
int x=5;
int y=5;
int hei=100;
int wid=100;
int k=1;
for (int i=0; i<3; i++)
{
for(int j=0;j<50;j++)
{
UIButton *buttonForImage=[[UIButton alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
[buttonForImage setImage:[UIImage imageNamed:#"Motocross.png"] forState:UIControlStateNormal];
[buttonForImage addTarget:self action:#selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
[buttonForImage setTag:k];
[scroll addSubview:buttonForImage];
NSLog(#"The tag is == %d",k++);
NSLog(#" (%d,%d)x == %d",i,j,(x+wid)*i+5);
NSLog(#" (%d,%d)y == %d",i,j,(y+hei)*j+5);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)imageButtonPressed:(UIButton*)sender{
NSLog(#"Enter in if condition of touch");
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.imageD = sender.imageView.image;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
You need to use UICollectionview rather than trying to juggle with rows /colns, images touches etc...
Good tutorial is here... UICollectionView Tutorial
(Assuming you dont support below iOS 6.0)
I have a UIScrollview in a tab, i added some UIView (NIB) to the UIScrollview. Each UIView has some UISwictch, labels, buttons, etc. How can i get the the label.text inside of the view added to the UIScrollview.
I tried a lot of things but i can access to the content of the UIView added to the UIScrollview.
Check with this,
for (UIView *addedView in [self.scrollView subviews])
{
for (UIView *sub in [addedView subviews])
{
if([sub isKindOfClass:[UILabel class]])
{
UILabel *myLabel = (UILabel *)sub;
NSLog(#"My label :%#",myLabel .text);
}
}
}
Here scrollView is your scroll view. It will print all label's text.
If you need to print any particular label's text. Then add a tag to it and check the tag before printing it, like if(myLabel.tag == 7)
set a unique tag for your label.
eg: label.tag = 10345 (some random number, which is a unique tag)
and you can search for the label in the parentView like this
UILabel *theLabel = (UILabel *)[parentView viewWithTag: 10345];
and then you can do what ever you want with the Label.
Step 1: Add this code in your scrollview
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTapGestureRecognizer];
Step 2: Implement this method
-(void)singleTap:(UITapGestureRecognizer *)gesture
{
// Convert gesture into view for getting subviews
CGPoint point = [gesture locationInView:mainScrollView];
UIView *tappedView = [mainScrollView hitTest:point withEvent:nil];
// Get the subviews of the view
NSArray *subviews = [view tappedView];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(#"%#", subview);
// List the subviews of subview
[self your_method:subview];
}
}
So I am usually follow this way to dismiss ModalViewController when tap outside its view
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
{
if(![self.view.window.gestureRecognizers containsObject:recognizer])
{
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapBehind:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[self.view.window addGestureRecognizer:recognizer];
[recognizer release];
}
}
}
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window
//Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.
if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
{
[self dismissModalViewControllerAnimated:YES];
[self.view.window removeGestureRecognizer:recognizer];
}
}
}
its working good with regular ModalViewController .. but now I have a modelViewController which is a NavigationViewController with "x" viewController as the root one .. when I use this way and tap on the navigationBar the controller is dismissed , or when I navigate to "y" controller from x controller and want to get back , also when I click the back button the ModalView is dismissed ! which is wrong behavior to me .. I just want the controller to be dismissed in case the tap was completely outside the controller (view area + navigationBar area) .. any one can provide a help please ?
Update your code following:
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint flocation = [sender locationInView:nil]; //Passing nil gives us coordinates in the window
//Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.
CGPoint tap = [self.view convertPoint:flocation fromView:self.view.window];
CGPoint tapBar = [self.navigationController.navigationBar convertPoint:flocation fromView:self.view.window];
if (![self.view pointInside:tap withEvent:nil] && ![self.navigationController.navigationBar pointInside:tapBar withEvent:nil])
{
// Remove the recognizer first so it's view.window is valid.
[self.view.window removeGestureRecognizer:sender];
[self dismissModalViewControllerAnimated:YES];
}
}
}
Remove UITapGestureRecognizer from window when tap on UIBarButtonItem
-(void)viewWillDisappear:(BOOL)animated {
[self.view.window removeGestureRecognizer:self.tapOutsideGestureRecognizer];
}
In my app I should implement the drag and drop of a imageView; the problem is that my imageView is inside a scrollview; it's my code
- (void) viewDidLoad{
[super viewDidLoad];
UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(dragGestureChanged:)];
[scrollViewAlfabeto addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myscrollView.gestureRecognizers)
{
[gestureRecognizer requireGestureRecognizerToFail:downwardGesture];
}
}
- (void) dragGestureChanged:(UIPanGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:scrollViewAlfabeto];
if (gesture.state == UIGestureRecognizerStateBegan)
{
[imageViewToMove removeFromSuperview];
[self.view addSubview:imageViewToMove];
UIView *draggedView = [myscrollView hitTest:point withEvent:nil];
if ([draggedView isKindOfClass:[UIImageView class]])
{
imageViewToMove = (UIImageView*)draggedView;
}
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
imageToMove.center = point;
}
else if (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed)
{
// Determine if dragged view is in an OK drop zone
// If so, then do the drop action, if not, return it to original location
NSLog(#"point.x final:%f", point.x);
NSLog(#"point.y final:%f", point.y);
if (CGRectContainsPoint(goal.frame, point)){
imageToMove.frame = CGRectMake(167, 159, 100, 100);
}
else{
[imageToMove removeFromSuperview];
[myscrollView addSubview:imageToMove];
[imageToMove setFrame:CGRectMake(12, 38, 100, 100)];
imageToMove = nil;
}
}
}
Then with my code I'm able to take an imageView from scrollView with longpress, drag it inside "self.view"; also if I drop this imageView over another imageView in self.view it puts on it. It work fine. But I have two problems:
1- when I drag this imageView and I do
[imageViewToMove removeFromSuperview];
[self.view addSubview:imageViewToMove];
the image view don't appear under my finger but in other position and I want that it remains under my finger
2- This code work only the first time when I launch my viewcontroller, because If I don't drop the imageview over other imageview inside self.view, itr return inside scrollview, but If I want drag it a second time, it don't work.
Can you help me?
I'm not advanced in objc, but...
Allow user ineraction [self.view userInteractionEnabled:YES];
Try [self.view setMultipleTouchEnabled:YES];
Taken from the documentation of UIView in relation to userInteractionEnabled:
Discussion
When set to NO, user events—such as touch and
keyboard—intended for the view are ignored and removed from the event
queue. When set to YES, events are delivered to the view normally. The
default value of this property is YES.
During an animation, user interactions are temporarily disabled for
all views involved in the animation, regardless of the value in this
property. You can disable this behavior by specifying the
UIViewAnimationOptionAllowUserInteraction option when configuring the
animation.
Hope it works