Как добавить градиент к виду изменения размера? - PullRequest
0 голосов
/ 03 апреля 2020

У меня возникла проблема с пониманием того, как правильно добавлять градиенты во всплывающее представление.

Я создал всплывающее представление в классе и вызывал его несколько раз в своем приложении.

class PopUp: UIView {

init(_message: String, _icon: String, _iconScale: Double, _time: TimeInterval) {
    super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

    self.backgroundColor = .clear

    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    button.setTitle("Ok", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.setTitleColor(.gray, for: .selected)
    button.titleLabel?.numberOfLines = 1
    button.addTarget(self, action: #selector(removeAlert), for: .touchUpInside)

    let buttonLine = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 1))
    buttonLine.backgroundColor = .white

    let message = UILabel()
    message.sizeToFit()
    message.textColor = UIColor.white
    message.text = _message
    message.lineBreakMode = .byWordWrapping
    message.numberOfLines = 10
    message.textAlignment = .center

    let imageIcon = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    let image = UIImage(named: _icon)
    imageIcon.image = image
    imageIcon.contentMode = .scaleAspectFill

    imageIcon.transform = CGAffineTransform(scaleX: CGFloat(_iconScale), y: CGFloat(_iconScale))

    let frameView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    frameView.backgroundColor = darkGray
    frameView.layer.borderColor = UIColor(red: 0, green: 100/255, blue: 120/255, alpha: 0.95).cgColor
    frameView.layer.borderWidth = 0
    frameView.layer.cornerRadius = 3

    self.addSubview(frameView)
    self.addSubview(message)
    self.addSubview(imageIcon)
    self.addSubview(buttonLine)
    self.addSubview(button)

    // set constrains
    frameView.translatesAutoresizingMaskIntoConstraints = false
    frameView.widthAnchor.constraint(equalTo: self.widthAnchor, constant: -30).isActive = true
    frameView.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
    frameView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0).isActive = true
    frameView.topAnchor.constraint(equalTo: imageIcon.topAnchor, constant: -10).isActive = true
    frameView.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: 0).isActive = true
    imageIcon.translatesAutoresizingMaskIntoConstraints = false
    imageIcon.heightAnchor.constraint(equalToConstant: 100).isActive = true
    imageIcon.widthAnchor.constraint(equalToConstant: 100).isActive = true
    imageIcon.centerXAnchor.constraint(equalTo: frameView.centerXAnchor).isActive = true
    imageIcon.bottomAnchor.constraint(equalTo: message.topAnchor, constant: -10).isActive = true
    message.translatesAutoresizingMaskIntoConstraints = false
    message.widthAnchor.constraint(equalTo: frameView.widthAnchor, constant: -20).isActive = true
    message.centerXAnchor.constraint(equalTo: frameView.centerXAnchor).isActive = true
    message.bottomAnchor.constraint(equalTo: button.topAnchor, constant: -15).isActive = true
    buttonLine.translatesAutoresizingMaskIntoConstraints = false
    buttonLine.widthAnchor.constraint(equalTo: frameView.widthAnchor, constant: -5).isActive = true
    buttonLine.heightAnchor.constraint(equalToConstant: 1).isActive = true
    buttonLine.bottomAnchor.constraint(equalTo: button.topAnchor, constant: 0).isActive = true
    buttonLine.centerXAnchor.constraint(equalTo: frameView.centerXAnchor, constant: 0).isActive = true
    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalTo: frameView.widthAnchor, multiplier: 1).isActive = true
    button.heightAnchor.constraint(equalToConstant: 50).isActive = true
    button.centerXAnchor.constraint(equalTo: frameView.centerXAnchor, constant: 0).isActive = true
    button.bottomAnchor.constraint(equalTo: frameView.bottomAnchor, constant: 0).isActive = true

    self.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)

    UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
        self.transform = CGAffineTransform.identity
    }) { (action) in
    }

    if _time != 0 {
        Timer.scheduledTimer(withTimeInterval: _time, repeats: false) { (action) in
            UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: .curveLinear, animations: {
                self.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            }) { (action) in
                self.removeFromSuperview()
            }
        }
    }
}

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

@objc func removeAlert(){
    UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: .curveLinear, animations: {
        self.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
    }) { (action) in
        self.removeFromSuperview()
    }
}

}

пока все хорошо. Но как, черт возьми, я могу добавить градиент, который подходит после изменения размера с ограничениями, или это абсолютно неправильный способ сделать это?

func showPopUp(){
    let disclaimerView = PopUp(_message: "A message...", _icon: "alert", _iconScale: 0.7, _time: 0)
    self.view.addSubview(disclaimerView)

    disclaimerView.translatesAutoresizingMaskIntoConstraints = false
    disclaimerView.heightAnchor.constraint(equalTo: disclaimerView.superview!.heightAnchor, constant: 0).isActive = true
    disclaimerView.widthAnchor.constraint(equalTo: disclaimerView.superview!.widthAnchor, constant: 0).isActive = true
    disclaimerView.centerXAnchor.constraint(equalTo: disclaimerView.superview!.centerXAnchor, constant: 0).isActive = true
    disclaimerView.centerYAnchor.constraint(equalTo: disclaimerView.superview!.centerYAnchor, constant: 0).isActive = tru
}

Как добавить слой и изменить размер градиента, зависит от размер вида?

...