如何:点按时将视图置于最前面;设置 max/min 捏合手势比例;将屏幕限制设置为平移手势
How to: bring view to front on tap; set max/min pinch gesture scale; set screen limit to pan gesture
注意:我发现的一切都太旧或与我实际需要的太不一样,而且我几乎尝试了我发现的一切,但没有一个是正确的。
我创建了 3 个 UIView,我可以拖动它们并缩放它们。
现在,我需要:
- 将其中一些放在前面:我想给每个人添加一个
UITapGesture 允许我将点击的 UIView 带到前面
将其他 UIView 传递到后面;
- 为 UIPinchGestureRecognizer 设置最大/最小比例,以便我
不能有超大的 UIiews 或小的 UIViews;
- 为 UIPanGestureRecognizer 设置一种屏幕限制,这样我
无法将我的 UIView 拖到主视图之外。
这是我的代码:
我的问题解决了!
- I figured out how to set a max/ min scale for the UIPinchGestureRecognizer, here's what I did.
var recognizerScale:CGFloat = 1.0
var maxScale:CGFloat = 1.5
var minScale:CGFloat = 0.8
@IBAction func handlePinch(_ pinchGesture: UIPinchGestureRecognizer) {
guard pinchGesture.view != nil else {return}
if pinchGesture.state == .began || pinchGesture.state == .changed{
if (recognizerScale < maxScale && pinchGesture.scale > 1.0) {
pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
recognizerScale *= pinchGesture.scale
pinchGesture.scale = 1.0
}
if (recognizerScale > minScale && pinchGesture.scale < 1.0) {
pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
recognizerScale *= pinchGesture.scale
pinchGesture.scale = 1.0
}
}
}
- In order to bring views to the front I created multiple UIPanGestureRecognizers and I added bringSubviewToFront:
@IBAction func handlePan1(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)
}
view.bringSubviewToFront(view1)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@IBAction func handlePan2(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)
}
view.bringSubviewToFront(view2)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
哎呀...
但是,如果您还想限制平移手势,则无法执行此操作,因此您可能想添加一个点击手势识别器:
将 gestureRecognizer 添加到您的视图并设置委托。
@IBAction func tappedView1(recognizer: UITapGestureRecognizer) {
view.bringSubviewToFront(myView)
}
在您看来DidLoad:
let tap = UITapGestureRecognizer(target: self, action: #selector(tappedView1))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
- To set a screen limit/ pan limit, I found this code:
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
let statusFrame = UIApplication.shared.statusBarFrame
if let recognizerView = recognizer.view {
if recognizerView.frame.origin.x < 0.0 {
recognizerView.frame.origin = CGPoint(x: 0.0, y: recognizerView.frame.origin.y)
}
if recognizerView.frame.origin.y < statusFrame.height {
recognizerView.frame.origin = CGPoint(x: recognizerView.frame.origin.x, y: statusFrame.height)
}
if recognizerView.frame.origin.x + recognizerView.frame.size.width > view.frame.width {
recognizerView.frame.origin = CGPoint(x: view.frame.width - recognizerView.frame.size.width, y: recognizerView.frame.origin.y)
}
if recognizerView.frame.origin.y + recognizerView.frame.size.height > view.frame.height {
recognizerView.frame.origin = CGPoint(x: recognizerView.frame.origin.x, y: view.frame.height - recognizerView.frame.size.height)
}
}
if let centerX = recognizer.view?.center.x, let centerY = recognizer.view?.center.y {
recognizer.view?.center = CGPoint.init(x: centerX + translation.x , y: centerY + translation.y)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
view.bringSubviewToFront(view1)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
注意:我发现的一切都太旧或与我实际需要的太不一样,而且我几乎尝试了我发现的一切,但没有一个是正确的。
我创建了 3 个 UIView,我可以拖动它们并缩放它们。 现在,我需要:
- 将其中一些放在前面:我想给每个人添加一个 UITapGesture 允许我将点击的 UIView 带到前面 将其他 UIView 传递到后面;
- 为 UIPinchGestureRecognizer 设置最大/最小比例,以便我 不能有超大的 UIiews 或小的 UIViews;
- 为 UIPanGestureRecognizer 设置一种屏幕限制,这样我 无法将我的 UIView 拖到主视图之外。
这是我的代码:
我的问题解决了!
- I figured out how to set a max/ min scale for the UIPinchGestureRecognizer, here's what I did.
var recognizerScale:CGFloat = 1.0
var maxScale:CGFloat = 1.5
var minScale:CGFloat = 0.8
@IBAction func handlePinch(_ pinchGesture: UIPinchGestureRecognizer) {
guard pinchGesture.view != nil else {return}
if pinchGesture.state == .began || pinchGesture.state == .changed{
if (recognizerScale < maxScale && pinchGesture.scale > 1.0) {
pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
recognizerScale *= pinchGesture.scale
pinchGesture.scale = 1.0
}
if (recognizerScale > minScale && pinchGesture.scale < 1.0) {
pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
recognizerScale *= pinchGesture.scale
pinchGesture.scale = 1.0
}
}
}
- In order to bring views to the front I created multiple UIPanGestureRecognizers and I added bringSubviewToFront:
@IBAction func handlePan1(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)
}
view.bringSubviewToFront(view1)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
@IBAction func handlePan2(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)
}
view.bringSubviewToFront(view2)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
哎呀... 但是,如果您还想限制平移手势,则无法执行此操作,因此您可能想添加一个点击手势识别器:
将 gestureRecognizer 添加到您的视图并设置委托。
@IBAction func tappedView1(recognizer: UITapGestureRecognizer) {
view.bringSubviewToFront(myView)
}
在您看来DidLoad:
let tap = UITapGestureRecognizer(target: self, action: #selector(tappedView1))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
- To set a screen limit/ pan limit, I found this code:
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
let statusFrame = UIApplication.shared.statusBarFrame
if let recognizerView = recognizer.view {
if recognizerView.frame.origin.x < 0.0 {
recognizerView.frame.origin = CGPoint(x: 0.0, y: recognizerView.frame.origin.y)
}
if recognizerView.frame.origin.y < statusFrame.height {
recognizerView.frame.origin = CGPoint(x: recognizerView.frame.origin.x, y: statusFrame.height)
}
if recognizerView.frame.origin.x + recognizerView.frame.size.width > view.frame.width {
recognizerView.frame.origin = CGPoint(x: view.frame.width - recognizerView.frame.size.width, y: recognizerView.frame.origin.y)
}
if recognizerView.frame.origin.y + recognizerView.frame.size.height > view.frame.height {
recognizerView.frame.origin = CGPoint(x: recognizerView.frame.origin.x, y: view.frame.height - recognizerView.frame.size.height)
}
}
if let centerX = recognizer.view?.center.x, let centerY = recognizer.view?.center.y {
recognizer.view?.center = CGPoint.init(x: centerX + translation.x , y: centerY + translation.y)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
view.bringSubviewToFront(view1)
recognizer.setTranslation(CGPoint.zero, in: self.view)
}