I Have UiView(PromoView),
I want to Open PromoView on UIButton's Action, also set Background Blur when popup view is open and when user click anywhere in view the PoPView will be hidden.
this is my code..
-(IBAction)PromoCode_btn:(id)sender
{
_PromoView.hidden=NO;
_PromoView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.view.alpha=0.5;
_PromoView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
}];
}
Try this,
@IBAction func PromoCode_btn(_ sender: Any) {
view_background.backgroundColor = UIColor.black
view_background.alpha = 0.8
view_background.translatesAutoresizingMaskIntoConstraints = false
layoutDic["view_background"] = view_background
self.view.addSubview(view_background)
view_background.isHidden = false
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view_background]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricdict, views: layoutDic))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view_background]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metricdict, views: layoutDic))
let promoHideTap = UITapGestureRecognizer.init(target: self, action: #selector(self.promoCancelAction))
promoHideTap.delegate = self
view_background.addGestureRecognizer(promoHideTap)
_PromoView.translatesAutoresizingMaskIntoConstraints = false
layoutDic["_PromoView"] = _PromoView
view_background.addSubview(_PromoView)
_PromoView.backgroundColor = UIColor.white
_PromoView.layer.borderColor = UIColor.lightGray.cgColor
_PromoView.layer.borderWidth = 0.2
_PromoView.layer.shadowColor = UIColor.gray.cgColor
_PromoView.layer.shadowOpacity = 0.2
view_background.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-200-[_PromoView(300)]", options: NSLayoutFormatOptions(rawValue: UInt(0)), metrics: metricdict, views: layoutDic))
view_background.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-250-[_PromoView]-250-|", options: NSLayoutFormatOptions(rawValue: UInt(0)), metrics: metricdict, views: layoutDic))
}
func promoCancelAction()->Void
{
view_background.isHidden = true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
{
// Method to recognize whether the tap originated from bg view or table view
if touch.view!.isDescendant(of: _PromoView)
{
return false
}
else
{
return true
}
}
Hope this work!