gpt4 book ai didi

ios - 在触摸事件上识别 CALayer(将功能代码移植到 Swift)

转载 作者:行者123 更新时间:2023-11-29 01:20:14 27 4
gpt4 key购买 nike

我正在将我自己的一个代码片段从 Objective-C 移植到 Swift,部分是作为学习练习,部分是因为我正在开始一个新应用。

我创建了一个 CALayer-derived 类,它包含一个子层 CALayers 数组。我将其用作图标的扩展菜单。如果有帮助,Obj-C 存储库就在这里: https://github.com/lofdev/AnimatedIconDrawer .

该类的实例嵌入在 UIViewController 中,在 Objective-C 中,我使用它来调用实例,引用子子层,这样我可以对实例做一些有趣的事情。

 // _drawer is the instance of a CALayer-derived class
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {
for (UITouch *touch in touches) {
CGPoint point = [touch locationInView:[touch view]];
point = [[touch view] convertPoint:point toView:nil];
CALayer *layer = [(CALayer *)self.view.layer.presentationLayer hitTest:point];

NSInteger clicked_item = [_drawer toggleOpenCloseWithTappedLayer:layer];
}
}
}

无论如何,我已经将代码移植到 Swift,但我似乎得到了不正确的对象引用。这是移植的代码。

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if touches.count > 0 {
NSLog("Got a touch.")
}
for touch in touches {
let point = touch.locationInView(touch.view)
let point_view = touch.view?.convertPoint(point, toView: nil)

// This line does not work properly... my guess
let point_layer = CALayer(layer: view.layer.presentationLayer()!).hitTest(point_view!)

let clicked_item = drawer.toggleOpenCloseWithTappedLayer(point_layer!)
}
}

我想我做错了什么,但我似乎无法弄清楚。我可以假设这是与指针相关的吗?

任何帮助将不胜感激。还有,请温柔点。我是 Swift 的新手。

谢谢,

最佳答案

尝试替换:

let point_layer = CALayer(layer: view.layer.presentationLayer()!).hitTest(point_view!)

与:

let point_layer = CALayer(layer: (self.view.layer.presentationLayer()?.hitTest(point_view!))!)

迟到

关于ios - 在触摸事件上识别 CALayer(将功能代码移植到 Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701661/

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