I want my swift code to to end the connection the slider has with object pg after the action with the slider is completed. So right now when you click of the imageview pg then use the slider the slider is connected to the size of the imageview. I want the size of the iamgeview to be controlled like. Press on pg - then press on slider and drag slider to size you want - when slider is not touch you can not change the size of pg without first touching it. I have a gif below to show you what is happening.
import UIKit
class ViewController: UIViewController {
var image1Width2: NSLayoutConstraint!
var image1Height2: NSLayoutConstraint!
var currentView: UIView?
var greenTransition: CGAffineTransform?
var slider = UISlider()
var PG = UIImageView()
var btn = UIButton()
var jake = false
var btnCouter = 0
var g2 = UIPanGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
slider.value = 0.5
PG.isUserInteractionEnabled = true
PG.image = UIImage(named: "c")
g2 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
PG.addGestureRecognizer(g2)
[PG,slider,btn].forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
let percent1 = self.view.frame.height * 0.1
image1Width2 = PG.widthAnchor.constraint(equalTo: view.widthAnchor ,multiplier: 0.06)
image1Height2 = PG.heightAnchor.constraint(equalTo: view.heightAnchor ,multiplier: 0.06)
NSLayoutConstraint.activate([
slider.bottomAnchor.constraint(equalTo: view.bottomAnchor),
slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.06, constant: 0),
slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0),
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
PG.topAnchor.constraint(equalTo: view.topAnchor, constant : percent1),
image1Width2,
image1Height2,
PG.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
slider.addTarget(self, action: #selector(hhh), for: .allEvents)
}
@objc func hhh() {
if jake == true {
image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
image1Height2.constant = CGFloat(slider.value) * view.frame.size.height * 0.10
}
else{
image1Width2.constant = CGFloat(0.5) * view.frame.size.width * 0.10
image1Height2.constant = CGFloat(0.5) * view.frame.size.height * 0.10
}
}
@objc func handleTapGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
}
@objc func g1Method(_ sender: UIPanGestureRecognizer){
jake = true
btnCouter = 1
if jake == true {
guard let child = sender.view else{return}
let transitionPoint = sender.translation(in: self.view)
let newTransition = CGAffineTransform(translationX: transitionPoint.x, y: transitionPoint.y)
switch sender.state {
case .ended,.cancelled:// on End
if let existing = greenTransition {
greenTransition = newTransition.concatenating(existing)
} else {
greenTransition = newTransition
}
default://on change and other states
if let existing = greenTransition {
child.transform = newTransition
.concatenating(existing)
} else {
child.transform = newTransition
}
}
self.view.layoutIfNeeded()
}
}
}