我有一个UIView,当我点击一个按钮时,我出现了,我基本上将它用作自定义警报视图。现在,当用户点击我添加到主视图的自定义UIView之外时,我想隐藏cusomt视图,我可以使用SELECT `$column` FROM `$table` WHERE $columns[0] = :value", array("value" => $value)
轻松完成此操作但是如何检查视图外的点按?
感谢您的帮助
答案 0 :(得分:6)
有两种方法:
您可以为自定义视图设置标记:
customview.tag=99;
然后在您的viewcontroller中,使用touchesBegan:withEvent:
委托
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch.view.tag!=99){
customview.hidden=YES;
}
}
每次想要弹出自定义视图时,更有可能在其背后有一个叠加层,它将填满您的屏幕(例如,alpha~0.4的黑色视图)。在这些情况下,您可以向其添加UITapGestureRecognizer
,并在每次希望显示自定义视图时将其添加到视图中。这是一个例子:
UIView *overlay;
-(void)addOverlay{
overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
UITapGestureRecognizer *overlayTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(onOverlayTapped)];
[overlay addGestureRecognizer:overlayTap];
[self.view addSubview:overlay];
}
- (void)onOverlayTapped
{
NSLog(@"Overlay tapped");
//Animate the hide effect, you can also simply use customview.hidden=YES;
[UIView animateWithDuration:0.2f animations:^{
overlay.alpha=0;
customview.alpha=0;
}completion:^(BOOL finished) {
[overlay removeFromSuperview];
}];
}
答案 1 :(得分:2)
就像在FlySoFast的答案中一样,我尝试了第一种方法,并且我只是分享了它的快速版本。您可以标记自定义视图并检查该视图是否触及,以便我们实现我们的解决方案。在下面我将自定义视图的标记值分配给900.
customview.tag = 900
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
if touch.view?.tag != 900 {
resetMenu()
}
}
我希望这个答案能对你有所帮助
答案 2 :(得分:1)
当您展示自定义提醒视图时,将该自定义提醒视图添加到另一个全屏视图中,通过将其tapGesture
设置为清除来清除该视图。在主视图中添加全屏视图,并在fullScreen不可见视图中添加tapGesture
,当它点击时删除此视图。
但是,如果您这样做,即使您触摸自定义警报视图,您也需要设置- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:self.customAlertView])
{
return NO;
}
return YES;
}
的委托并实施此方法
Notice: Undefined variable: _SESSION in *PATH* on line 25
答案 3 :(得分:1)
在Swift中使用函数pointInside:
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
if let view = customView {
//if UIView is open open
let newPoint = self.convertPoint(point, toView: view)
let pointIsInsideGenius = view.pointInside(newPoint, withEvent: event)
// tapping inside of UIView
if pointIsInsideGenius {
return true
} else {
// if tapped outside then remove UIView
view.removeFromSuperview()
view = nil
}
}
}
return false
}
答案 4 :(得分:0)
我真的很喜欢使用KLCPopuo库(很容易在github上找到),它可以为你做任何事情。
你给它一个观点,它会显示它,动画它,消除它,并且更多。查找并使用它:)
如果你不想,你可以考虑将UIAlertController子类化,将其弯曲到你的确切警报需求。
答案 5 :(得分:0)
您可以使用以下库:https://github.com/huynguyencong/EzPopup
将PopViewController初始化为您想要在外部点击时将其关闭的视图
let popup = PopupViewController(contentView: viewNeedToRemoveWhenTapOutside, position: .bottomLeft(position))
present(popup, animated: true, completion: nil)
答案 6 :(得分:0)
您可以顺便说一句。主要技巧是将按钮全屏显示在自定义视图后面的按钮。当您单击按钮时,您只需关闭自定义视图。这是完整的代码。
这是完整的自定义uiview类
import Foundation
import UIKit
class CustomAlartView: UIView {
static let instance = CustomAlartView()
@IBOutlet var parentView: UIView!
@IBOutlet weak var mainView: UIView!
@IBOutlet weak var userInput: UITextField!
override init(frame: CGRect) {
super.init(frame: frame)
Bundle.main.loadNibNamed("CustomAlartView", owner: self, options: nil)
setupView()
}
//initWithCode to init view from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// setupView()
}
@IBAction func tappedCancel(_ sender: Any) {
parentView.removeFromSuperview()
}
@IBAction func tappedOk(_ sender: Any) {
if userInput.text == "" {
print("\(userInput.text)")
}
else{
parentView.removeFromSuperview()
}
}
@IBAction func tappedOutside(_ sender: Any) {
print("click outside")
parentView.removeFromSuperview()
}
//common func to init our view
private func setupView() {
parentView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
parentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
mainView.layer.shadowColor = UIColor.gray.cgColor
mainView.layer.shadowOpacity = 1
mainView.layer.shadowOffset = CGSize(width: 10, height: 10)
mainView.layer.shadowRadius = 10
mainView.layer.cornerRadius = 15
mainView.layer.masksToBounds = true
}
enum alartType {
case success
case failed
}
func showAlart() {
UIApplication.shared.keyWindow?.addSubview(parentView)
}
}