我是Swift的新手,我想用这一行更新值:
circleShape.strokeEnd = CGFloat(sum)
每次点击“计算”(获得总和的价值)。我怎么能这样做?
这是我的代码:
@IBOutlet weak var centerCircle: UIImageView!
var num1 = 0.0
var num2 = 0.0
var sum = 0.0
var a = 0.0
var b = 0.0
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var sumNum: UILabel!
@IBAction func vote1(sender: UIButton) {
num1 = Double(label1.text!)!
self.label1.text = String(num1 + 1)
a = num1 + 1
}
@IBAction func vote2(sender: UIButton) {
num2 = Double(label2.text!)!
self.label2.text = String(num2 + 1)
b = num2 + 1
}
@IBAction func calculate(sender: AnyObject) {
sum = Double(a/(b+a))
sumNum.numberOfLines = 0
sumNum.lineBreakMode = NSLineBreakMode.ByWordWrapping
sumNum.text = "\(sum)"
}
override func viewDidLoad() {
super.viewDidLoad()
// round view
let roundView = UIView(frame: CGRectMake(85, 100, 150, 150))
roundView.backgroundColor = UIColor.whiteColor()
roundView.layer.cornerRadius = roundView.frame.size.width / 2
// bezier path
let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
radius: roundView.frame.size.width / 2.2 ,
startAngle: CGFloat(-0.5 * M_PI),
endAngle: CGFloat(1.5 * M_PI),
clockwise: true)
// circle shape
let circleShape = CAShapeLayer()
circleShape.path = circlePath.CGPath
circleShape.strokeColor = UIColor.blackColor().CGColor
circleShape.fillColor = UIColor.clearColor().CGColor
circleShape.lineWidth = 14
// set start and end values
circleShape.strokeStart = 0.0
circleShape.strokeEnd = CGFloat(sum)
// add sublayer
roundView.layer.addSublayer(circleShape)
// add subview
self.view.addSubview(roundView)
self.view.insertSubview(roundView, belowSubview: centerCircle)
}
答案 0 :(得分:0)
您需要将circleShape
声明为全局变量,并在strokeEnd
函数中修改其calculate
值。
@IBOutlet weak var centerCircle: UIImageView!
var num1 = 0.0
var num2 = 0.0
var sum = 0.0
var a = 0.0
var b = 0.0
let circleShape = CAShapeLayer()
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var sumNum: UILabel!
@IBAction func vote1(sender: UIButton) {
num1 = Double(label1.text!)!
self.label1.text = String(num1 + 1)
a = num1 + 1
}
@IBAction func vote2(sender: UIButton) {
num2 = Double(label2.text!)!
self.label2.text = String(num2 + 1)
b = num2 + 1
}
@IBAction func calculate(sender: AnyObject) {
sum = Double(a/(b+a))
sumNum.numberOfLines = 0
sumNum.lineBreakMode = NSLineBreakMode.ByWordWrapping
sumNum.text = "\(sum)"
circleShape.strokeEnd = CGFloat(sum)
}
override func viewDidLoad() {
super.viewDidLoad()
// round view
let roundView = UIView(frame: CGRectMake(85, 100, 150, 150))
roundView.backgroundColor = UIColor.whiteColor()
roundView.layer.cornerRadius = roundView.frame.size.width / 2
// bezier path
let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
radius: roundView.frame.size.width / 2.2 ,
startAngle: CGFloat(-0.5 * M_PI),
endAngle: CGFloat(1.5 * M_PI),
clockwise: true)
// circle shape
circleShape.path = circlePath.CGPath
circleShape.strokeColor = UIColor.blackColor().CGColor
circleShape.fillColor = UIColor.clearColor().CGColor
circleShape.lineWidth = 14
// set start and end values
circleShape.strokeStart = 0.0
circleShape.strokeEnd = CGFloat(sum)
// add sublayer
roundView.layer.addSublayer(circleShape)
// add subview
self.view.addSubview(roundView)
self.view.insertSubview(roundView, belowSubview: centerCircle)
}