从superview

时间:2017-10-23 20:50:31

标签: ios swift uikit cllocationmanager

我只是在按下相应的按钮时尝试从其超级视图中删除视图,但我没有这样做。

视图是我创建的自定义笔尖,名为RequestLocationView

来自viewDidLayoutSubviews

func initBeaconServices() {
    locationManager = CLLocationManager()
    if locationManager.responds(to: #selector(CLLocationManager.requestWhenInUseAuthorization)) {
        requestView = RequestLocationView(frame: UIScreen.main.bounds)
        requestView.setupView()
        requestView.alpha = 0
        requestView.delegate = self
        self.navigationController?.view.addSubview(requestView)
        UIView.animate(withDuration: 0.5, delay : 1, options: .curveEaseOut, animations: {
            self.requestView.alpha = 1
        }, completion: nil)
    }
    locationManager.delegate = self
    locationManager.pausesLocationUpdatesAutomatically = true

    let uuid = UUID(uuidString: "869A6E2E-AE14-4CF5-8313-8D6976058A7A")

    // Create the beacon region to search for with those values.
    beaconRegion = CLBeaconRegion(proximityUUID: uuid!, identifier: "com.dejordan.Capiche")
}

这是RequestView的代码:

class RequestLocationView: UIView {

    @IBOutlet weak var acceptButton: UIButton!
    @IBOutlet weak var rejectButton: UIButton!

    var lView: UIView!

    weak var delegate: HandleLocationPermissionDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    private func xibSetup() {
        lView = loadViewFromNib()
        lView.frame = bounds
        lView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        lView.translatesAutoresizingMaskIntoConstraints = true
        addSubview(lView)
    }

    func loadViewFromNib() -> UIView {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let nibView = nib.instantiate(withOwner: self, options: nil).first

        return nibView as! UIView
    }

    func setupView() {
        acceptButton.layer.cornerRadius = acceptButton.frame.height / 2
    }

    func fadeIn(completion: @escaping (Bool) -> Void) {
        UIView.animate(withDuration: 1.3, animations: {
            self.alpha = 1
        }, completion: completion)
    }

    func fadeOut(completion:  ((Bool) -> Void)?) {
        UIView.animate(withDuration: 1.3, animations: {
            self.alpha = 0
        }, completion: completion)
    }

    @IBAction func acceptPressed(_ sender: UIButton) {
        delegate?.allowPermissions()
    }

    @IBAction func rejecttPressed(_ sender: UIButton) {
        delegate?.denyPermissions()
    }
}

以下是父viewController中的委托方法:

func allowPermissions() {
    requestView.fadeOut {(finished)  in
        if finished {
            self.locationManager.requestWhenInUseAuthorization()
            self.requestView.removeFromSuperview()
        }
    }
}

func denyPermissions() {
    requestView.fadeOut {(didFinish) in
        self.requestView.removeFromSuperview()
    }
}

当您点击拒绝时会发生这种情况......您可以看到背景略微褪色,但是视图会保持原样。

Not responding as hoped...

我已经在这个问题上搔了一会儿,并且已经扫过许多Stack Overflow帖子但无济于事......

1 个答案:

答案 0 :(得分:1)

{p> CLLocationManager代码可能以不太友好的方式进行交互。

如果将RequestLocationView初始化移至viewDidLoad更正了问题,则可能是原因。

要在acceptButton上获得正确的半径,请尝试在自定义视图中添加layoutSubviews处理程序:

override func layoutSubviews() {
    acceptButton.layer.cornerRadius = acceptButton.bounds.height / 2
}