How to mask out a view when filling an area?
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 | |-MaskThanks,
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)
- create a mask view (programmatically or with IB) and call it "maskView".
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];
you will need to set your view controller as the delegate for "UIGestureRecognizerDelegate"
@interface YourViewController : UIViewController <UIGestureRecognizerDelegate>
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];
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
精彩评论