Когда объект увеличивается с помощью ползунка. Объект, положение которого не должно перемещаться. Вы можете ясно видеть, что происходит на гифке ниже.
Вы можете видеть, как синее изображение перемещается вправо. Он вообще не должен двигаться. Это как-то связано с
blueMove.leadingAnchor.constraint (equalTo: greenMove.trailingAnchor, constant: 0)])
Я просто не знаю, как получить тот же эффект с кодом, не ограничивая его trailingAnchor greenMove.
import UIKit
class ViewController: UIViewController {
var image1Width: NSLayoutConstraint!
var image1Height: NSLayoutConstraint!
var image1Width2: NSLayoutConstraint!
var image1Height2: NSLayoutConstraint!
var greenMove = UIImageView()
var slider = UISlider()
var image12 = UIImageView()
var blueMove = UIImageView()
var existingTransition : CGAffineTransform?
var clock = Int()
var currentView: UIView?
var g2 = UIPanGestureRecognizer()
var g3 = UIPanGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
greenMove.isUserInteractionEnabled = true
blueMove.isUserInteractionEnabled = true
g2 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g1Method))
greenMove.addGestureRecognizer(g2)
g3 = UIPanGestureRecognizer(target: self, action: #selector(ViewController.g2Method))
blueMove.addGestureRecognizer(g3)
greenMove.backgroundColor = .systemGreen
image12.backgroundColor = .yellow
blueMove.backgroundColor = .blue
[greenMove,image12,slider,blueMove].forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
//image11
image1Width = greenMove.widthAnchor.constraint(equalTo: view.widthAnchor ,multiplier: 0.2)
image1Height = greenMove.heightAnchor.constraint(equalTo: view.heightAnchor ,multiplier: 0.20)
NSLayoutConstraint.activate([greenMove.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
image1Width,
image1Height,
greenMove.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant :0)])
//image12
image1Width2 = blueMove.widthAnchor.constraint(equalTo: view.widthAnchor ,multiplier: 0.2)
image1Height2 = blueMove.heightAnchor.constraint(equalTo: view.heightAnchor ,multiplier: 0.20)
NSLayoutConstraint.activate([blueMove.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
image1Width2,
image1Height2,
blueMove.leadingAnchor.constraint(equalTo: greenMove.trailingAnchor, constant :0)])
slider.addTarget(self, action: #selector(hhh), for: .allEvents)
}
override func viewDidLayoutSubviews() {
NSLayoutConstraint.activate ([
image12.topAnchor.constraint(equalTo: view.topAnchor, constant : greenMove.bounds.height),
image12.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5, constant: 0),
image12.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4, constant: 0),
image12.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
slider.topAnchor.constraint(equalTo: image12.bottomAnchor, constant : 0),
slider.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.20, constant: 0),
slider.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.20, constant: 0),
slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
])
}
@objc func handleTapGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
}
@objc func g1Method(_ sender: UIPanGestureRecognizer){
clock = 1
let subview = image12
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 = existingTransition{
self.existingTransition = newTransition.concatenating(existing)
}else{
self.existingTransition = newTransition
}
default://on change and other states
if let existing = existingTransition{
child.transform = newTransition
.concatenating(existing)
}else{
child.transform = newTransition
}
}
self.view.layoutIfNeeded()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
subview.addGestureRecognizer(tapGesture)
}
@objc func g2Method(_ sender: UIPanGestureRecognizer){
clock = 2
let subview = blueMove
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 = existingTransition{
self.existingTransition = newTransition.concatenating(existing)
}else{
self.existingTransition = newTransition
}
default://on change and other states
if let existing = existingTransition{
child.transform = newTransition
.concatenating(existing)
}else{
child.transform = newTransition
}
}
self.view.layoutIfNeeded()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
subview.addGestureRecognizer(tapGesture)
}
@objc func hhh() {
if clock == 1 {
image1Width.constant = CGFloat(slider.value) * view.frame.size.width * 0.25
image1Height.constant = CGFloat(slider.value) * view.frame.size.height * 0.25
}
if clock == 2 {
image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.25
image1Height2.constant = CGFloat(slider.value) * view.frame.size.height * 0.25
}
}
}