我如何在swift中保存和获取userDefaults中的图像值

时间:2018-03-20 16:39:23

标签: ios swift imageview nsuserdefaults

通过这段代码,我在某个角度动画图像,但我想保存它的值并获取它的值。我很困惑如何保存图像值。

if totalVal == 250 || totalVal < 500 
{
    let defaults = UserDefaults.standard
    // let imageSave = UserDefaults.standard

    UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
        self.needleImage.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 0.40)
    }, completion: nil)

    self.PointsLbl.text = "12 Points"
    defaults.set(self.PointsLbl.text, forKey: "Points")    
} else if totalVal >= 500 && totalVal < 1000 {

    let deff = UserDefaults.standard


    UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: { ()
         self.needleImage.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 0.60)        
    }, completion: nil)

    self.PointsLbl.text = "30 Points"
    deff.set(self.PointsLbl.text, forKey: "Points")
} else if totalVal >= 1000 && totalVal < 2000  {

    let deff1 = UserDefaults.standard

    UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
        self.needleImage.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 0.75)
    }, completion: nil)

    self.PointsLbl.text = "45 Points"
    deff1.set(self.PointsLbl.text, forKey: "Points")            
}

1 个答案:

答案 0 :(得分:0)

动画停止时使用“NSStringFromCGPoint”将值保存到NSUserDefault,并在移动图像时获取值。

NSUserDefault是Singleton,您可以通过KVC从任何地方访问它的价值。