使用标签组合@objc func 方法
use tag to combine @objc func methods
我的代码使用 UIPan gestureRecongnizzer 声明了 2 个不同的 @objc func 方法。我认为会有一种方法可以使用一个标签来使用 1 个 objc func 方法。当前所做的所有功能都是移动图像视图。我不知道标签是否是最好的方式,我对其他解决方案持开放态度,我只想要 1 个功能。具体看pgGestureMethod和sgGestureMethod.
var pg = UIImageView()
var pgGesture = UIPanGestureRecognizer()
var pgCon = [NSLayoutConstraint]()
var sg = UIImageView()
var sgGesture = UIPanGestureRecognizer()
var sgCon = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
[pg,sg].forEach({
[=10=].translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview([=10=])
[=10=].isUserInteractionEnabled = true
})
pgGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.pgGestureMethod(_:)))
pg.addGestureRecognizer(pgGesture)
sgGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.sgGestureMethod(_:)))
sg.addGestureRecognizer(sgGesture)
}
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
self.view.bringSubviewToFront(pg)
let tranistioon = sender.translation(in: self.view)
pg.center = CGPoint(x: pg.center.x + tranistioon.x, y: pg.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
@objc func sgGestureMethod(_ sender: UIPanGestureRecognizer){
let tranistioon = sender.translation(in: self.view)
sg.center = CGPoint(x: sg.center.x + tranistioon.x, y: sg.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
欢迎使用 Whosebug。
所以在你的情况下,你可以从手势函数中的参数了解调用了哪个手势。
class SomeViewController: UIViewController {
var pg = UIImageView()
var pgGesture = UIPanGestureRecognizer()
var pgCon = [NSLayoutConstraint]()
var sg = UIImageView()
var sgGesture = UIPanGestureRecognizer()
var sgCon = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
[pg,sg].forEach {[unowned self] in
[=10=].translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview([=10=])
[=10=].isUserInteractionEnabled = true
}
pgGesture = UIPanGestureRecognizer(target: self, action: #selector(gestureDidRecognize(_:)))
pg.addGestureRecognizer(pgGesture)
sgGesture = UIPanGestureRecognizer(target: self, action: #selector(gestureDidRecognize(_:)))
sg.addGestureRecognizer(sgGesture)
}
}
@objc private extension SomeViewController {
func gestureDidRecognize(_ gesture: UIPanGestureRecognizer){
guard let gestureView = gesture.view else { return }
let transition = gesture.translation(in: view)
switch gesture {
case pgGesture:
view.bringSubviewToFront(pg)
default: break
}
gestureView.center = CGPoint(
x: gestureView.center.x + transition.x,
y: gestureView.center.y + transition.y)
gesture.setTranslation(.zero, in: view)
}
}
手势识别器有一个 view
属性 来引用它们添加到的视图,所以你甚至不需要标签!
只需使用一个这样的单一操作方法:
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
// I replaced every "pg" with "sender.view!"
self.view.bringSubviewToFront(sender.view!)
let tranistioon = sender.translation(in: self.view)
sender.view!.center = CGPoint(x: sender.view!.center.x + tranistioon.x, y: sender.view!.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
如果不是故意写self.view.bringSubviewToFront(sg)
,你可以简单地检查sender.view!
:
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
if sender.view == pg {
self.view.bringSubviewToFront(pg)
}
let tranistioon = sender.translation(in: self.view)
sender.view!.center = CGPoint(x: sender.view!.center.x + tranistioon.x, y: sender.view!.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
我的代码使用 UIPan gestureRecongnizzer 声明了 2 个不同的 @objc func 方法。我认为会有一种方法可以使用一个标签来使用 1 个 objc func 方法。当前所做的所有功能都是移动图像视图。我不知道标签是否是最好的方式,我对其他解决方案持开放态度,我只想要 1 个功能。具体看pgGestureMethod和sgGestureMethod.
var pg = UIImageView()
var pgGesture = UIPanGestureRecognizer()
var pgCon = [NSLayoutConstraint]()
var sg = UIImageView()
var sgGesture = UIPanGestureRecognizer()
var sgCon = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
[pg,sg].forEach({
[=10=].translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview([=10=])
[=10=].isUserInteractionEnabled = true
})
pgGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.pgGestureMethod(_:)))
pg.addGestureRecognizer(pgGesture)
sgGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.sgGestureMethod(_:)))
sg.addGestureRecognizer(sgGesture)
}
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
self.view.bringSubviewToFront(pg)
let tranistioon = sender.translation(in: self.view)
pg.center = CGPoint(x: pg.center.x + tranistioon.x, y: pg.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
@objc func sgGestureMethod(_ sender: UIPanGestureRecognizer){
let tranistioon = sender.translation(in: self.view)
sg.center = CGPoint(x: sg.center.x + tranistioon.x, y: sg.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
欢迎使用 Whosebug。
所以在你的情况下,你可以从手势函数中的参数了解调用了哪个手势。
class SomeViewController: UIViewController {
var pg = UIImageView()
var pgGesture = UIPanGestureRecognizer()
var pgCon = [NSLayoutConstraint]()
var sg = UIImageView()
var sgGesture = UIPanGestureRecognizer()
var sgCon = [NSLayoutConstraint]()
override func viewDidLoad() {
super.viewDidLoad()
[pg,sg].forEach {[unowned self] in
[=10=].translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview([=10=])
[=10=].isUserInteractionEnabled = true
}
pgGesture = UIPanGestureRecognizer(target: self, action: #selector(gestureDidRecognize(_:)))
pg.addGestureRecognizer(pgGesture)
sgGesture = UIPanGestureRecognizer(target: self, action: #selector(gestureDidRecognize(_:)))
sg.addGestureRecognizer(sgGesture)
}
}
@objc private extension SomeViewController {
func gestureDidRecognize(_ gesture: UIPanGestureRecognizer){
guard let gestureView = gesture.view else { return }
let transition = gesture.translation(in: view)
switch gesture {
case pgGesture:
view.bringSubviewToFront(pg)
default: break
}
gestureView.center = CGPoint(
x: gestureView.center.x + transition.x,
y: gestureView.center.y + transition.y)
gesture.setTranslation(.zero, in: view)
}
}
手势识别器有一个 view
属性 来引用它们添加到的视图,所以你甚至不需要标签!
只需使用一个这样的单一操作方法:
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
// I replaced every "pg" with "sender.view!"
self.view.bringSubviewToFront(sender.view!)
let tranistioon = sender.translation(in: self.view)
sender.view!.center = CGPoint(x: sender.view!.center.x + tranistioon.x, y: sender.view!.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}
如果不是故意写self.view.bringSubviewToFront(sg)
,你可以简单地检查sender.view!
:
@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
if sender.view == pg {
self.view.bringSubviewToFront(pg)
}
let tranistioon = sender.translation(in: self.view)
sender.view!.center = CGPoint(x: sender.view!.center.x + tranistioon.x, y: sender.view!.center.y + tranistioon.y)
sender.setTranslation(CGPoint.zero,in: self.view)
}