如何使用ARKit识别用户是否单击了屏幕上显示的对象-Swift

时间:2018-07-01 09:29:37

标签: swift uigesturerecognizer arkit iosdeployment

我是一个新的iOS程序。我要构建一个允许用户单击屏幕上显示的特定对象的应用程序。我正在对显示的对象使用addGestureRecognizer,以识别是否已单击用户,然后我只想在屏幕上添加另一个对象。

这是我到目前为止所做的

objpizza = make2dNode(image:#imageLiteral(resourceName: "pizza"),width: 0.07,height: 0.07)
    objpizza.position = SCNVector3(0,0,-0.2)
    objpizza.name = "none"

    self.arView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(selectObject)))
    arView.scene.rootNode.addChildNode(objpizza)

这里的make2dNode功能只是调整对象

func make2dNode(image: UIImage, width: CGFloat = 0.1, height: CGFloat = 0.1) -> SCNNode {
    let plane = SCNPlane(width: width, height: height)
    plane.firstMaterial!.diffuse.contents = image
    let node = SCNNode(geometry: plane)
    node.constraints = [SCNBillboardConstraint()]
    return node
}

这是我在self.arView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(selectObject)))中实现时从未调用过的函数

@objc func selectObject() {

    print("Image has been selected")
}

2 个答案:

答案 0 :(得分:5)

检测SCNNode上的触摸不仅需要向视图中添加UITapGestureRecogniser

为了检测到您触摸了哪个SCNNode,您需要使用SCNHitTest(与您的poseRecognizer结合使用),

  

查找位于指定位置的场景元素的过程   点,或沿着指定的线段(或射线)。

SCNHitTest寻找:

  

沿指定射线的SCNGeometry对象。对于每个路口   在射线和几何之间,SceneKit创建一个命中测试结果   提供有关两个SCNNode对象(包含   几何图形以及该几何图形上相交的位置   表面。

好吧,您可能会想,但这在我的情况下如何实际起作用?

好吧,首先创建一个具有SCNSphere Geometry的SCNNode并将其添加到场景中。

//1. Create An SCNNode With An SCNSphere Geometry
let nodeOneGeometry = SCNSphere(radius: 0.2)

//2. Set It's Colour To Cyan
nodeOneGeometry.firstMaterial?.diffuse.contents = UIColor.cyan

//3. Assign The Geometry To The Node
nodeOne = SCNNode(geometry: nodeOneGeometry)

//4. Assign A Name For Our Node
nodeOne.name = "Node One"

//5. Position It & Add It To Our ARSCNView
nodeOne.position = SCNVector3(0, 0, -1.5)
augmentedRealityView.scene.rootNode.addChildNode(nodeOne)

您会在这里注意到,我已经为SCNNode分配了一个名称,这使跟踪它(例如通过hitTest识别它)变得更加容易。

现在,我们已将SCNNode添加到层次结构中,让我们像这样创建UITapGestureRecognizer

//1. Create A UITapGestureRecognizer & Add It To Our MainView
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(checkNodeHit(_:)))
tapGesture.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapGesture)

现在我们已经完成了所有设置,我们需要创建checkNodeHit函数来检测用户点击的节点:

/// Runs An SCNHitTest To Check If An SCNNode Has Been Hit
///
/// - Parameter gesture: UITapGestureRecognizer
@objc func checkNodeHit(_ gesture: UITapGestureRecognizer){

    //1. Get The Current Touch Location In The View
    let currentTouchLocation = gesture.location(in: self.augmentedRealityView)

    //2. Perform An SCNHitTest To Determine If We Have Hit An SCNNode
    guard let hitTestNode = self.augmentedRealityView.hitTest(currentTouchLocation, options: nil).first?.node else { return }

    if hitTestNode.name == "Node One"{

        print("The User Has Successfuly Tapped On \(hitTestNode.name!)")

    }
}

现在,如果您想将SCNNode放置在用户点击的位置,则必须使用ARSCNHitTest来做到这一点,它提供了:

  

有关通过检查一个点找到的真实表面的信息   AR会话的设备相机视图。

通过执行此操作,我们便可以使用结果的worldTransform属性将虚拟内容放置在该位置。

对于您的信息,worldTransform是:

  

命中测试结果相对于   世界坐标系。

ARKit中的位置可以很容易地像这样可视化:

enter image description here

再次让我们看看如何使用它来放置虚拟对象:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    //1. Get The Current Touch Location In Our ARSCNView & Perform An ARSCNHitTest For Any Viable Feature Points
    guard let currentTouchLocation = touches.first?.location(in: self.augmentedRealityView),
          let hitTest = self.augmentedRealityView.hitTest(currentTouchLocation, types: .featurePoint).first else { return }

    //2. Get The World Transform From The HitTest & Get The Positional Data From The Matrix (3rd Column)
    let worldPositionFromTouch = hitTest.worldTransform.columns.3

    //3. Create An SCNNode At The Touch Location
    let boxNode = SCNNode()
    let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    boxGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
    boxNode.geometry = boxGeometry
    boxNode.position = SCNVector3(worldPositionFromTouch.x, worldPositionFromTouch.y, worldPositionFromTouch.z)

    //4. Add It To The Scene Hierachy
    self.augmentedRealityView.scene.rootNode.addChildNode(boxNode)

}

希望有帮助...

答案 1 :(得分:2)

您需要实施

Declare @auditstring nvarchar(max)


--select * from deleted;

select  * into #temptable from inserted;

while(Exists(select id from #temptable)) --boolean condition if there are rows are not
Begin
    set @auditstring =''

    --if there are many rows we still select the first one

    select Top 1 @Id =id,@newname=name,@newgen=gender,@newsal=salary,@newdeptid=departmentid
    from #temptable;

    select Top 1 @Id =id,@oldname=name,@oldgen=gender,@oldsal=salary,@olddeptid=departmentid
    from deleted where @Id=id;

    set @auditstring=' Employee with id= '+CAST(@Id as varchar(20))+ ' changed '
    if(@oldname<>@newname)
        set @auditstring+= 'name from '+ @oldname +' to' +@newname

    if(@oldsal<>@newsal)
        set @auditstring+= 'salary from ' +CAST(@oldsal AS NVARCHAR(100)) + ' to    ' +CAST(@newsal AS NVARCHAR(100))

    if(@oldgen<>@newgen)
    set @auditstring+= 'gender from ' + @oldgen + ' to  ' + @newgen

    if(@olddeptid<>@newdeptid)
    set @auditstring+= 'departmentid from ' + cast(@olddeptid as nvarchar(5))+' to'+cast(@newdeptid as nvarchar(5)) 

insert into emp_audit values(@auditstring)

delete from #temptable where id=@Id

end

用于功能识别是否已单击用户

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
arView.addGestureRecognizer(tapGesture)

享受编码兄弟。