Swift Playgrounds 中的 UIGestureRecognizer
Posted
技术标签:
【中文标题】Swift Playgrounds 中的 UIGestureRecognizer【英文标题】:UIGestureRecognizer in Swift Playgrounds 【发布时间】:2017-03-31 23:45:10 【问题描述】:我正在尝试将 UIGestureRecognizer 集成到我的快速操场中,这样无论我在操场上点击什么,我的精灵都会到达那个点。我什么都试过了!观看无数的 youtube 视频,并继续堆栈溢出以寻求答案,但每个人都从创建一个名为 view 的类开始,它是一个 UIView,并且在他们的代码中他们一直提到“self”。相反,我创建了一个名为“view”的变量,它是一个 SKView。我尝试获取他们的部分代码,将其放入我的代码中并进行更改,但它不起作用。这是我目前所做的工作。
view.addGestureRecognizer(UIGestureRecognizer(target: view, action: #selector(handleTap(sender:))))
func handleTap(sender: UITapGestureRecognizer)
player.position = sender.location(in: view)
我的 Playground 一直告诉我我正在使用未解析的标识符 'handleTap(sender:)'
【问题讨论】:
【参考方案1】:在 Playground 中具有不同手势识别器状态的 UIGestureRecognizer 示例
swift3+
import UIKit
import PlaygroundSupport
class ViewController : UIViewController
var yellowView: UIView!
var redView: UIView!
var yellowViewOrigin: CGPoint!
override func loadView()
// UI
let view = UIView()
view.backgroundColor = .white
yellowView = UIView()
yellowView.backgroundColor = .yellow
view.addSubview(yellowView)
redView = UIView()
redView.backgroundColor = .red
view.addSubview(redView)
// Layout
redView.translatesAutoresizingMaskIntoConstraints = false
yellowView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
yellowView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0),
yellowView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0),
yellowView.heightAnchor.constraint(equalToConstant: 80.0),
yellowView.widthAnchor.constraint(equalToConstant: 80.0),
redView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20.0),
redView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0),
redView.heightAnchor.constraint(equalToConstant: 80.0),
redView.widthAnchor.constraint(equalToConstant: 80.0)
])
self.view = view
override func viewDidLoad()
super.viewDidLoad()
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(_:)))
yellowView.addGestureRecognizer(pan)
yellowViewOrigin = yellowView.frame.origin
view.bringSubview(toFront: yellowView)
@objc func handlePanGesture(_ sender: UIPanGestureRecognizer)
let targetView = sender.view!
let translation = sender.translation(in: view)
switch sender.state
case .began,.changed:
targetView.center = CGPoint(x: targetView.center.x + translation.x
,y: targetView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: view)
case .ended:
if targetView.frame.intersects(redView.frame)
UIView.animate(withDuration: 0.3)
targetView.alpha = 0.0
else
UIView.animate(withDuration: 0.3)
targetView.frame.origin = self.yellowViewOrigin
break
default:
break
PlaygroundPage.current.liveView = ViewController()
【讨论】:
【参考方案2】:我认为您需要在函数声明前添加@objc
标记,以便handleTap
对#selector
可见。
@objc func handleTap(sender: UITapGestureRecognizer)
player.position = sender.location(in: view)
【讨论】:
这不起作用,因为我的项目不在类中【参考方案3】:func handleTap
必须从您的 view
类型中调用。
请尝试将其添加到 SKView 扩展中:
extension SKView
func handleTap(sender: UITapGestureRecognizer)
player.position = sender.location(in: view)
【讨论】:
以上是关于Swift Playgrounds 中的 UIGestureRecognizer的主要内容,如果未能解决你的问题,请参考以下文章
如何将视频保存到 Swift Playgrounds 中的 URL
如何解决 Swift Playgrounds 中的“运行此页面时出现问题”消息?
Swift Playgrounds 可以查看同一个项目中的其他源文件吗?
如何将数据从 Playground 页面传递到 Swift Playgrounds 中的另一个 Playground 页面?