我下面的swift代码有一个连接到pangesture的图像视图。当移动图像视图后,在文本域中输入内容时。图像视图会恢复到原来的位置。gif表示正在发生的事情。我只是不希望文本输入到文本域后pangesutre的效果无效。
链接到吉思堡https://github.com/redrock34/sse
import UIKit
class ViewController: UIViewController {
var pic = UIImageView()
let fight = (0..<10).map { _ in UIImageView() }
var textEnter = UITextField()
var g2 = UIPanGestureRecognizer()
var slider = UISlider()
override func viewDidLoad() {
super.viewDidLoad()
fight[0].image = UIImage(named: "a.png")
fight.forEach{
$0.isUserInteractionEnabled = true
}
[slider,textEnter].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = .blue
}
slider.backgroundColor = .clear
g2 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
fight[0].addGestureRecognizer(g2)
pic.backgroundColor = .clear
pic.backgroundColor = .systemGreen
fight.forEach{
$0.backgroundColor = .clear
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
[pic].forEach{
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
// Do any additional setup after loading the view.
NSLayoutConstraint.activate ([
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
pic.topAnchor.constraint(equalTo: fight[0].bottomAnchor, constant : 0),
pic.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.62, constant: 0),
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
textEnter.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
textEnter.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
textEnter.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1, constant: 0),
textEnter.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
fight[0].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
fight[0].topAnchor.constraint(equalTo: textEnter.bottomAnchor, constant : 0),
fight[0].heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
fight[0].widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.10, constant: 0),
fight[0].leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
slider.topAnchor.constraint(equalTo: pic.bottomAnchor, constant : 0),
slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.08, constant: 0),
slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
])
textEnter.textAlignment = .center
self.view.sendSubviewToBack(pic)
}
@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) }
}
3条答案
按热度按时间v6ylcynt1#
尝试创建一个UIImageVIew类并编写以下代码:
不要忘记从属性菜单中将类draggableImage分配给Image View。
5lhxktic2#
对于同一对象,不能将显式框架设置与自动布局约束混合使用。
例如,如果您:
label.translatesAutoresizingMaskIntoConstraints = false
然后通过密码。
.center
属性您将看到标签移动,但下次UI更新时(编辑文本字段、点击按钮、旋转设备等),自动布局将根据标签的约束条件重置标签的框架。
因此,在平移手势处理程序中,您可以更新
fight[0]
对象的.constant
值(而不是更改其.center
),或者...可以为
fight
对象保留label.translatesAutoresizingMaskIntoConstraints = true
,并显式设置其框架和中心。注意:在任何一种情况下,您都不***希望***约束任何其他相对于
fight
对象的元素,否则它们将随着您移动fight[0]
而移动。这是对
ViewController
类(来自GitHub zip)的一个修改,实现了在fight对象上使用自动布局约束的 * not * 方法。fdbelqdn3#
请改用此方法
对不起,如果我的代码是混乱的。但我检查了一些scenaios它的工作正常。
快乐编码