获取相互叠加的视图

时间:2015-02-05 18:06:52

标签: ios swift uiview overlapping

我写了一个显示通知视图的简单类。

import UIKit

enum NotificationPosition {
    case Top
    case Bottom
}

enum NotificationType {
    case Success
    case Error
    case Progress
    case Info
    case Custom
}


public class NotificationView: UIView {

    private var iconImageView = UIImageView()
    private var messageLabel = UILabel()
    private var containerView: UIView!
    private var messageText: String!

    public var height: CGFloat!

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

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

    init(inView: UIView, position: NotificationPosition, type: NotificationType, height: CGFloat, text: String) {
        super.init()

        self.height = height
        self.containerView = inView
        self.messageText = text

        createNotificationView(type)
        placeOffScreen(position)
        animate(position)
    }

    private func createNotificationView(type: NotificationType) {
        switch type {
        case .Progress:
            addProgressIndicator()
        case .Error:
            println("")
        case .Success:
            println("")
        case .Info:
            println("")
        case .Custom:
            println("")
        default: ()
        }

        addMessageLabel(messageText)
    }

    private func addProgressIndicator() {
        let progressIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        progressIndicatorView.center = CGPoint(x: 25, y: height / 2)
        progressIndicatorView.activityIndicatorViewStyle = .White
        progressIndicatorView.startAnimating()
        addSubview(progressIndicatorView)
    }

    private func addMessageLabel(text: String) {
        let messageLabel = UILabel()
        messageLabel.font = UIFont.boldSystemFontOfSize(14)
        messageLabel.text = text
        messageLabel.textColor = UIColor.whiteColor()
        messageLabel.textAlignment = .Center
        messageLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        addSubview(messageLabel)

        let xCenterConstraint = NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0)
        let yCenterConstraint = NSLayoutConstraint(item: messageLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0)
        let leadingConstraint = NSLayoutConstraint(item: messageLabel, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 10)
        let trailingConstraint = NSLayoutConstraint(item: messageLabel, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: -10)
        addConstraints([ xCenterConstraint, yCenterConstraint, leadingConstraint, trailingConstraint])
    }

    private func placeOffScreen(position: NotificationPosition) {

        switch position {
        case .Top:
            self.frame = CGRect(x: 0, y: -height, width: containerView.frame.width, height: height)

        case .Bottom:
            self.frame = CGRect(x: 0, y: containerView.frame.size.height, width: containerView.frame.width, height: height)

        default: ()
        }
    }

    private func animate(position: NotificationPosition) {

        switch position {
        case .Top:
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                self.frame.origin.y = 0
            })

        case .Bottom:
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                self.frame.origin.y = self.containerView.frame.size.height - self.frame.size.height
            })

        default: ()
        }
    }

    public func hide() {
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.frame.origin.y = self.containerView.frame.size.height
        })
    }
}

这是您将其添加到视图中的方式。

let notificationView = IJNotificationView(inView: view, position: .Bottom, type: .Info, height: 35, text: "Synchronization completed")
notificationView.backgroundColor = UIColor.greenColor()
view.addSubview(notificationView)

这是它的外观。

enter image description here

虽然它有效,但是有一个问题。正如您所看到的,当我单击几个按钮以显示许多通知视图时,它们彼此重叠。我想要做的是让他们互相叠加。像this一样。这张图片来自这个名为ALAlertBanner的库,它提供与我的类相似的功能,但我无法弄清楚它是如何做到的。

任何人都可以就如何做到这一点给我一个建议/想法吗?

感谢。

0 个答案:

没有答案