Swift3 Swift操场中的手势识别器
我正在尝试将UIGestureRecognitor集成到我的swift游乐场中,这样无论我在游乐场中敲击什么,我的精灵都会到达那个点。我什么都试过了!看了无数的youtube视频,然后继续堆栈溢出寻找答案,但每个人都从创建一个名为view的类开始,这个类是UIView,在他们的代码中,他们一直提到“self”。我创建了一个名为“view”的变量,它是一个SKView。我试图获取他们的部分代码,将其放入我的代码中,然后对其进行更改,但它就是不起作用。这是我到目前为止所做的工作Swift3 Swift操场中的手势识别器,swift3,sprite-kit,xcode8,uigesturerecognizer,swift-playground,Swift3,Sprite Kit,Xcode8,Uigesturerecognizer,Swift Playground,我正在尝试将UIGestureRecognitor集成到我的swift游乐场中,这样无论我在游乐场中敲击什么,我的精灵都会到达那个点。我什么都试过了!看了无数的youtube视频,然后继续堆栈溢出寻找答案,但每个人都从创建一个名为view的类开始,这个类是UIView,在他们的代码中,他们一直提到“self”。我创建了一个名为“view”的变量,它是一个SKView。我试图获取他们的部分代码,将其放入我的代码中,然后对其进行更改,但它就是不起作用。这是我到目前为止所做的工作 view.addGe
view.addGestureRecognizer(UIGestureRecognizer(target: view, action: #selector(handleTap(sender:))))
func handleTap(sender: UITapGestureRecognizer) {
player.position = sender.location(in: view)
}
我的游乐场一直告诉我,我正在使用一个未解析的标识符'handleTap(sender:)”我认为您需要在函数声明前面添加
@objc
标记,这样handleTap
将对选择器可见
@objc func handleTap(sender: UITapGestureRecognizer) {
player.position = sender.location(in: view)
}
我认为您需要在函数声明前面添加@objc
标记,以便选择器可以看到handleTap
@objc func handleTap(sender: UITapGestureRecognizer) {
player.position = sender.location(in: view)
}
必须从视图
类型中调用func handleTap
请尝试将其添加到SKView扩展中:
extension SKView {
func handleTap(sender: UITapGestureRecognizer) {
player.position = sender.location(in: view)
}
}
必须从视图
类型中调用func handleTap
请尝试将其添加到SKView扩展中:
extension SKView {
func handleTap(sender: UITapGestureRecognizer) {
player.position = sender.location(in: view)
}
}
具有操场中手势识别器不同状态的手势识别器示例
swift3+
导入UIKit
导入PlaygroundSupport
类ViewController:UIViewController{
var yellowView:UIView!
var redView:UIView!
var yellowViewOrigin:CGPoint!
重写func loadView(){
//用户界面
let view=UIView()
view.backgroundColor=.white
yellowView=UIView()
yellowView.backgroundColor=.yellow
view.addSubview(黄色视图)
redView=UIView()
redView.backgroundColor=.red
view.addSubview(红色视图)
//布局
redView.translatesAutoResizezingMaskintoConstraints=false
yellowView.TranslatesAutoResizezingMaskintoConstraints=假
NSLayoutConstraint.activate([
yellowView.topAnchor.constraint(等式:view.topAnchor,常数:20.0),
yellowView.leadingAnchor.constraint(等式:view.leadingAnchor,常数:20.0),
yellowView.heightAnchor.constraint(相等常量:80.0),
yellowView.widthAnchor.constraint(相等常量:80.0),
redView.bottomAnchor.constraint(等式:view.bottomAnchor,常数:-20.0),
redView.trailingAnchor.constraint(等式:view.trailingAnchor,常数:-20.0),
redView.heightAnchor.constraint(equalToConstant:80.0),
redView.widthAnchor.constraint(equalToConstant:80.0)
])
self.view=view
}
重写func viewDidLoad(){
super.viewDidLoad()
让pan=UIPANGestureRecognitor(目标:self,操作:#选择器(self.handlePangestre(:))
yellowView.AddGestureRecognitor(pan)
yellowViewOrigin=yellowView.frame.origin
视图。bringSubview(toFront:yellowView)
}
@objc func handlePanGesture(发送方:UIPANGESTURE识别器){
让targetView=sender.view!
let translation=sender.translation(在:视图中)
切换发送器状态{
案例。开始,。更改:
targetView.center=CGPoint(x:targetView.center.x+translation.x
,y:targetView.center.y+translation.y)
sender.setTranslation(CGPoint.zero,in:view)
案件结束:
如果targetView.frame.相交(redView.frame){
UIView.animate(持续时间:0.3){
targetView.alpha=0.0
}
}
否则{
UIView.animate(持续时间:0.3){
targetView.frame.origin=self.yellowViewOrigin
}
}
打破
违约:
打破
}
}
}
PlaygroundPage.current.liveView=ViewController()
使用操场中手势识别器的不同状态创建手势识别器示例
swift3+
导入UIKit
导入PlaygroundSupport
类ViewController:UIViewController{
var yellowView:UIView!
var redView:UIView!
var yellowViewOrigin:CGPoint!
重写func loadView(){
//用户界面
let view=UIView()
view.backgroundColor=.white
yellowView=UIView()
yellowView.backgroundColor=.yellow
view.addSubview(黄色视图)
redView=UIView()
redView.backgroundColor=.red
view.addSubview(红色视图)
//布局
redView.translatesAutoResizezingMaskintoConstraints=false
yellowView.TranslatesAutoResizezingMaskintoConstraints=假
NSLayoutConstraint.activate([
yellowView.topAnchor.constraint(等式:view.topAnchor,常数:20.0),
yellowView.leadingAnchor.constraint(等式:view.leadingAnchor,常数:20.0),
yellowView.heightAnchor.constraint(相等常量:80.0),
yellowView.widthAnchor.constraint(相等常量:80.0),
redView.bottomAnchor.constraint(等式:view.bottomAnchor,常数:-20.0),
redView.trailingAnchor.constraint(等式:view.trailingAnchor,常数:-20.0),
redView.heightAnchor.constraint(equalToConstant:80.0),
redView.widthAnchor.constraint(equalToConstant:80.0)
])
self.view=view
}
重写func viewDidLoad(){
super.viewDidLoad()
让pan=UIPANGestureRecognitor(目标:self,操作:#选择器(self.handlePangestre(:))
yellowView.AddGestureRecognitor(pan)
yellowViewOrigin=yellowView.frame.origin
视图。bringSubview(toFront:yellowView)
}
@objc func handlePanGesture(发送方:UIPANGESTURE识别器){
让targetView=sender.view!
让翻译=s