正在try 将UITextfield添加到Pinch的顶部以zoom 图像.我能够让按键在图像上放大,但添加UITextfield是我被卡住的地方.我正在try 让UITextfield在放大/缩小图像时停留在图像的特定部分.我以为我快成功了,但Xcode似乎不喜欢我添加到UITextfield的约束.谢谢你的帮助.
当前代码:
import UIKit
import Foundation
class ZoomImage: UIViewController, UITextFieldDelegate {
private var textField: UITextField {
let ut = UITextField()
ut.backgroundColor = .black
return ut
}
// create scrollView
private let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.backgroundColor = .red
return sv
}()
// create imageView
private var imageView: UIImageView = {
let v = UIImageView()
v.backgroundColor = .blue
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup() {
// Allows us to have this view be the delegate of the textfield
self.textField.delegate = self
// Allows us to have this view be the delegate of the scrollview
scrollView.delegate = self
// add Scrollview to view
self.view.addSubview(scrollView)
// Defining imageView
let image = UIImage(named: "test.png")!
imageView = UIImageView(image: image)
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.preferredPresentationSizeForItemProvider)
imageView.contentMode = .scaleAspectFit
imageView.addSubview(textField)
//Defining Textfield
textField.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.preferredPresentationSizeForItemProvider)
textField.placeholder = "TEsting"
// add UIImageView to scroll view
scrollView.addSubview(imageView)
scrollView.addSubview(textField)
imageView.translatesAutoresizingMaskIntoConstraints = false
textField.translatesAutoresizingMaskIntoConstraints = false
scrollView.translatesAutoresizingMaskIntoConstraints = false
// respect safe-area
let safeG = view.safeAreaLayoutGuide
// Zoom range
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 10.0
// constraints
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: safeG.leadingAnchor, constant: 0),
scrollView.trailingAnchor.constraint(equalTo: safeG.trailingAnchor, constant: 0),
scrollView.bottomAnchor.constraint(equalTo: safeG.bottomAnchor, constant: 0),
scrollView.topAnchor.constraint(equalTo: safeG.topAnchor, constant: 0),
imageView.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor, constant: 0),
imageView.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor, constant: 0),
imageView.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor, constant: 0),
imageView.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: 0),
imageView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
imageView.heightAnchor.constraint(equalTo: scrollView.heightAnchor),
textField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0),
textField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0),
textField.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0),
textField.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0)
])
}
}
extension ZoomImage: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}