我有一项任务,我必须根据计时器值增加视图大小。我知道视图框架会增加,但是我正在搜索第一轮完成的方式,然后另一个图层轮到它,以便视图框架大小增加,所需的输出也将显示。
答案 0 :(得分:-1)
这是代码。
import UIKit
class ViewController: UIViewController{
var offsetx:CGFloat!
var offsety:CGFloat!
var size: CGFloat!
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
offsetx = view.frame.size.width/2.0;
offsety = view.frame.size.height/2.0;
size = 1.0
timer = Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(drawCircle), userInfo: nil, repeats: true)
}
func drawCircle() {
let cirecle:UIView = UIView()
cirecle.frame = CGRect(x: offsetx, y: offsety, width: size, height: size);
cirecle.layer.borderColor = UIColor.green.cgColor
cirecle.layer.borderWidth = 1.0
cirecle.backgroundColor = UIColor.clear
cirecle.layer.cornerRadius = size / 2.0
offsetx = offsetx - 6.0
offsety = offsety - 6.0
size = size + 12.0
view.addSubview(cirecle)
}
答案 1 :(得分:-1)
像这样......
var radius = 1
func drawCircle() {
let circle = UIView()
circle.frame.size = CGSize(width: radius * 2, height: radius * 2)
circle.backgroundColor = .clear
circle.layer.cornerRadius = radius
circle.layer.borderWidth = 1
circle.layer.borderColor = UIColor.red.cgColor
circle.center = self.center
view.addSubview(circle)
radius += 1
}
请注意。不确定是否要继续添加视图。如果没有,请将view属性放在方法之外,只更新大小和/或先从superview中删除它,然后再重新初始化它。此外,您可能希望将半径限制为最大值。