0

我正在使用自定义 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"];
   }
 }

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

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

4

1 回答 1

1

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

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

类似的东西:

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

否则图像将无法触及...

于 2012-10-15T12:45:37.517 回答