【问题标题】:how can i detect Custom UIImageView touch event?如何检测自定义 UIImageView 触摸事件?
【发布时间】:2012-10-15 12:39:53
【问题描述】:

我正在使用自定义 UIImageview 来检测触摸?但我无法检测到对该特定图像视图的触摸。

-(void)viewDidLoad {
  [super viewDidLoad];
  mainView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
  image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 300, 100, 100)];
  image.image = [UIImage imageNamed:@"CyanSquare.png"];
  image2 = [[UIImageView alloc] initWithFrame:CGRectMake(150, 600, 100, 100)];
  image2.image = [UIImage imageNamed:@"CyanSquare.png"];
  image.tag = 1;
  image2.tag = 2;
  [self.mainView addSubview:image];
  [self.mainView addSubview:image2];
  [self.view addSubview:mainView];
}

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

   UITouch *touch = [[event allTouches] anyObject];
   [super touchesBegan:touches withEvent:event];
   if ([touch view] == [image viewWithTag:1]) {
    NSLog(@"touch  beggin 1");
    mainView.image = [UIImage imageNamed:@"VideoBkGround.png"];
   }
   if ([touch view] == [image2 viewWithTag:2])
    {
    NSLog(@"touch  beggin 2");
    mainView.image = [UIImage imageNamed:@"VideoRunning.png"];
   }
 }

在这段代码中我没有检测到触摸?请帮帮我?

如果没有自定义视图,它就会被检测到。

【问题讨论】:

  • 您正在使用自定义 UIImageView 但它是否继承/继承了原始 UIImageView 的子类?
  • 我不明白这里的标签是什么意思...为什么不直接针对 image 和 image2 进行测试?
  • 我正在将 2 个 UIImageview(这些是自定义的,不在 .XIB 中)添加到另一个 ImageView。该图像视图被添加到主视图中,例如:[self.view addSubView:imageView];
  • 嗨 Wolvorin 如何继承/继承原始 UIImageView 的子类?

标签: objective-c uiimageview uitouch


【解决方案1】:

您需要为要与之交互的 UIImageView 启用用户交互。

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

类似的东西:

image.userInteractionEnabled = YES; 
image2.userInteractionEnabled = YES;

否则图片将无法触摸...

【讨论】:

    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多