【问题标题】:Detect touch of UIImage added as subview of UIScrollView检测作为 UIScrollView 的子视图添加的 UIImage 的触摸
【发布时间】:2013-02-26 05:53:15
【问题描述】:

我有UIScrollView 和许多UIImageViews。我需要将图像从UIScrollView 拖放到另一个视图。在scrollView touch 之外工作。但是在滚动视图内部触摸不起作用。我使用了touchesBegan,touchesMoved 等方法。请帮我。

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

}

【问题讨论】:

  • 您是否尝试过将 UIImageView 设置为 [self setUserInteractionEnabled:TRUE]?如果我没记错的话,它默认是禁用的。
  • @IkmalEzzani,UserInteractionEnabled 为 true。但它不起作用。
  • UIScrollView touchesBegan 内部不适用于子视图。只需验证我的答案即可。
  • @Madhu,请提供一些示例代码
  • 检查它用示例代码更新了我的答案。

标签: iphone ios uiscrollview uitouch


【解决方案1】:

您需要使用从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];

CustomImageView 类应该是继承自 UIImageView 之类的

 @interface CustomImageView : UIImageView
  {
  }

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

     }

【讨论】:

  • @madhu:感谢您的回答。真的很棒,很有帮助!
【解决方案2】:

在滚动视图中添加一个点击手势识别器,以便在滚动视图中启用触摸:

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];

然后用指定的方法编写代码。

-(void)methodName:(UITapGestureRecognizer *)gesture
{
     //your code here
}

【讨论】:

  • 不要忘记将 ExclusiveTouch 设置为 yes,就像下面提到的 Bhargavi...如果您不这样做,用户可能会在滚动时不小心“点击”图像...@ 987654323@ 将防止意外触摸,如果用户只是滚动浏览(尤其是启用分页),他们不会意外触发 50 亿(实数)点击。
【解决方案3】:

我不知道这是否对你有帮助。但是尝试将UIImageViewexclusiveTouch属性设置为YES

【讨论】:

  • 这很重要!许多人忽略了这一点。
【解决方案4】:

您是否将滚动视图添加为 IBOutlet?如果它来自 xib 并且正如您所说的触摸在滚动外部而不是内部可用,我猜您可能不小心取消了 userInteractionEnabled 以阻止触摸事件的滚动视图。并且您已将 UIImageViews 作为子视图添加到 UIScrollView。对于 UIImageView,您必须首先将 userInteractionEnabled 设置为 YES,然后在必要时添加任何手势以获取事件或使用 touchesBegan、touchesMoved 方法。除非您设置启用交互,否则您不会在 UIImageView 上收到触摸事件。如果父视图我的意思是 UIScrollView 的交互被禁用,你也不会接触 UIImageView。希望这会有所帮助:)

【讨论】:

  • 我以编程方式添加了 UIScrollView。我设置了 scrollView.userInteractionEnabled = YES 。但是触摸不起作用..
  • 是的,触摸图像不起作用。UIScrollView 包含所有图像。
  • 你能滚动触摸视图吗?滚动视图是否检测到触摸?你是如何在 UIImageview 上添加触摸感受器的?你为 UIImageview 设置了 userinteractionEnabled YES 吗?
  • 我认为您的 scrollView 代表会覆盖触摸方法。此外,触摸 UIImageview 令人头疼,因为它们不能像我们预期的那样工作。尝试在图像上添加一个自定义类型的 UIButton,并将框架作为图像视图的框架。或者你可以在 imageView 上添加一个点击手势并检查它是否工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2016-02-06
  • 2014-06-21
  • 1970-01-01
相关资源
最近更新 更多