iOS Swift 手势 : 如何 link 两个项目有手势
iOS Swift Gestures : How can I link two items with gestures
我的视图中有 2 个按钮。我创建了 2 个 UIPanGestureRecognizer,每个按钮一个。我可以单独移动它们,但我希望当我向右滑动一个按钮时,另一个向左移动。我真的不知道该怎么做。最佳做法是什么?
@IBAction func button1(_ sender: Any) {}
@IBAction func button2(_ sender: Any) {}
@IBAction func bottomButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y/* + translation.y*/)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@IBAction func topButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x - translation.x,
y:view.center.y/* + translation.y*/)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
在每个平移手势 IBAction 中,您想将未被平移的按钮设置为具有与被平移的按钮相反的 x 位置。
@IBAction func bottomButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x: view.center.x + translation.x,
y: view.center.y)
// Move the other button here too
// Notice we are subtracting the translation.x value instead of adding
button2.center = CGPoint(x: button2.center.x - translation.x,
y: button2.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@IBAction func topButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x: view.center.x + translation.x,
y: view.center.y)
// Move the other button here too
// Notice we are subtracting the translation.x value instead of adding
button1.center = CGPoint(x: button1.center.x - translation.x,
y: button1.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
这是一个可用的 Playground
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
let button1 = UIButton(frame: CGRect(x: 160, y: 100, width: 40, height: 40))
let button2 = UIButton(frame: CGRect(x: 160, y: 200, width: 40, height: 40))
override func loadView() {
super.loadView()
// Tab Views
button1.backgroundColor = .orange
self.view.addSubview(button1)
button2.backgroundColor = .blue
self.view.addSubview(button2)
let panGesture1 = UIPanGestureRecognizer(target: self, action: #selector(button1Pan(recognizer:)))
button1.addGestureRecognizer(panGesture1)
let panGesture2 = UIPanGestureRecognizer(target: self, action: #selector(button2Pan(recognizer:)))
button2.addGestureRecognizer(panGesture2)
}
@objc func button1Pan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y)
// Move the other button here too
button2.center = CGPoint(x: button2.center.x - translation.x,
y: button2.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@objc func button2Pan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y)
// Move the other button here too
button1.center = CGPoint(x: button1.center.x - translation.x,
y: button1.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = ViewController()
我的视图中有 2 个按钮。我创建了 2 个 UIPanGestureRecognizer,每个按钮一个。我可以单独移动它们,但我希望当我向右滑动一个按钮时,另一个向左移动。我真的不知道该怎么做。最佳做法是什么?
@IBAction func button1(_ sender: Any) {}
@IBAction func button2(_ sender: Any) {}
@IBAction func bottomButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y/* + translation.y*/)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@IBAction func topButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x - translation.x,
y:view.center.y/* + translation.y*/)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
在每个平移手势 IBAction 中,您想将未被平移的按钮设置为具有与被平移的按钮相反的 x 位置。
@IBAction func bottomButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x: view.center.x + translation.x,
y: view.center.y)
// Move the other button here too
// Notice we are subtracting the translation.x value instead of adding
button2.center = CGPoint(x: button2.center.x - translation.x,
y: button2.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@IBAction func topButtonGesture
(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x: view.center.x + translation.x,
y: view.center.y)
// Move the other button here too
// Notice we are subtracting the translation.x value instead of adding
button1.center = CGPoint(x: button1.center.x - translation.x,
y: button1.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
这是一个可用的 Playground
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
let button1 = UIButton(frame: CGRect(x: 160, y: 100, width: 40, height: 40))
let button2 = UIButton(frame: CGRect(x: 160, y: 200, width: 40, height: 40))
override func loadView() {
super.loadView()
// Tab Views
button1.backgroundColor = .orange
self.view.addSubview(button1)
button2.backgroundColor = .blue
self.view.addSubview(button2)
let panGesture1 = UIPanGestureRecognizer(target: self, action: #selector(button1Pan(recognizer:)))
button1.addGestureRecognizer(panGesture1)
let panGesture2 = UIPanGestureRecognizer(target: self, action: #selector(button2Pan(recognizer:)))
button2.addGestureRecognizer(panGesture2)
}
@objc func button1Pan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y)
// Move the other button here too
button2.center = CGPoint(x: button2.center.x - translation.x,
y: button2.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@objc func button2Pan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y)
// Move the other button here too
button1.center = CGPoint(x: button1.center.x - translation.x,
y: button1.center.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = ViewController()