gpt4 book ai didi

swift - 在 UIVIew 的左上角创建 UILabel 并直接向下设置动画?

转载 作者:行者123 更新时间:2023-11-28 12:48:00 34 4
gpt4 key购买 nike

我已经为这个新游戏工作了几个月,遇到了一个我无法解决的问题!

所以基本上我在我的 UIViewController 的右上角创建了一个“状态部分” .在这个“状态部分”会出现UILabels将状态更新保存为 Strings .这些UILabels将出现在 UIView 的最左上角并且会慢慢向下并丢失Alpha同时..一旦到达底部,最终就会消失。

我做了什么?

因此,正如您在之前的代码中看到的那样,我已经能够创建 UILabel在随机位置,但由于某种原因 UILabels在屏幕外产生。

enter image description here

代码

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

}

}

我添加了问题的图像和 UIView状态 View 也是如此。我在左上角制作 UILabel 时遇到问题,我尝试了下面的这段代码,它甚至没有在屏幕上“生成”UILabel。

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

最佳答案

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()
}

}

}

关于swift - 在 UIVIew 的左上角创建 UILabel 并直接向下设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448371/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com