簡體   English   中英

檢測UIImageView上的觸碰事件

[英]detect a touch down event on UIImageView(s)

我在UIView上放置了4個UIImageView,並將它命名為

UIImageView myImg1 = [[UIImageView alloc] init];     
UIImageView myImg2 = [[UIImageView alloc] init];     
UIImageView myImg3 = [[UIImageView alloc] init]; 
UIImageView myImg4 = [[UIImageView alloc] init];

我怎么能告訴編譯器我剛剛觸及UIImageView 1或UIImaveView 2.如果我只能使用UIImageView來完成這個任務,而不是按鈕。 請指導我這個問題。 謝謝

一個時尚的解決方案是使用UIGestureRecognizer對象:

在你的loadView方法中(或你編寫UIImageView (s)繪圖的任何地方......):

   UITapGestureRecognizer *tapRecognizer;
    UIImageView *anImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    [anImageView setTag:0]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
    [anImageView setBackgroundColor:[UIColor redColor]];
    [anImageView setUserInteractionEnabled:TRUE];
    tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
    tapRecognizer.numberOfTapsRequired = 1;
    [anImageView addGestureRecognizer:tapRecognizer];
    [contentView addSubview:anImageView];
    [anImageView release];

    UIImageView *anotherImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 180, 100, 100)];
    [anotherImageView setTag:1]; //Pay attention here!, Tag is used to distinguish between your UIImageView on the selector
    [anotherImageView setBackgroundColor:[UIColor greenColor]];
    [anotherImageView setUserInteractionEnabled:TRUE];
    tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
    tapRecognizer.numberOfTapsRequired = 1;
    [anotherImageView addGestureRecognizer:tapRecognizer];
    [contentView addSubview:anotherImageView];
    [anotherImageView release];

這是一個簡單的imageViewDidTapped: sample方法,可用於區分上面的兩個示例UIImageView對象:

- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;

    UIImageView *tappedImageView = (UIImageView *)[tapGesture view];

    switch (tappedImageView.tag) {
        case 0:
            NSLog(@"UIImageView 1 was tapped");
            break;
        case 1:
            NSLog(@"UIImageView 2 was tapped");
            break;
        default:
            break;
    }
}

您應該考慮使用tag屬性。 為每個圖像指定一個唯一tag ,然后使用此信息確定觸摸的圖像。 例如,設置標簽

UIImageView myImg1 = [[UIImageView alloc] init];     
myImg1.tag = 1;

當你收到img的觸摸時,請致電

img.tag

檢索標簽。

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        singleFingerTap.numberOfTapsRequired = 1;
        [myImg1 addGestureRecognizer:singleFingerTap];
        [myImg2 addGestureRecognizer:singleFingerTap];
        [singleFingerTap release];

和行動方法

-(void)handleSingleTap:(UIGestureRecognizer*)sender{
    //your code here
}

你可以這樣做。

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

        UITouch *touch = [touches anyObject];

       CGPoint touchLocation = [touch locationInView:self.view];
   if(CGRectContainsPoint(myImg1.view.frame,touchLocation))
 {
NSLog(@" Image 1");
}
else if(CGRectCont..

注意......如果點位於多個圖像視圖中,則會輸出NSLog ..在這種情況下..檢查哪個圖像視圖位於頂部..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM