0

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 ?

Nikhat Afshan
  • 65
  • 1
  • 1
  • 7
  • http://stackoverflow.com/questions/14785395/dismissing-the-keyboard-from-a-uitextfield-uitextview-as-a-subview-of-uiscrollvi/14788474#14788474 – Rajneesh071 Feb 09 '13 at 13:12

3 Answers3

0

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

Community
  • 1
  • 1
Rushi
  • 4,553
  • 4
  • 33
  • 46
  • Can u please tell me how ?.I have referred the link u gave but where did he made subclass?There are all events of a view – Nikhat Afshan Feb 05 '13 at 10:13
  • @NikhatAfshan : 1. Create a subclass of UIScrollView 2. override Touches end methods – Rushi Feb 05 '13 at 10:19
  • @NikhatAfshan : check this http://iphonedevsdk.com/forum/iphone-sdk-development/6686-uiscrollview-wont-respond-to-touch-event-scrolls-pans-but-tochesshouldbegin-ne.html – Rushi Feb 05 '13 at 10:20
0

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];
  }
Vishal
  • 8,246
  • 6
  • 37
  • 52
  • Yes we will get the touch point(width and height).Can u please tell me How can I get if the touched event is image1 or image2 ? – Nikhat Afshan Feb 05 '13 at 10:10
0

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];
  }
Rajan Balana
  • 3,775
  • 25
  • 42
  • Yes we will get the touch point(width and height).Can u please tell me How can I get if the touched event is image1 or image2 ? – Nikhat Afshan Feb 05 '13 at 10:10
  • You will get the X and Y axis and according to that , You can find out, whether at that position which Image is placed either it will be Image 1 or Image 2 – Rajan Balana Feb 05 '13 at 10:12
  • I got x=283 and y=228 when I click on the middle of the image.When I click on the left of the image I do get some other positions.So how can I compare an image within some area? – Nikhat Afshan Feb 05 '13 at 10:16
  • You don't need to compare image with an area, but you need to perform the task depending upon the area tapped. – Rajan Balana Feb 05 '13 at 10:18
  • Yes my image dimensions are (217,257,89,82).When I click at varoius points of image I get separate x and y touchpoints.Do I need to write separately for all the points to go to same ViewController? – Nikhat Afshan Feb 05 '13 at 10:21
  • You can compare the range of coordinates, If the coordinates are that in which your image is lying then perform the function else leave it. – Rajan Balana Feb 05 '13 at 10:23