Swift: 如何设置max/min为平移手势?
Swift: How to set max/min to pan gesture?
注意:我已经尝试过我找到的其他答案,但没有任何效果。
我需要为 UIView 设置一个屏幕限制,我可以在我的主视图周围拖动(这样我就不能将它拖到视图之外)。另外我想知道如何让这个屏幕限制适应不同的设备。
@IBAction func handlePan(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)
}
下面展示了如何在视图控制器 (UIViewController
) 的主视图上平移 UILabel
控件,以及如何将其平移移动限制在后者的可见边界内。请注意,假定状态栏是可见的。所以上边界由状态栏的高度决定。
import UIKit
class ViewController: UIViewController {
// MARK: - Variables
// MARK: - IBOutlet
@IBOutlet weak var label: UILabel!
// MARK: - IBAction
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(panGesture)
}
@objc func handlePan(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
let statusFrame = UIApplication.shared.statusBarFrame
if let senderView = sender.view {
if senderView.frame.origin.x < 0.0 {
senderView.frame.origin = CGPoint(x: 0.0, y: senderView.frame.origin.y)
}
if senderView.frame.origin.y < statusFrame.height {
senderView.frame.origin = CGPoint(x: senderView.frame.origin.x, y: statusFrame.height)
}
if senderView.frame.origin.x + senderView.frame.size.width > view.frame.width {
senderView.frame.origin = CGPoint(x: view.frame.width - senderView.frame.size.width, y: senderView.frame.origin.y)
}
if senderView.frame.origin.y + senderView.frame.size.height > view.frame.height {
senderView.frame.origin = CGPoint(x: senderView.frame.origin.x, y: view.frame.height - senderView.frame.size.height)
}
}
if let centerX = sender.view?.center.x, let centerY = sender.view?.center.y {
sender.view?.center = CGPoint.init(x: centerX + translation.x , y: centerY + translation.y)
sender.setTranslation(CGPoint.zero, in: self.view)
}
}
}
注意:我已经尝试过我找到的其他答案,但没有任何效果。
我需要为 UIView 设置一个屏幕限制,我可以在我的主视图周围拖动(这样我就不能将它拖到视图之外)。另外我想知道如何让这个屏幕限制适应不同的设备。
@IBAction func handlePan(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)
}
下面展示了如何在视图控制器 (UIViewController
) 的主视图上平移 UILabel
控件,以及如何将其平移移动限制在后者的可见边界内。请注意,假定状态栏是可见的。所以上边界由状态栏的高度决定。
import UIKit
class ViewController: UIViewController {
// MARK: - Variables
// MARK: - IBOutlet
@IBOutlet weak var label: UILabel!
// MARK: - IBAction
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(panGesture)
}
@objc func handlePan(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
let statusFrame = UIApplication.shared.statusBarFrame
if let senderView = sender.view {
if senderView.frame.origin.x < 0.0 {
senderView.frame.origin = CGPoint(x: 0.0, y: senderView.frame.origin.y)
}
if senderView.frame.origin.y < statusFrame.height {
senderView.frame.origin = CGPoint(x: senderView.frame.origin.x, y: statusFrame.height)
}
if senderView.frame.origin.x + senderView.frame.size.width > view.frame.width {
senderView.frame.origin = CGPoint(x: view.frame.width - senderView.frame.size.width, y: senderView.frame.origin.y)
}
if senderView.frame.origin.y + senderView.frame.size.height > view.frame.height {
senderView.frame.origin = CGPoint(x: senderView.frame.origin.x, y: view.frame.height - senderView.frame.size.height)
}
}
if let centerX = sender.view?.center.x, let centerY = sender.view?.center.y {
sender.view?.center = CGPoint.init(x: centerX + translation.x , y: centerY + translation.y)
sender.setTranslation(CGPoint.zero, in: self.view)
}
}
}