2

I have UIScrollView with many UIImageViews.I need to drag and drop image from UIScrollView to another view. Outside the scrollView touch is worked. But inside scroll view touch is not worked. I used touchesBegan,touchesMoved etc method. Please help me.

-(IBAction)selectBut:(id)sender
{

 scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x,y,w,h)];

 scrollView.userInteractionEnabled = YES;

 int y = 0;

 for (int i = 0; i < [myArray count]; i++) {

UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];

 image.userInteractionEnabled = YES;

  y=y+35;

 [scrollView addSubview:image];

}

[self.view addSubview:scrollView];

[scrollView setContentSize:CGSizeMake(150, 300)]

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

   UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 1) {

        NSLog(@"One touch !!");
    }

}
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
IKKA
  • 6,297
  • 7
  • 50
  • 88

4 Answers4

5

you need to customize the UIImageView with your own view inherited from UIImageView. Provide touch methods in that customized subclass and add it on your UIScrollView..

Subviews of UIScrollview will never call touchesBegan method directly. You need to customized with subview to get touchesBegan properties of that added subview/customized view.

I mean to say take subclass of ImageView like

CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];

CustomImageView class is should be inherited from UIImageView like

 @interface CustomImageView : UIImageView
  {
  }

in # .m File

  #import "CustomImageView.h"

@implementation CustomImageView


    - (id)initWithFrame:(CGRect)frame
   {
self = [super initWithFrame:frame];
if (self) {

}
return self;
}



    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      NSLog(@"%s", __FUNCTION__);

     }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {


    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {


    UITouch *touch = [touches anyObject];

      if ([touch tapCount] == 2) {
    drawImageView.image = nil;
    return;
    }

     }
Madhu
  • 1,542
  • 1
  • 14
  • 30
3

Add a Tap gesture recognizer to the scroll view for enabling touch inside scroll view:

UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(methodName:)];
singlTap.cancelsTouchesInView = NO; //Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
[scrollViewName addGestureRecognizer:singlTap];

then write your code in specified method.

-(void)methodName:(UITapGestureRecognizer *)gesture
{
     //your code here
}
  • 1
    Don't forget to set exclusiveTouch to yes like Bhargavi mentioned below... If you don't do this than the user might accidentally "tap" the image when they are just scrolling... `exclusiveTouch` will prevent accidental touches that way if the user is just scrolling through (especially with Paging enabled) they won't accidentally trigger 50 gagillion (real number) taps. – Albert Renshaw Feb 26 '13 at 06:06
2

I don't know if this help you. But try to set the exclusiveTouch property of the UIImageView to YES.

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
0

Did you add your scrollview as an IBOutlet? If its from xib and as you say touches are available outside the scroll and not within, I guess you might have accidentally unchecked userInteractionEnabled for the scrollview which inhibits touch events. And you have added UIImageViews as subviews to UIScrollView. For UIImageView you have to first set userInteractionEnabled to YES and then add any gestures if necessary to get events or use the touchesBegan, touchesMoved methods. Unless you set interaction enabled you wont receive touch events on UIImageView. If the parent view I mean the UIScrollView's interaction is disabled you wont get touches on UIImageView too. Hope this helps :)

Meera
  • 1,031
  • 6
  • 25
  • I added UIScrollView programatically.I set scrollView.userInteractionEnabled = YES . But touch not working.. – IKKA Feb 26 '13 at 06:26
  • ya,touch on image is not working.UIScrollView contains all images. – IKKA Feb 26 '13 at 06:32
  • Are you able to scroll the view on touch?? Does the scrollview detects touch? How did you add touch receptors on UIImageview? Did you set userinteractionEnabled YES for UIImageview? – Meera Feb 26 '13 at 06:51
  • I think your scrollView delegates overrides touch methods. Moreover touch on UIImageview is a headache as they dont work well as we expect. Try adding a UIButton of type custom over the image with frame as that of your imageviews. Or can you just add a tap gesture over imageView and check whether its working?? – Meera Feb 26 '13 at 07:13