gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:36:25 25 4
gpt4 key购买 nike

我在将 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);

}

如何解决这个问题,请告诉我

最佳答案

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

使用以下代码:

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

您可以在此处阅读有关 NSCodingNSKeyedArchiver 的更多信息:NSCoding , NSKeyedArchiver

关于ios - 我在将 uiview 当前位置设置为 uiview 默认位置时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42808318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com