• 努力在pin顶上添加一个UITextfield,以图索姆图像。 我能够把pin子带到一个图像工作上去,但添加了UIText的现场,即Im正在 st。 我试图让UIText现场在图像中流离时保持某种形象的具体部分。 我认为我已接近尾声,但X条码似乎并不赞成我给美国信息技术公司增加的限制。 求助热线。
Current code:
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
}
}