检测作为UIScrollView的子视图添加的UIImage的触摸

IKK*_*KKA 2 iphone uiscrollview uitouch ios

我有UIScrollView很多UIImageViews。我需要将图像拖放UIScrollView到另一个视图。在外部的scrollView触摸工作。但是内部滚动视图触摸不起作用。我用touchesBegantouchesMoved等等方法。请帮我。

-(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 !!");
    }

}
Run Code Online (Sandbox Code Playgroud)

Mad*_*dhu 5

您需要UIImageView使用继承自的自己的视图来自定义UIImageView。在该自定义子类中提供触摸方法,并将其添加到您的UIScrollView..

的子视图UIScrollview永远不会touchesBegan直接调用方法。您需要使用子视图进行自定义,以获取touchesBegan该添加的子视图/自定义视图的属性。

我的意思是说像接受ImageView的子类

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

CustomImageView 类应该从UIImageView继承

 @interface CustomImageView : UIImageView
  {
  }
Run Code Online (Sandbox Code Playgroud)

在#.m文件中

  #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;
    }

     }
Run Code Online (Sandbox Code Playgroud)