#####做了一个有关iOS AR的demo,功能就是找出周围的红包然后点击消失,这么一个简单的功能。

#####注:demo借鉴的泊学上的

#####github地址

2.项目创建成功之后进入到ViewController.swift里面。里面不需要改什么只需要在ARSKViewDelegate里面返回自己需要的Node对象就行了。

1
2
3
4
5
6
// MARK: - ARSKViewDelegate

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
// Create and configure a node for the anchor added to the view's session.
return SKSpriteNode(imageNamed: "money.png")
}

3.有关AR逻辑和交互的处理是放在scene.swift里面的。所以剩下的代码写在scene.swift里面就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    let labelTitleNode = SKLabelNode()//用于显示当前有多少node存在

var createNodeCount = 0
var currentNodeCount = 0{
didSet{
labelTitleNode.text = "\(currentNodeCount) in your room"
}
}
var myTimer:Timer?//定时器,定时创建node

//属性初始化
override func didMove(to view: SKView) {
// Setup your scene here

labelTitleNode.fontSize = 25
labelTitleNode.color = .white
labelTitleNode.position = CGPoint(x: 0, y: view.frame.midY - 50)
addChild(labelTitleNode)
currentNodeCount = 0

myTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { timer in
self.createNode()
})
}

4.创建node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//定时生成对象
func createNode(){
if currentNodeCount == 10{
myTimer?.invalidate()
myTimer = nil
return
}

currentNodeCount += 1
createNodeCount += 1

//----------生成对象
guard let sceneView = self.view as? ARSKView else { return }
//生成随机位置
let randNumber = GKRandomSource.sharedRandom()
//生成x轴的旋转矩阵
let xRotation = simd_float4x4(SCNMatrix4MakeRotation(randNumber.nextUniform() * Float.pi * 2, 1, 0, 0))
let yRotation = simd_float4x4(SCNMatrix4MakeRotation(randNumber.nextUniform() * Float.pi * 2, 0, 1, 0))
//合成坐标
let rotation = simd_mul(xRotation, yRotation)
var translation = matrix_identity_float4x4
translation.columns.3.z = -1//摄像头前面一米

let transform = simd_mul(rotation, translation)

let anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor)
}

5.处理node交互

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   //AR场景交互
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// guard let sceneView = self.view as? ARSKView else {
// return
// }
// // Create anchor using the camera's current position
// if let currentFrame = sceneView.session.currentFrame {
//
// // Create a transform with a translation of 0.2 meters in front of the camera
// var translation = matrix_identity_float4x4
// translation.columns.3.z = -0.2//在距离镜头墙两厘米位置设置了一个对象
// let transform = simd_mul(currentFrame.camera.transform, translation)
//
// // Add a new anchor to the session
// let anchor = ARAnchor(transform: transform)
// sceneView.session.add(anchor: anchor)
// }

//ar交互
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let currentNodes = nodes(at: location)
if let node = currentNodes.first{
currentNodeCount -= 1
//放大渐变消失
let scaleOut = SKAction.scale(by: 2, duration: 0.2)
let fadeOut = SKAction.fadeOut(withDuration: 0.2)
let group = SKAction.group([scaleOut, fadeOut])
let sequence = SKAction.sequence([group, SKAction.removeFromParent()])

node.run(sequence)

if currentNodeCount == 0, createNodeCount == 10{
labelTitleNode.removeFromParent()
self.addChild(SKSpriteNode(imageNamed: "game_over"))
}
}
}

相关文章
评论
分享
  • 删除视频背景音乐、添加视频背景音乐

    删除视频背景音乐删除视频的背景音乐其实很简单,把视频数据导入进来,然后我们的视频数据其实是有两条轨道一条是音频,另外一条是视频。由于数据的tracks(轨道包含多条音视频)是只读的,所以在这里重新创建一个混合器,然后把数据源的视频轨道...

    删除视频背景音乐、添加视频背景音乐
  • AAC音频转WAV

    将aac编码格式的音频转为lpcm编码的音频,转出来的音频会比原来大个10倍左右.1234567891011121314151617181920212223242526272829303132333435363738394041424...

    AAC音频转WAV
  • Message Filtter Extension 短信过滤

    手机最近老是收到各种垃圾短信,于是想要自己写一个app来过滤垃圾短信,找到了Message Filtter Extension 1.首先创建一个空的工程,然后创建好了之后添加一个target。1选中工程->Editor-&g...

    Message Filtter Extension 短信过滤
  • Shell删除所有苹果证书

    123456789101112131415161718#!/bin/bashexpired=$(security find-identity)if [ -z "$expired" ] #-z判断字符串长度是否为0 为0返回tru...

    Shell删除所有苹果证书
  • Swift语言国际化

    第一步在工程设置里面添加对应国际化的语言 第二步添加Localizble.strings 第三步选择Localizble.strings文件,然后选择Localize…选择需要的语言,选择完了之后你就会看见刚才创建的Localizbl...

    Swift语言国际化
  • Docker Nginx 静态资源部署

    1. 查看docker仓库中的nginx命令1docker search nginx 2.为选定需要pull到系统重的官方Nginx镜像1docker pull nginx 这个过程需要时间,需要耐心等待一下。看到这个图就是成功了。 ...

    Docker Nginx 静态资源部署
  • Nginx vue打包部署

    最近在弄vue的项目,完了需要打包部署到服务器,然后之前也没有弄过自己在这边一边查资料一边琢磨怎么弄。幸运的是花了一天半的时间还搞定了。 1.在conf.d文件下添加一个新的配置文件,12345678910111213141516...

    Nginx vue打包部署
  • SCP文件上传下载

    从服务器获取文件1scp -p 8080 root@192.168.1.1:/usr/temp/file.txt /Desktop/ 上传文件到服务器1scp -p 8080 /Desktop/uploadfile root@192....

    SCP文件上传下载
  • 音频剪切、合成、淡入淡出

    音频剪切1.音频剪切比较简单传入音频文件和剪切的时间段就可以了12345678910111213141516171819202122232425/** 初始化输出文件路径 */ NSString *url = "音频源文件路径"...

    音频剪切、合成、淡入淡出
  • swift 做一个答题的功能

    实现的效果是:1.点击答案,判断正确,如果选择的答案错误显示出正确的答案。然后可以浏览上一题的信息。2.可以查看上一题的答题情况。看下效果图吧 思路:1.看看题的模型吧,我这里是用一个数组来装的一个类,然后点击下一题的时候数组索引+...

    swift 做一个答题的功能