brkr brkr - 6 months ago 9
Swift Question

Create UILabel at TopLeft of UIVIew and animate straight downwards?

Iv been working on this new game for a couple months now and run into a problems I cant figure out!

So basically I am creating a "status section" in the top right of my

UIViewController
. In this "status section" will appear
UILabels
that hold status updates as
Strings
. These
UILabels
will appear at the very top left of the
UIView
and will go slowly downwards and lose
Alpha
at the same time..eventually dissappearing once it reaches the bottom.

What Iv Done?

So as you can see in the code before so far Iv been able to create the
UILabel
at random locations but for some reason the
UILabels
spawn off screen.

enter image description here

Code

func createStatus() {


let viewWidth = self.statusView.frame.width
let viewHeight = self.statusView.frame.width

let randomX = CGFloat(viewWidth / 2)
let randomY = CGFloat(arc4random_uniform(UInt32(viewHeight)) + 50)


let frame = self.statusView.frame.origin

let status = UILabel(frame: CGRect(origin: CGPoint(x: CGFloat(viewWidth / 4) - 20, y: CGFloat(viewHeight - viewHeight) + 10), size: CGSize(width: 200.0, height: 50.0)))

status.text = "This is a status"
status.textColor = UIColor.redColor()
status.alpha = 1
self.statusView.addSubview(status)
print("Status made")
UIView.animateWithDuration(2) {
status.center = frame
status.alpha = 0

}

}


Iv added an image of the problem and the
UIView
statusView as well. Im having trouble making the UILabel at the top left and i'v tried this code below and it doesnt even "spawn" the UILabel on the screen.

let frame = self.statusView.frame.origin
CGPoint topLeft = CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame));

Answer
func createStatus() {


    let viewWidth = self.statusView.frame.width
    let viewHeight = self.statusView.frame.width

    //Why are these here?
    let randomX = CGFloat(viewWidth / 2)
    let randomY = CGFloat(arc4random_uniform(UInt32(viewHeight)) + 50)


    let origin = CGPointZero//Origins are relative to superview, so top left would be (0, 0)

    //Renamed status to statusLabel so that we know its a label and not a model object
    let statusLabel = UILabel(frame: CGRect(origin: origin, size: CGSize(width: 200.0, height: 50.0)))

    statusLabel.text = "This is a status"
    statusLabel.textColor = UIColor.redColor()
    statusLabel.alpha = 1
    self.statusView.addSubview(statusLabel)
    print("Status made")
    UIView.animateWithDuration(2) {
        //Animate the origin instead of center position. 
        //The origin will be the height of the statusView - the height of your label. x stays 0 to keep it along left edge.
        statusLabel.frame.origin = CGPoint(x: 0, y: self.statusView.frame.height - statusLabel.frame.height )
        statusLabel.alpha = 0

    }, { (completed) in
        if completed {
           //If statusLabel doesn't have a superView it will be dealloc after the function goes out of scope. So remove it from superview after animaion is complete and ARC takes care of it.
           statusLabel.removeFromSuperview()
        }

    }

}
Comments