My swift code below creates 2 objects a imageview and a uitextfield. The imageview whichs is named pic is connected to a uipangestureReconginzier. When the user touches it moves around the view controller just like I want it to. However when the user touches the textfield and enters text. The pic is moved back to its defined constraint. I cant think of what is causing this.
import UIKit
class ViewController: UIViewController {
var g11 = UIPanGestureRecognizer()
var textEnter = UITextField()
var pic = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
g11 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
pic.addGestureRecognizer(g11)
[textEnter,pic].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = .systemBlue
$0.isUserInteractionEnabled = true
}
textEnter.backgroundColor = .brown
NSLayoutConstraint.activate([
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
pic.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
pic.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5, constant: 0),
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
textEnter.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
textEnter.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant : 0),
textEnter.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.3, constant: 0),
textEnter.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
])
}
@objc func g1Method(_ sender: UIPanGestureRecognizer){
let tranistioon = sender.translation(in: self.view)
sender.view!.center = CGPoint(x: sender.view!.center.x + tranistioon.x, y: sender.view!.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view) }
}