Responding to touchesBegan in UIPickerView instead of UIView

后端 未结 5 947
萌比男神i
萌比男神i 2020-11-30 12:02

I have a UIPickerView that gets faded out to 20% alpha when not in use. I want the user to be able to touch the picker and have it fade back in.

I can get it to work

5条回答
  •  攒了一身酷
    2020-11-30 12:17

    Here is some code that does what you want:

    @interface TouchDetectionView : UIPickerView {
    
    }
    - (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event;
    @end
    @implementation TouchDetectionView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
        [hitTestView touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
        [hitTestView touchesMoved:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
        [hitTestView touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UIView * hitTestView = [self getNextResponderView:touches withEvent:event];
        [hitTestView touchesCancelled:touches withEvent:event];
    }
    
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        return self;
    }
    
    - (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch * touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
        UIView * hitTestView = [super hitTest:point withEvent:event];
    
        return ( hitTestView == self ) ? nil : hitTestView;
    }
    

提交回复
热议问题