English 中文(简体)
UIKit: How to Add a UITextfield on up of pinch to zoom像
原标题:UIKit: How to Add a UITextfield on top of pinch to zoom image

• 努力在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
    }
}
最佳回答

首先,我们需要在你的法典中确定几个严肃的 b。

  1. 改动<条码>外地,因此只创造了一个实例。 您的法典正在创建一个新的<代码>UIText Field,每次都提到text Field property。 与其他人一样:

    private let textField: UITextField = {
        let ut = UITextField()
        ut.backgroundColor = .gray
        return ut
    }()
    
  2. Don t试图将案文领域添加到两种不同的观点中。 移除:

    imageView.addSubview(textField)
    
  3. Don t创建新的 UIImageView. 改为:

    imageView = UIImageView(image: image)
    

    imageView.image = image
    
  4. 你正在使用制约因素。 Don setting 构思框架。 删除任何观点中设定<代码>范围的财产的任何线。

接下来是更新浏览限制,以便使用<代码> 范围/指南>和<代码>contentLayoutGuide。 然后将案文放在内容的顶部/上角。

NSLayoutConstraint.activate([
    scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: safeG.leadingAnchor, constant: 0),
    scrollView.frameLayoutGuide.trailingAnchor.constraint(equalTo: safeG.trailingAnchor, constant: 0),
    scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: safeG.bottomAnchor, constant: 0),
    scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: safeG.topAnchor, constant: 0),

    imageView.leadingAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.leadingAnchor, constant: 0),
    imageView.trailingAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.trailingAnchor, constant: 0),
    imageView.bottomAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.bottomAnchor, constant: 0),
    imageView.topAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.topAnchor, constant: 0),

    imageView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
    imageView.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),

    textField.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: 0),
    textField.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 0),
])

有了所有这一切,你就可以初步看到全貌,文本领域位于上右角。 但是,正如你所描绘的图像一样,案文领域居于首位,但并没有像预期的那样走向权利。 如果你向左边铺设所有通道,案文领域就出现在屏幕右边,而不是图像观点右边。

随着滚动观点的改变,这个问题可以通过更新案文领域的制约因素加以解决。

增加以下加权观点代表方法:

func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
    textField.removeFromSuperview()
    scrollView.addSubview(textField)
    let xC = NSLayoutConstraint(item: self.textField, attribute: .trailing, relatedBy: .equal, toItem: scrollView.contentLayoutGuide, attribute: .trailing, multiplier: scale, constant: 0)
    let yC = NSLayoutConstraint(item: self.textField, attribute: .top, relatedBy: .equal, toItem: scrollView.contentLayoutGuide, attribute: .top, multiplier: scale, constant: 0)
    NSLayoutConstraint.activate([
        xC,
        yC,
    ])
}

这确保文本领域在每个动物区(或外)结束时正确定位。


这对我来说似乎是一种怀疑。 www.un.org/Depts/DGACM/index_french.htm 似乎并没有随着内容的循环而更新。 无需人工将<编码>>> > > 标准/代码> 应用于违禁品<代码> 多放代代码/代码>。


我尝试了一种替代做法,将案文领域直接添加到图像观点上,并确定案文领域相对于图像观点的限制。 除案文领域外,没有其他代码的工作也随图像观点而改变。 由于目标是使案文保持其原有规模,那是无法工作的。 将“规模”改为案文领域,使案文易于达到预期规模,但这会造成立场的改变。 我在运用各种转变观点的同时,设法克服制约因素。

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签