【问题标题】:Identifying a CALayer on Touch Event (porting functional code to Swift)在触摸事件上识别 CALayer(将功能代码移植到 Swift)
【发布时间】:2016-01-10 03:16:49
【问题描述】:

我正在将我自己的一个代码 sn-ps 从 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 = (view.layer.presentationLayer() as? CALayer)?.hitTest(point_view!)

标签: ios objective-c swift calayer


【解决方案1】:

尝试替换:

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

与:

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

迟到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多