Can you attach a UIGestureRecognizer to multiple views?

前端 未结 12 978
忘了有多久
忘了有多久 2020-11-22 14:08
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];         


        
12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 15:00

    You can do it using this code my views which are imageviews in the xib.

    - (void)viewDidLoad
    {
        firstIV.tag = 501;
        secondIV.tag = 502;
        thirdIV.tag = 503;
        forthIV.tag = 504;
    
        [self addTapGesturetoImageView: firstIV];
        [self addTapGesturetoImageView: secondIV];
        [self addTapGesturetoImageView: thirdIV];
        [self addTapGesturetoImageView: forthIV];
    }
    
    -(void)addTapGesturetoImageView:(UIImageView*)iv
    {
        iv.userInteractionEnabled = YES;
        UITapGestureRecognizer * textfielBGIVTapGasture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textfielBGIVTapped:)];
        textfielBGIVTapGasture.numberOfTapsRequired = 1;
        [iv addGestureRecognizer:textfielBGIVTapGasture];
    }
    
    - (void)textfielBGIVTapped:(UITapGestureRecognizer *)recognizer {
        int tag = recognizer.view.tag-500;
        switch (tag) {
            case 1:
            {
                //firstIV tapped;
                break;
            }
            case 2:
            {
                //secondIV tapped;
                break;
            }
            case 3:
            {
                //thirdIV tapped;
                break;
            }
            case 4:
            {
                //forthIV tapped;
                break;
            }
            default: {
                break;
            }
        }
    }
    

提交回复
热议问题