尝试在应用程序内复制iOS样式通知,但动画不起作用

时间:2018-08-19 12:07:11

标签: ios swift core-animation

我正在开发一个高度依赖诸如聊天应用程序之类的通知的应用程序,但它实际上不是聊天应用程序。

我想做的是,当用户在使用应用程序时在设备上收到推送通知时,会在应用程序内部创建通知。这是聊天应用程序中非常常见的功能。

我到达过的地方-

我能够在应用程序顶部创建一个框,其中包含要显示通过推送通知发送的信息的详细信息,并设计了设计方法。

我面临的问题-

我希望它的工作方式类似于从顶部出现的动画,然后在播放动画2秒后折叠起来,但是没有动画在起作用,我无法继续。

代码流-

UIViewController +扩展

func showInlineNotification(header: String, message: String) {

    let window = UIApplication.shared.keyWindow
    let notificationView = UIView(frame: CGRect.zero)
    notificationView.backgroundColor = .lightGray
    let heading = UILabel(frame: .zero)
    let messageLabel = UILabel(frame: .zero)
    heading.textColor = UIColor.black
    messageLabel.textColor = UIColor.black
    heading.text = header
    messageLabel.text = message
    notificationView.addSubview(heading)
    notificationView.addSubview(messageLabel)
    heading.translatesAutoresizingMaskIntoConstraints = false
    messageLabel.translatesAutoresizingMaskIntoConstraints = false
    messageLabel.numberOfLines = 0

    notificationView.layer.cornerRadius = 20
    notificationView.clipsToBounds = true
    window?.addSubview(notificationView)
    notificationView.translatesAutoresizingMaskIntoConstraints = false


    self.view.bringSubview(toFront: notificationView)


    self.view.topAnchor.constraint(equalTo: notificationView.topAnchor, constant: -20).isActive = true
    self.view.leftAnchor.constraint(equalTo: notificationView.leftAnchor, constant: 0).isActive = true
    self.view.rightAnchor.constraint(equalTo: notificationView.rightAnchor, constant: 0).isActive = true
    let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .height, relatedBy: .equal, toItem: notificationView, attribute: .height, multiplier: 2, constant: 0)
    heightConstraint.isActive = true


        notificationView.topAnchor.constraint(equalTo: heading.topAnchor, constant: -20).isActive = true
        notificationView.leftAnchor.constraint(equalTo: heading.leftAnchor, constant: -8).isActive = true
        notificationView.rightAnchor.constraint(equalTo: heading.rightAnchor, constant: -8).isActive = true

        heading.leftAnchor.constraint(equalTo: messageLabel.leftAnchor, constant: 0).isActive = true
        heading.rightAnchor.constraint(equalTo: messageLabel.rightAnchor, constant: 0).isActive = true

        NSLayoutConstraint(item: heading, attribute: .bottom, relatedBy: .equal, toItem: messageLabel, attribute: .top, multiplier: 1, constant: 4).isActive = true



        UIView.animate(withDuration: 4.0, delay: 2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.2, options: [.curveLinear], animations: {
                heightConstraint.constant = 200

            self.view.layoutIfNeeded()
        }, completion: { (completed) in
            print("animation completed")

        })




}

每当收到推送通知并且通过通知实现该功能时,就会在视图控制器上调用该功能。

1 个答案:

答案 0 :(得分:0)

在这里,您将notificationView的高度self.view的1/2设为

let heightConstraint = NSLayoutConstraint(item: self.view, attribute: .height, relatedBy: .equal, toItem: notificationView, attribute: .height, multiplier: 2, constant: 0)
heightConstraint.isActive = true

尽管它的高度应该取决于内容,所以您需要删除该约束并将messagelb的底部钩到notificationView的底部约束,像这样

messageLabel.bottomAnchor.constraint(equalTo: notificationView.bottomAnchor, constant: 0).isActive = true

//

加上

,您首先需要将notificationView的底部设置为self.view的顶部(以使其从顶部滑动到底部)

1-

notificationView.bottomAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true

2-然后将其删除并添加

notificationView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true

//

内部动画块应该是

self.view.layoutIfNeeded()

//

您可能会做类似的事情

func showInlineNotification(header: String, message: String) {

        let window = UIApplication.shared.keyWindow
        let notificationView = UIView(frame: CGRect.zero)
        notificationView.backgroundColor = .lightGray
        let heading = UILabel(frame: .zero)
        let messageLabel = UILabel(frame: .zero)
        heading.textColor = UIColor.black
        messageLabel.textColor = UIColor.black
        heading.text = header
        messageLabel.text = message
        notificationView.addSubview(heading)
        notificationView.addSubview(messageLabel)
        heading.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.numberOfLines = 0

        notificationView.layer.cornerRadius = 20
        notificationView.clipsToBounds = true
        window?.addSubview(notificationView)
        notificationView.translatesAutoresizingMaskIntoConstraints = false


        self.view.bringSubview(toFront: notificationView)


        let botCon = self.view.topAnchor.constraint(equalTo: notificationView.bottomAnchor, constant: 0)
        botCon.isActive = true
        self.view.leftAnchor.constraint(equalTo: notificationView.leftAnchor, constant: 0).isActive = true
        self.view.rightAnchor.constraint(equalTo: notificationView.rightAnchor, constant: 0).isActive = true


        notificationView.topAnchor.constraint(equalTo: heading.topAnchor, constant: -20).isActive = true
        notificationView.leftAnchor.constraint(equalTo: heading.leftAnchor, constant: -8).isActive = true
        notificationView.rightAnchor.constraint(equalTo: heading.rightAnchor, constant: -8).isActive = true

        heading.leftAnchor.constraint(equalTo: messageLabel.leftAnchor, constant: 0).isActive = true
        heading.rightAnchor.constraint(equalTo: messageLabel.rightAnchor, constant: 0).isActive = true
        messageLabel.topAnchor.constraint(equalTo: messageLabel.headingAnchor, constant: 4).isActive = true
        messageLabel.bottomAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 0).isActive = true

        self.view.layoutIfNeeded()

        botCon.constant = -1 * notificationView.bounds.height

        UIView.animate(withDuration: 4.0, delay: 2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.2, options: [.curveLinear], animations: {

            self.view.layoutIfNeeded()

        }, completion: { (completed) in
            print("animation completed")

        })


}