gpt4 book ai didi

ios - 如何将按钮从弹出窗口拖动到整个应用程序? ( swift )

转载 作者:行者123 更新时间:2023-11-30 11:14:37 25 4
gpt4 key购买 nike

在我的应用程序中,我显示一个弹出窗口,其中显示自定义 UIView,允许用户使用各种 slider 选择颜色。我还想实现一个“吸管”工具,用户可以点击并按住然后拖动以从应用程序中可见的任何颜色中选择颜色。在我的自定义 UIView 中,我向按钮添加了一个 UIPanGestureRecognizer ,该按钮指向 handlePan 方法:

var eyedropperStartLocation = CGPoint.zero
@objc func handlePan(recognizer: UIPanGestureRecognizer) {

// self is a custom UIView that contains my color selection
// sliders and is placed inside a UITableView that's in
// the popover.
let translation = recognizer.translation(in: self)
if let view = recognizer.view {

switch recognizer.state {
case .began:
eyedropperStartLocation = view.center

case .ended, .failed, .cancelled:
view.center = eyedropperStartLocation
return

default: break

}
view.center = CGPoint(x: view.center.x + translation.x,
y: view.center.y + translation.y)
recognizer.setTranslation(CGPoint.zero, in: self)

}
}

我可以拖动按钮并更改位置,但是我有两个问题:

  1. 吸管按钮并不总是位于其他项目的前面,即使是在弹出窗口内或弹出窗口内的自定义 UIView 内
  2. 吸管按钮在弹出框范围之外时消失

如何让按钮始终可见,包括在弹出窗口之外?我想要检测用户在应用程序中松开它的位置,以便我可以确定它的颜色。

最佳答案

我想出了如何做到这一点,所以我会回答我自己的问题。我没有在弹出窗口内的 View /按钮上移动,而是创建一个新的 UIImageView 并将其添加到应用程序的窗口中,让它跨越整个应用程序。原始按钮保留在原来的位置 - 您可以轻松更改其状态以使其看起来不同,或者根据需要隐藏它。

您还可以使用 Interface Builder 来绑定(bind) @IBActions,但我只是在代码中完成了所有操作。 clickButton 方法开始计算窗口中的位置并将其放在屏幕上。 handlePan 方法进行翻译并让您移动它。

下面的所有代码都是 swift 4 并且可以在 XCode 9.4.1 中运行(假设我没有引入任何拼写错误):

// Call this from all your init methods so it will always happen
func commonInit() {
let panner = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
theButton.addGestureRecognizer(panner)
theButton.addTarget(self, action: #selector(clickButton), for: .touchDown)
eyedropperButton.addTarget(self, action: #selector(unclickButton), for: .touchUpInside)
eyedropperButton.addTarget(self, action: #selector(unclickButton), for: .touchUpOutside)
}

var startLocation = CGPoint.zero
lazy var floatingView: UIImageView = {
let view = UIImageView(image: UIImage(named: "imagename"))
view.backgroundColor = UIColor.blue
return view
}()


// When the user clicks button - we create the image and put it on the screen
// this makes the action seem faster vs waiting for the panning action to kick in
@objc func clickButton() {

guard let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window else { return }

// We ask the button what it's bounds are within it's own coordinate system and convert that to the
// Window's coordinate system and set the frame on the floating view. This makes the new view overlap the
// button exactly.
floatingView.frame = theButton.convert(theButton.bounds, to: nil)

window.addSubview(floatingView)

// Save the location so we can translate it as part of the pan actions
startLocation = floatingView.center
}

// This is here to handle the case where the user didn't move enough to kick in the panGestureRecognizer and cancel the action
@objc func unclickButton() {
floatingView.removeFromSuperview()
}

@objc func handlePan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self)

switch recognizer.state {
case .ended, .failed, .cancelled:
doSomething()
floatingView.removeFromSuperview()
return
default: break

}

// This section is called for any pan state except .ended, .failed and .cancelled
floatingView.center = CGPoint(x: floatingView.center.x + translation.x,
y: floatingView.center.y + translation.y)
recognizer.setTranslation(CGPoint.zero, in: self)
}

关于ios - 如何将按钮从弹出窗口拖动到整个应用程序? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com