如何使我的浮动UIButton仅停留在homeViewController上?

时间:2019-03-14 23:07:38

标签: swift5

我在homeViewController上自定义了Floating UIButton,并将其显示在其他viewControllers中。我尝试使用此方法将其删除

  

if roundButton.superView!= nil {           DispatchQueue.main.async {               roundButton.removeFromSuperview()               roundButton =零           }       }

但是在我按下一次后它会删除它,并且从不显示它消失了。如果有人知道如何将其从其他viewControllers删除并仅在HomeviewController上显示,将不胜感激。这是我的代码

 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = 200
    tabBarController?.customizableViewControllers = nil

    createFloatingButton()
    buttonSetup()
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)


  }
  override func viewDidAppear(_ animated: Bool) {

    if UserDefaults.standard.bool(forKey: "hasViewedWalkthrough") {

      return
    }

    let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
    if let walkthroughViewController = storyboard.instantiateViewController(withIdentifier: "WalkthroughViewController") as? WalkthroughViewController {

      present(walkthroughViewController, animated: true, completion: nil)
    }
  }

  func createFloatingButton() {
    roundButton = UIButton(type: .custom)
    roundButton.translatesAutoresizingMaskIntoConstraints = false
    roundButton.backgroundColor = .white
    // Make sure you replace the name of the image:
    roundButton.setImage(UIImage(named:"nile1"), for: .normal)
    roundButton.imageView?.layer.cornerRadius = 37.5
    // create a function
    roundButton.addTarget(self, action: #selector(clickhereButtonPressed(_:)), for: UIControl.Event.touchUpInside)
    roundButton.setNeedsLayout()
    roundButton.layoutIfNeeded()
    // We're manipulating the UI, must be on the main thread:
    DispatchQueue.main.async {
      if let keyWindow = UIApplication.shared.keyWindow {
        keyWindow.addSubview(self.roundButton)
        NSLayoutConstraint.activate([
          keyWindow.trailingAnchor.constraint(equalTo: self.roundButton.trailingAnchor, constant: 10),
          keyWindow.bottomAnchor.constraint(equalTo: self.roundButton.bottomAnchor, constant: 90),
          self.roundButton.widthAnchor.constraint(equalToConstant: 75),
          self.roundButton.heightAnchor.constraint(equalToConstant: 75)])
      }
      // Make the button round:
      self.roundButton.layer.cornerRadius = 37.5
      // Add a black shadow:
      self.roundButton.layer.shadowColor = UIColor.black.cgColor
      self.roundButton.layer.shadowOffset = CGSize(width: 0.0, height: 5.0)
      self.roundButton.layer.masksToBounds = false
      self.roundButton.layer.shadowRadius = 2.0
      self.roundButton.layer.shadowOpacity = 0.5
      // Add a pulsing animation to draw attention to button:
      let scaleAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
      scaleAnimation.duration = 0.4
      scaleAnimation.repeatCount = .greatestFiniteMagnitude
      scaleAnimation.autoreverses = true
      scaleAnimation.fromValue = 1.0;
      scaleAnimation.toValue = 1.05;
      self.roundButton.layer.add(scaleAnimation, forKey: "scale")
    }
  }

0 个答案:

没有答案