如何:将视图放在前面;设置最大/最小捏合手势比例;将屏幕限制设置为平移手势 [英] How to: bring view to front on tap; set max/min pinch gesture scale; set screen limit to pan gesture
问题描述
注意:我发现的所有东西都太旧了,或者与我实际需要的东西相差太大,而且我尝试了几乎所有我发现的东西,但没有一种方法是正确的.
我创建了 3 个 UIView,我可以拖动它们并缩放它们.现在,我需要:
- 将其中一些放在前面:我想为每个添加一个UITapGesture 允许我将点击的 UIView 带到前面将其他 UIViews 传递到后面;
- 为 UIPinchGestureRecognizer 设置最大/最小比例,以便我不能有超大的 UIiews 或微小的 UIViews;
- 为 UIPanGestureRecognizer 设置一种屏幕限制,以便我不能将我的 UIViews 拖到主视图之外.
这是我的代码:
https://github.com/marybnq/Views
我解决了我的问题!
<块引用>- 我想出了如何为 UIPinchGestureRecognizer 设置最大/最小比例,这就是我所做的.
变量识别器Scale:CGFloat = 1.0var maxScale:CGFloat = 1.5var minScale:CGFloat = 0.8@IBAction func handlePinch(_ pinchGesture: UIPinchGestureRecognizer) {守卫 pinchGesture.view != nil else {return}如果 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)识别器刻度 *= pinchGesture.scalepinchGesture.scale = 1.0}if (recognizerScale > minScale && pinchGesture.scale <1.0) {pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)识别器刻度 *= pinchGesture.scalepinchGesture.scale = 1.0}}}
<块引用>
- 为了将视图置于最前面,我创建了多个 UIPanGestureRecognizers 并添加了 combineSubviewToFront:
@IBAction func handlePan1(recognizer:UIPanGestureRecognizer) {让翻译=recognizer.translation(in: self.view)如果让视图 = 识别器.视图 {view.center = CGPoint(x:view.center.x + translation.x,y:view.center.y + translation.y)}view.bringSubviewToFront(view1)识别器.setTranslation(CGPoint.zero, in: self.view)}@IBAction func handlePan2(recognizer:UIPanGestureRecognizer) {让翻译=recognizer.translation(in: self.view)如果让视图 = 识别器.视图 {view.center = CGPoint(x:view.center.x + translation.x,y:view.center.y + translation.y)}view.bringSubviewToFront(view2)识别器.setTranslation(CGPoint.zero, in: self.view)}
ecc...但是,如果您也想限制平移手势,则无法执行此操作,因此您可能需要添加一个点击手势识别器:
向您的视图添加一个gestureRecognizer并设置代理.
@IBAction func tappedView1(recognizer: UITapGestureRecognizer) {view.bringSubviewToFront(myView)}
在您的视图中DidLoad:
let tap = UITapGestureRecognizer(target: self, action: #selector(tappedView1))tap.cancelsTouchesInView = falseself.view.addGestureRecognizer(点击)
<块引用>
- 要设置屏幕限制/平移限制,我找到了以下代码:
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {让翻译=recognizer.translation(in: self.view)让 statusFrame = UIApplication.shared.statusBarFrame如果让receiverView =receiver.view {如果receiverView.frame.origin.x <0.0 {识别视图.frame.origin = CGPoint(x:0.0,y:识别视图.frame.origin.y)}如果receiverView.frame.origin.y
NB: everything I found is too old or way too different from what I actually need, also I tried almost everything I found and nothing worked the right way.
I created 3 UIViews, I can drag them around and zoom them. Now, I need to:
- Bring some of them to the front: I want to add to each of them an UITapGesture that allows me to bring the tapped UIView to the front passing the other UIViews to the back;
- Set a max/ min scale for the UIPinchGestureRecognizer so that I can't have super large UIiews or tiny UIViews;
- Set a sort of screen limit to the UIPanGestureRecognizer so that I can't drag my UIViews outside of the main view.
Here's my code:
https://github.com/marybnq/Views
I solved my problem!
- 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)
}
ecc... BUT you can't do this if you want to limit the pan gesture as well, so you might want to add a tap gestureRecognizer instead:
Add a gestureRecognizer to your view and set the delegate.
@IBAction func tappedView1(recognizer: UITapGestureRecognizer) {
view.bringSubviewToFront(myView)
}
In your viewDidLoad:
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)
}
这篇关于如何:将视图放在前面;设置最大/最小捏合手势比例;将屏幕限制设置为平移手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!