我将uiview当前位置设置为uiview默认位置时遇到问题?

时间:2017-03-15 11:21:34

标签: ios swift

我将uiview当前位置设置为uiview默认位置时遇到问题请帮忙。我正在使用imageoutletshoefront.bounds.origin = self.imageoutletshoefront.bounds.origin

let panGestureRecognizer = UIPanGestureRecognizer(target:self, action: "panHandler:");
    imageviewUIviewOutlet.addGestureRecognizer(panGestureRecognizer);

调用函数“panHandler”

 // Pan gesture handler

func panHandler(recognizer:UIPanGestureRecognizer){
        let translation = recognizer.translationInView(imageviewUIviewOutlet);
        func sliderChanged(sender: UISlider) {
            //Use the value from the slider for something
            print(sliderMaximizeAndMinimize.value)

            if (sliderMaximizeAndMinimize.value > 1 && sliderMaximizeAndMinimize.value < 3)
            {
                var value = (sliderMaximizeAndMinimize.value) as? CGFloat
                self.imageviewUIviewOutlet.transform = CGAffineTransformMakeScale(value! , value! )
                imageoutletshoefront.transform = CGAffineTransformMakeScale(value!, value!)
            }
        }
        recognizer.view!.center = CGPoint(x: recognizer.view!.center.x + translation.x, y: recognizer.view!.center.y + translation.y);
        recognizer.setTranslation(CGPointZero, inView: view);

    }

如何解决此问题请告诉我

1 个答案:

答案 0 :(得分:0)

您需要将数据保留在磁盘上。

使用以下代码:

import UIKit
import Foundation

struct codingKeys {
    static let finalPoint = "defaultPoint"
}

class ViewController: UIViewController{

    var defaultPoint = CGPoint.zero
    @IBOutlet weak var myView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let pan = UIPanGestureRecognizer(target: self, action: #selector(didPan))
        myView.addGestureRecognizer(pan)
        setDefaultPosition()

    }


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
         setDefaultPosition()
    }


    func setDefaultPosition(){
        if doesPersistentPositionURLExist(){
            myView.center = getDefaultPosition()
            self.view.layoutIfNeeded()
        }

    }

    func getPersistentPositionURL() -> URL{
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let defaultPositionURL = documentsDirectory.appendingPathComponent("defaultPosition")
        print(defaultPositionURL)
        return defaultPositionURL
    }

    func doesPersistentPositionURLExist() -> Bool{
        let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("defaultPosition").path
        if FileManager.default.fileExists(atPath: path){
            return true
        } else {
            return false
        }
    }

    func persistDefaultPosition(){
        let data = NSMutableData()
        let archiver = NSKeyedArchiver(forWritingWith: data)
        archiver.encode(defaultPoint, forKey: codingKeys.finalPoint)
        archiver.finishEncoding()
        data.write(to: getPersistentPositionURL(), atomically: true)
    }

    func getDefaultPosition() -> CGPoint{
        let url = getPersistentPositionURL()
        if let data = try? Data(contentsOf: url){
            let unarchiver = NSKeyedUnarchiver(forReadingWith: data)
            let point = unarchiver.decodeCGPoint(forKey: codingKeys.finalPoint)
            unarchiver.finishDecoding()
            return point
        } else {
            return CGPoint.zero
        }
    }

    func didPan(_ sender: UIPanGestureRecognizer){
        let translation = sender.translation(in: self.view)
        let finalX = myView.center.x + translation.x
        let finalY = myView.center.y + translation.y
        defaultPoint = CGPoint(x: finalX, y: finalY)
        myView.center = CGPoint(x: finalX, y: finalY)
        view.layoutIfNeeded()
        persistDefaultPosition()
        sender.setTranslation(CGPoint.zero, in: self.view)
    }
}

您可以在此处详细了解NSCodingNSKeyedArchiverNSCodingNSKeyedArchiver