2012-10-15 125 views
0

我使用自定義UIImageview檢測觸摸?但我無法檢測到特定的imageview觸摸。我如何檢測自定義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"]; 
    } 
} 

在這段代碼中,我沒有檢測到觸摸?請幫助我?

無法自定義視圖的檢測。

+0

使用自定義的UIImageView,但它繼承了原有的UIImageView的/子? –

+0

我不明白這個標籤是關於這裏的...爲什麼不只是測試圖像和image2? – Eiko

+0

我將2個UIImageview(這些都是自定義不在.XIB中)添加到另一個ImageView。該imageview被添加到主視圖像:[self.view addSubView:imageView]; – sreenivas

回答

1

您需要爲要與之交互的UIImageView啓用用戶交互。

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

類似的東西:

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

否則圖片不會得到觸動......