问题
My swift code right now uses a func addBOx to add a series of imageviews to a uiviewcontroller screen. When the user double taps on a single imageview it allows it to change the width of it. When the series of imageviews is on the screen and the user taps the opacity button then if and only if they tap the opacity button then after touch the iamgeview that was added then the slider that was added to changed the width changes the opacity or alpha of the imageview selected. I have added the gif below of what I am trying to do.
import UIKit
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, width: 70, height: 40)
b8.frame = CGRect(x: view.center.x-115, y: view.center.y, width: 70, 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)
}
}
来源:https://stackoverflow.com/questions/59463648/use-tag-to-determine-the-task-a-slider-does-if-and-only-if-a-button-is-tapped