开发者

How to mask out a view when filling an area?

开发者 https://www.devze.com 2023-02-11 19:34 出处:网络
could anyo开发者_高级运维ne tell me how to place a translucent black mask over the whole screen, but with the area of a particular UIView being excluded? I want to use this mask over a UITextField, wh

could anyo开发者_高级运维ne tell me how to place a translucent black mask over the whole screen, but with the area of a particular UIView being excluded? I want to use this mask over a UITextField, which calls resignFirstResponder when the outside part of the textfield is tapped.

The subview tree would be like:

UIWindow

|-UIView

| |-UITextField

|

|-Mask

Thanks,


You can use the:

- (void)bringSubviewToFront:(UIView *)view

And send the UITextField to the front after you add the black mask view.

UPDATE

Ok this are the steps to do it (you can see the apple example for UIGestureRecognizers for more)

  1. create a mask view (programmatically or with IB) and call it "maskView".
  2. create a gestureRecognizer and add it to the maskView.

            UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
            recognizer.delegate = self;
            UIImageView *maskView =  [[UIImageView alloc] init];
            [maskView addGestureRecognizer:recognizer];
    
  3. you will need to set your view controller as the delegate for "UIGestureRecognizerDelegate"

    @interface YourViewController : UIViewController   <UIGestureRecognizerDelegate>
    
  4. add the maskView to your ViewController when you want to mask the screen. and then move the text field above the mask.

    [self.view addSubView:maskView]; [self.view bringSubviewToFront:textField];

  5. set this 2 functions: in the first one you can set the action if the user touches the mask

    - (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
    //resign the first responder when the user taps the mask
     //you can remove the mask here if you want to   
    

    } in the second one you tell the app not to receive touches from the textField

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    // Disallow recognition of tap gestures in the segmented control.
    if ((touch.view == textField)) {//checks if the touch is on the textField
        return NO;
    }
    return YES;
    

    }

Hope it make some sense

shani

0

精彩评论

暂无评论...
验证码 换一张
取 消