我想在按下按钮后添加图像,然后在再次按下按钮后将其删除,并添加具有不同尺寸和位置的相同图像。我尝试使用
imageView.image = nil
但这没有用,因为根本没有显示出来!
@IBAction func btnCalc(sender: AnyObject) {
let Lead1 = txtLead1.text
let LeadI: Float = (Lead1! as NSString).floatValue
let Lead2 = txtLead2.text
let LeadII: Float = (Lead2! as NSString).floatValue
let Lead3 = txtLead3.text
let LeadIII: Float = (Lead3! as NSString).floatValue
let a = 0.5*(LeadII + LeadIII)
let b = 0.5*(LeadI - LeadIII)
let c = (-0.5*(LeadI + LeadII))
let d :Float = 2/1.7320508075688772935274463415058723669428052538103806280558
let e = LeadIII/LeadI
let y :Float = Float (atan(d*(e+0.5)))
let z = (y*180)/3.141592654
let z1 = z + 180
let zz = LeadI/(cos((z*3.141592654)/180))
let zzz = -zz
let Z1 = CGFloat((z1*3.141592654)/180)
let Z = CGFloat((z*3.141592654)/180)
let imageName = "Arrow.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
if LeadI == 0 && LeadII == 0 && LeadIII == 0 {
lblaVF.text = "aVF = 0"
lblaVL.text = "aVL = 0"
lblaVR.text = "aVR = 0"
lblmEA.text = "Mean Electrical Axis = 0"
lblA.text = "A = 0"
//Remove old image from view
} else if LeadII == LeadIII + LeadI && zz<0 {
lblaVF.text = "aVF = \(a)"
lblaVL.text = "aVL = \(b)"
lblaVR.text = "aVR = \(c)"
lblmEA.text = "Mean Electrical Axis = \(z1)"
lblA.text = "A = \(zzz)"
imageView.frame = CGRect(x: 107, y: 340, width: 160, height: 24)
view.addSubview(imageView)
imageView.transform = CGAffineTransformMakeRotation(Z1)
} else if LeadII == LeadIII + LeadI {
lblaVF.text = "aVF = \(a)"
lblaVL.text = "aVL = \(b)"
lblaVR.text = "aVR = \(c)"
lblmEA.text = "Mean Electrical Axis = \(z)"
lblA.text = "A = \(zz)"
imageView.frame = CGRect(x: 107, y: 340, width: 160, height: 24)
view.addSubview(imageView)
imageView.transform = CGAffineTransformMakeRotation(Z)
} else {
//Remove old image from view
lblaVF.text = "aVF ="
lblaVL.text = "aVL ="
lblaVR.text = "aVR ="
lblmEA.text = "Mean Electrical Axis ="
lblA.text = "A ="
let alert = UIAlertController(title:"Houston, we've got a problem!", message:"Rememer that Lead II = Lead I + Lead III", preferredStyle:UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"OK",style: UIAlertActionStyle.Default, handler:nil))
self.presentViewController(alert, animated:true, completion:nil)
}
}
答案 0 :(得分:1)
通过查看您的示例,所有这一切都发生在IBAction
,每次您创建新的UIImageView
时。因此,如果您将image
设置为新创建的UIImageView
的nil,您将看不到任何效果,因为它尚未添加到视图中。
您的视图控制器上需要UIImageView
的实例变量。在viewDidLoad
中添加一次imageView,然后在IBAction中修改它,而不是每次都创建一个新的。
这里还有其他一些可以帮助您调试的事情: