Touches event for a UIScrollView - iphone

How can I get touch event for UIScrollView.I have a UIViewController and I placed a scrollView in viewController.I need to add imageViews to scrollView and if a particular image is clicked it should redirect to its corresponding viewController.
I knew how to add touch event to a UIViewController.But that doesn't work for a scrollView.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch= [touches anyObject];
if ([touch view] == img1)
{
ViewController2 *viewController2=[[ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[self presentViewController: viewController2 animated:YES completion:nil];
}
}
HOw can I get touches event for a scrollView ?

The only way to do this is by creating a custom UIScrollView Your custom UIScrollView will be a subclass of UIScrollView.
Also for more options you can check this

Set things like below:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
and you get the touch in:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}

You can use the ScrollViewDelegate and can use the function "ScrollViewDidScroll" for getting the scrollview scroll event.
Set up a tap gesture recognizer:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}

Related

UIGestureRecognizer across entire view

I want to be able to make a gesture recognizer listening for a two finger swipe, anywhere on the screen.
Right now I have a UITableView in a sub-view that scrolls, of course, and it doesn't seem to pick up the UIGestureRecognizer that I set on the view. When you use the two fingers, it just scrolls...
Is there a way to make the entire view, maybe superview, listen for a two finger touch and when it recognizes it, it will instead of scrolling the table view, do what I want it to do?
EDIT: Heres my code but this is suposed to be a very general question. I just want to be able to have UIGestureRecognizers across the entire view, the whole thing.
navBarTitle.title = #"Crunch";
//opening the menuView from launch
//setup the customtable view as a subview in menuView
SDMenuViewController *mvc = [[[SDMenuViewController alloc] initWithNibName:#"SDNestedTableView" bundle:nil] autorelease];
[self addChildViewController:mvc];
[mvc didMoveToParentViewController:self];
[menuView addSubview:mvc.view];
[mvc.view setFrame:CGRectMake(mvc.view.frame.origin.x, mvc.view.frame.origin.y, mvc.view.frame.size.width, mvc.view.frame.size.height + 44)]; //add navBarHeight, for some strage reason
//add swipe down gesture on for the menu
UISwipeGestureRecognizer *swipeClose =[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(closeMenu)] autorelease];
swipeClose.numberOfTouchesRequired = 2;
swipeClose.direction = UISwipeGestureRecognizerDirectionUp;
UISwipeGestureRecognizer *swipeOpen = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(openMenu)] autorelease];
swipeOpen.numberOfTouchesRequired = 2;
swipeOpen.direction = UISwipeGestureRecognizerDirectionDown;
for (UIGestureRecognizer* rec in menuView.gestureRecognizers) {
[rec requireGestureRecognizerToFail:swipeClose];
[rec requireGestureRecognizerToFail:swipeOpen];
}
for (UIGestureRecognizer* rec in mvc.view.gestureRecognizers) {
[rec requireGestureRecognizerToFail:swipeClose];
[rec requireGestureRecognizerToFail:swipeOpen];
}
[mvc.view addGestureRecognizer:swipeClose];
[mvc.view addGestureRecognizer:swipeOpen];
[self.view addGestureRecognizer:swipeClose];
[self.view addGestureRecognizer:swipeOpen];
Maybe I did not explain my question clearly. But for what I was doing this ended up working, and maybe it will help other people in the future:
Two finger swipe in UIScrollview for iPad application
you can try to do something like this:
UISwipeGestureRecognizer* p = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(actionPan:)];
p.numberOfTouchesRequired = 2;
for (UIGestureRecognizer* rec in tableView.gestureRecognizers) {
[rec requireGestureRecognizerToFail:p];
}
[self.view addGestureRecognizer:p];
hope that will help
//In view did load
[self.view setMultipleTouchEnabled:YES];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[event touchesForView:self.view] count] > 1) {
NSLog(#"%d active touches",[[event touchesForView:self.view] count]) ;
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
}
}
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
// do some thing...
}
}
i Hope this would solve your problem. and in touches enabled make the count equal to 2 or any number of touches.. :) feel free to ask if you have any doubts
  Add another view with frame of self.view over self.view like "viewForGesture" and add gesture recognizer on that view like
UIView *viewForGesture = [[ [UIView alloc]initWithFrame:self.view.frame] autorelease];
[viewForGesture setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:viewForGesture];
.
.
.
[viewForGesture addGestureRecognizer:swipeOpen];
[viewForGesture addGestureRecognizer:swipeClose];
There's two way of making UIView detecting touch. The approach you defined and want that's UIGestureRecognizer, that answer already given by Ezeki.
Another approach you can use to detect touch is to override UITouch delegates.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.tapCount==2)
{
//Do something ... (Here, you can also check for the object which touched.)
}
}

How to give action on click of an image

I am developing an app in which what i want is when a user clicks on an image in an image view
An Action should be fired(like when a button is clicked).
I don't want button image. I just want an image view and action on it.
Kindly help.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:singleTap];
[singleTap release];
-(void)tapDetected{
NSLog(#"single Tap on imageview");
}
According to your comment, if you want to know about image's index then you can do this by tag property of image view. Assign your image view's tag property as per your need and get the tag on Touch event.
For example :
Add UITapGestureRecognizer
ITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
Use its delegate method : - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
// called when touch is began or when user touches
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
int indexOfImageView = touch.view.tag;
}
Hope it helps you...

UITapGestureRecognizer with couple of views

I am using :
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewClicked:)];
[tmp addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
to get notification when a view(bigview for our example) i clicked(I have a lot of view), and in the front there is a UIView (a blank one) the he is in the front of the view (there is a reason why this view is in the front before al the views).
there is now a problem to get notification when tmp is tapped because the bigview is in the front.
there is any solution for something like this?
EDIT
In the bigview i have UISwipeGestureRecognizer:
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[itemsView addGestureRecognizer:recognizer];
[recognizer release];
and if i make userInteractionEnabled in the bigview to NO he don't get notification on swipe
Two options:
A. Set userInteractionEnabled to NO on that big/blank view.
B. Implement pointInside:withEvent: on that big/blank view
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// UIView will be "transparent" for touch events if we return NO
return NO;
}
(as taken from allowing-interaction-with-a-uiview-under-another-uiview)

How can I get touchesBegan event on SuperView

In my program there is two views(scrollView-super, view-sub*2).
In my case two subviews are subviewed in scrollView. touchesBegan event called in subviews.
How Can I get event in scrollView???
#interface MyScrollView:UIScrollView
...
#implement MyScrollView
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//here is my code
//but I can't get event here
...
}
-(id)initWithFrame:(CGRect) frame
{
...
MyView *view1 = [[MyView alloc] initWithFrame:(0, 0, 320, 240);
MyView *view2 = [[Myview alloc] initWithFrame:(0, 240, 320,240);
[self addSubview: view1];
[self addSibvoew: view2];
...
}
#interface MyView:UIView
...
#implement MyScrollView
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//break points
//here comes event
}
Try this code..
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scroll addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)touch{
CGPoint touchPoint=[gesture locationInView:scrollView];
touchPoint = [touch locationInView:self.view];
}
I suggest making ur two subview global and then in the superview touchesbegan method pass the touches and the event to the subviews for handling. So somethig like [view1 touchesBegan:touches event:event];
i have not tested it but that should be one way.

Double tap on some specific subviews with UIGestureRecognizer subclass?

I followed the session's demo of the 121 WWDC 2010 (Advanced Gesture Recognition) to find a way to have all behaviors (rotate, scale, translate) on an other class (TransformGestureReconizer) and all goes well and do this for the subviews:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:baseView];
subView1.userInteractionEnabled = YES;
[self addTransformGestureToView:subView1];
}
Here is my problem :
I would like to have an action when I double tap on a desired subview.
So If I add a :
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
I can't choose which view my action delivers (like changing the image on it)
If I add on the main view:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
I can only handle the double tap on the main view, but not for the subviews and can only do it on the TransformGestureReconizer.h but then not choose the view tapped ( I think because subclass of UIGestureRecognizer).
I found a solution :
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self addTransformGestureToView:subView1];
[self.subView1 addGestureRecognizer:doubleTap];
[doubleTap release];