我现在的快速代码使用func addBOx将一系列图像视图添加到uiviewcontroller屏幕。当用户双击单个图像视图时,它允许它更改其宽度。当一系列图像视图出现在屏幕上并且用户点击不透明度按钮时,只有并且只有当他们点击不透明度按钮时,才在触摸添加的iamgeview之后,然后添加以更改宽度的滑块将更改不透明度或选择的图像视图。我在下面添加了要尝试做的gif。
class ViewController: UIViewController {
var sx = UISlider()
var count: Int = 0
var ht = -90
var ww = 80
var arrTextFields = [UIImageView]()
var b7 = UIButton()
var b8 = UIButton()
var currentView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
[b7,sx,b8].forEach {
$0.translatesAutoresizingMaskintoConstraints = false
view.addSubview($0)
$0.backgroundColor = .systemOrange
}
b7.setTitle("add imageview",for: .normal)
b8.setTitle("opacity",for: .normal)
b7.frame = CGRect(x: view.center.x-115,y: view.center.y + 200,width: 70,height: 40)
sx.frame = CGRect(x: view.center.x-115,y: view.center.y-200,height: 40)
b8.frame = CGRect(x: view.center.x-115,y: view.center.y,height: 40)
b7.addTarget(self,action: #selector(addBOx),for: .touchUpInside)
b8.addTarget(self,action: #selector(op),for: .touchUpInside)
}
@objc func op() {
//uses slider to change the alpha after the imageview added is selected.
}
@objc func addBOx() {
let subview = UIImageView()
subview.isUserInteractionEnabled = true
arrTextFields.append(subview)
view.addSubview(subview)
sx.addTarget(self,action: #selector(ji),for: .valueChanged)
sx.minimumValue = 10
sx.maximumValue = 150
subview.frame = CGRect(x: view.bounds.midX - 0,y: view.bounds.midY + CGFloat(ht),width: CGFloat(ww),height: 35)
subview.backgroundColor = .purple
subview.tag = count
let pan = UIPanGestureRecognizer(target: self,action: #selector(handlePanGestured(_:)))
subview.addGestureRecognizer(pan)
count += 1
ht += 50
arrTextFields.append(subview)
let tapGesture = UITapGestureRecognizer(target: self,action: #selector(handletapGestured(_:)))
subview.addGestureRecognizer(tapGesture)
}
@objc func handletapGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
}
@objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
let draggedView = gesture.view!
view.bringSubviewToFront(draggedView)
let translation = gesture.translation(in: view)
draggedView.center = CGPoint(x: draggedView.center.x + translation.x,y: draggedView.center.y + translation.y)
gesture.setTranslation(.zero,in: view)
}
@objc func ji(sender : UISlider){
ww = Int(sx.value)
currentView?.alpha = CGFloat(sx.value)
currentView?.bounds.size.width = CGFloat(sx.value)
}
}