shu223 / Pulsator

Pulse animation for iOS
MIT License
1.42k stars 161 forks source link

Not visible until view disappears/reappears #7

Closed n1mda closed 3 years ago

n1mda commented 8 years ago

Pulsator is not visible until I close the app and reopen it (causing viewDidLayoutSubviews to trigger). No matter if I call centerPulsator(), force layouting subviews upon load etc. What could be the problem?

import Foundation
import Pulsator

class TestView: UIViewController {

    var icon: UIImage?
    var message: String?
    var buttonTitle: String?

    lazy var iconView: UIImageView = { [unowned self] in
        let view = UIImageView()

        view.backgroundColor = UIColor.clearColor()
        view.image = self.icon

        return view
    }()

    lazy var pulsator: Pulsator = { [unowned self] in
        let pulsator = Pulsator()

        pulsator.numPulse = 3
        pulsator.radius = 100
        pulsator.backgroundColor = UIColor.redColor()

        pulsator.position = self.iconView.layer.position

        return pulsator
    }()

    lazy var messageLabel: UILabel = { [unowned self] in
        let label = UILabel()

        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(17)
        label.adjustsFontSizeToFitWidth = true
        label.numberOfLines = 0
        label.minimumScaleFactor = 0.5
        label.textAlignment = .Center

        label.text = self.message

        return label
    }()

    lazy var settings: UIButton = { [unowned self] in
        let button = UIButton()

        button.backgroundColor = UIColor.redColor()
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitle(self.buttonTitle, forState: .Normal)

        button.addTarget(self, action: #selector(openSystemSettings), forControlEvents: .TouchUpInside)

        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()

        [iconView, messageLabel, settings].forEach {
            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.bringSubviewToFront($0)
        }

        iconView.layer.superlayer?.insertSublayer(pulsator, below: iconView.layer)

        setupConstraints()

        pulsator.start()
        centerPulsator()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        centerPulsator()
    }

    func centerPulsator() {
        iconView.layer.layoutIfNeeded()
        pulsator.position = iconView.layer.position
    }

    func openSystemSettings() {
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    }

    func setupConstraints() {

        // Icon and pulsator
        for attribute: NSLayoutAttribute in [.Width, .Height] {
            view.addConstraint(NSLayoutConstraint(item: iconView, attribute: attribute, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 70))
        }
        for attribute: NSLayoutAttribute in [.CenterX, .CenterY] {
            view.addConstraint(NSLayoutConstraint(item: iconView, attribute: attribute, relatedBy: .Equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0))
        }

        // Settings Button
        view.addConstraint(NSLayoutConstraint(item: settings, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50))
        for attribute: NSLayoutAttribute in [.Left, .Right, .Bottom] {
            view.addConstraint(NSLayoutConstraint(item: settings, attribute: attribute, relatedBy: .Equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0))
        }

        // Message Label
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Bottom, relatedBy: .Equal, toItem: settings, attribute: .Top, multiplier: 1, constant: -40))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1, constant: 20))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1, constant: -20))
    }
}
keanysa commented 8 years ago

func centerPulsator() { iconView.layer.layoutIfNeeded() pulsator.position = iconView.layer.position iconView.setNeedsLayout() } ?

DeepAnchor commented 8 years ago

@n1mda currently experiencing the same issue. Any luck with this?

n1mda commented 8 years ago

None yet :(

Axel Möller

On 28 October 2016 at 20:14:46, DeepAnchor (notifications@github.com) wrote:

@n1mda https://github.com/n1mda currently experiencing the same issue. Any luck with this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/shu223/Pulsator/issues/7#issuecomment-256990725, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB6Cmgmv2J18ZDAyJ4IqSlHQ2uxYHXUks5q4juWgaJpZM4Jps68 .

sambing commented 6 years ago

Seems like this hasn't changed since the question was asked. Moving the code to viewDidAppear worked for me.