自定义视图第一次没有出现

时间:2018-01-25 23:35:58

标签: ios swift uiview swift4

当用户第一次打开我的应用时,我的自定义视图不会显示。但下次它工作正常。

当用户点击按钮时,我正在调用此视图。

let menu = MenuView(image: image, title: "text", buttons: buttons)
menu.show(animated: true)

自定义视图代码

class MenuView: UIView, Menu {

  var background = UIView()
  var blackOverlay = UIView()

  convenience init(image: UIImage, title: String, buttons: [UIButton]) {
    self.init(frame: UIScreen.main.bounds)
    setupView(image: image, title: title, buttons: buttons)
  }

  @objc func cancelTapped() {
    hide(animated: true)
  }

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

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  func setupView(image: UIImage, title: String, buttons: [UIButton]) {
    blackOverlay.backgroundColor = UIColor.black.withAlphaComponent(0.5)
    blackOverlay.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
    addSubview(blackOverlay)
    let backgroundWidth = self.frame.width - CGFloat(80.0)
    let imageView = UIImageView(frame: CGRect(x: (backgroundWidth/2)-17, y: 40, width: 34, height: 34))
    imageView.image = image
    imageView.contentMode = .center
    background.addSubview(imageView)

    let titleLabel = UILabel()
    let stringHeight = title.stringHeight + 14
    titleLabel.frame = CGRect(x: background.center.x+8, y: 100, width: backgroundWidth-16, height: stringHeight)
    titleLabel.font = UIFont.systemFont(ofSize: 15, weight: .regular)
    titleLabel.text = title
    titleLabel.textAlignment = .center
    titleLabel.numberOfLines = 0
    background.addSubview(titleLabel)

    var newHeight: CGFloat = 0
    for i in 0...buttons.count-1 {
      buttons[i].frame.origin = CGPoint(x: 0, y: titleLabel.frame.height + 125 + CGFloat(i*50))
      buttons[i].frame.size = CGSize(width: backgroundWidth, height: 50)
      buttons[i].setTitleColor(.gingerColor, for: .normal)
      buttons[i].setTitleColor(UIColor.gingerColor.withAlphaComponent(0.5), for: .highlighted)
      buttons[i].titleLabel?.textAlignment = .center
      newHeight+=buttons[i].frame.height

      let separator = UIView()
      separator.frame.origin = CGPoint(x: 0, y: titleLabel.frame.height + 125 + CGFloat(i*50))
      separator.frame.size = CGSize(width: frame.width, height: 1)
      separator.backgroundColor = UIColor(hexString: "dedede")
      background.addSubview(separator)

      if i == buttons.count-1 {
        buttons[i].setTitleColor(UIColor(hexString: "9E9E9E"), for: .normal)
      }

      buttons[i].addTarget(self, action: #selector(self.cancelTapped), for: .touchUpInside)

      background.addSubview(buttons[i])
    }
    background.frame.origin = CGPoint(x: center.x, y: frame.height)
    background.frame.size = CGSize(width: frame.width-80, height: 90 + titleLabel.frame.height+imageView.frame.height + CGFloat(newHeight))
    background.backgroundColor = .white
    background.layer.cornerRadius = 16
    background.layer.masksToBounds = true
    addSubview(background)
  }

}

和我的自定义视图协议&扩展代码:

protocol Menu {
  func show(animated: Bool)
  func hide(animated: Bool)
  var blackOverlay: UIView { get }
  var background: UIView { get }
}

extension Menu where Self: UIView {

  func show(animated: Bool) {
    self.blackOverlay.alpha = 0
    self.background.alpha = 0
    self.blackOverlay.center = self.center
    self.background.center = CGPoint(x: self.center.x, y: self.frame.height+self.background.frame.height/2)
    UIApplication.shared.delegate?.window??.rootViewController?.view.addSubview(self)
    if animated {
      UIView.animate(withDuration: 0.33, animations: {
        self.blackOverlay.alpha = 1
        self.background.alpha = 1
      })
      UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: {
        self.background.center = self.center
      }, completion: { (completed) in
        print("completed is \(completed)")
      })
    } else {
      self.blackOverlay.alpha = 1
      self.background.alpha = 1
      self.background.center = self.center
    }
  }

  func hide(animated: Bool) {
    if animated {
      UIView.animate(withDuration: 0.33, animations: {
        self.blackOverlay.alpha = 0
        self.background.alpha = 0
      })
      UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: {
        self.background.center = CGPoint(x: self.center.x, y: self.frame.height + self.background.frame.height/2)
      }, completion: { (completed) in
        self.removeFromSuperview()
      })
    } else {
      self.removeFromSuperview()
    }
  }
}

如您所见,我在完成动画块中完成了一个参数,它第一次返回false。所有后续的时间都会返回false

1 个答案:

答案 0 :(得分:0)

我发现了我的错误。

我试图在rootController上面显示我的自定义视图控制器。但是,用户的root控制器第一次是第一次入门屏幕。所以,我的自定义视图试图在另一个控制器上显示。