gpt4 book ai didi

ios - 从另一个类调用 iOS 平移和捏合识别器

转载 作者:行者123 更新时间:2023-11-29 01:50:00 24 4
gpt4 key购买 nike

我正在使用 Swift 在 iOS 中开发 SpriteKit 应用程序,并且我已经为我的 map 实现了平移和捏合手势。代码如下所示:

    //Zoom recognizer
let pinch: UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinch:"))
view.addGestureRecognizer(pinch)

// Move recognizer
let pan: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector("pan:"))
view.addGestureRecognizer(pan)


func pinch(sender: UIPinchGestureRecognizer){

var anchorPoint: CGPoint = sender.locationInView(sender.view)
anchorPoint = convertPointFromView(anchorPoint)

if (sender.state == UIGestureRecognizerState.Changed) {

let anchorPointInMySKNode: CGPoint = world!.convertPoint(anchorPoint, fromNode: self)
world!.setScale(world!.xScale * sender.scale)

let mySKNodeAnchorPointInScene: CGPoint = self.convertPoint(anchorPointInMySKNode, fromNode: world!)
let translationOfAnchorInScene = CGPointSubtract(anchorPoint, point2: mySKNodeAnchorPointInScene)

world!.position = CGPointAdd(world!.position, point2: translationOfAnchorInScene)

sender.scale = 1.0
}
}

func pan(sender: UIPanGestureRecognizer){

if(sender.state == UIGestureRecognizerState.Began){

sender.setTranslation(CGPointZero, inView: sender.view)
}

else if (sender.state == UIGestureRecognizerState.Changed){

var translation: CGPoint = sender.translationInView(sender.view!)
translation = CGPointMake(-translation.x, translation.y)

world!.position = CGPointSubtract(world!.position, point2: translation)
sender.setTranslation(CGPointZero, inView: sender.view)
}
}

代码工作正常,但目前它是在 GameScene.swift 类中编写的,但是如果可以创建一个名为 Gestures.swift 的单独类并调用我想要的任何手势,那就更好了。我尝试了几种方法,但没有一个有效。两个主要问题是如何处理 GestureRecognizer 函数中的选择器以及如何传递父节点(在我的例子中是在 GameScene.swift 中初始化的世界)

最佳答案

关于ios - 从另一个类调用 iOS 平移和捏合识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31515420/

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