我想在按下按钮后添加图像,然后在再次按下按钮后将其删除,然后添加具有不同大小和位置的同一图像。我尝试使用


  imageView.image =无


但这没有用,因为什么都不会显示!

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


}

最佳答案

通过查看您的示例,所有这些都是在IBAction中发生的,并且每次您创建新的UIImageView时都会发生。因此,如果您将image设置为新创建的UIImageView的nil,则您将看不到任何效果,因为尚未将其添加到视图中。

看来您将在UIImageView的视图控制器上需要一个实例变量。在viewDidLoad中添加一次imageView,然后在IBAction中对其进行修改,而不是每次都创建一个新视图。

这里还有其他需要考虑的事项可以帮助您进行调试:


也许您应该考虑将其分解为较小的方法,以便在视图控制器上使用实例变量来保持某种状态。
变量名应该有意义,而不是(a,b,c,d等)
为清楚起见,变量名应以小写字母开头,而类应以大写字母开头。
尽可能避免使用幻数,尤其是在有其他可用替代方法时。 M_PI而不是3.14 ....

关于ios - 如何快速删除具有不同特征的图像然后读取?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34041048/

10-13 03:56