Skip to content

Commit

Permalink
增加title
Browse files Browse the repository at this point in the history
  • Loading branch information
fengke committed Feb 28, 2018
1 parent 5d7c73c commit 6586735
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 13 deletions.
6 changes: 6 additions & 0 deletions ARImageViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
445AAA851FEA40370052F741 /* ARImagePreviewView.m in Sources */ = {isa = PBXBuildFile; fileRef = 445AAA831FEA40370052F741 /* ARImagePreviewView.m */; };
445AAAB31FEA4FC80052F741 /* ARImageViewerConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 445AAAB21FEA4FC80052F741 /* ARImageViewerConfig.m */; };
445AAABE1FEA87000052F741 /* c.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 445AAABA1FEA87000052F741 /* c.jpg */; };
44EB313D2046A0AF00324C8C /* ARTextNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 44EB313B2046A0AF00324C8C /* ARTextNode.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -42,6 +43,8 @@
445AAAB11FEA4FC80052F741 /* ARImageViewerConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ARImageViewerConfig.h; sourceTree = "<group>"; };
445AAAB21FEA4FC80052F741 /* ARImageViewerConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ARImageViewerConfig.m; sourceTree = "<group>"; };
445AAABA1FEA87000052F741 /* c.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = c.jpg; sourceTree = "<group>"; };
44EB313B2046A0AF00324C8C /* ARTextNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARTextNode.m; sourceTree = "<group>"; };
44EB313C2046A0AF00324C8C /* ARTextNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARTextNode.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -101,6 +104,8 @@
445AAA831FEA40370052F741 /* ARImagePreviewView.m */,
445AAAB11FEA4FC80052F741 /* ARImageViewerConfig.h */,
445AAAB21FEA4FC80052F741 /* ARImageViewerConfig.m */,
44EB313C2046A0AF00324C8C /* ARTextNode.h */,
44EB313B2046A0AF00324C8C /* ARTextNode.m */,
);
path = ARViewer;
sourceTree = "<group>";
Expand Down Expand Up @@ -182,6 +187,7 @@
445AAA851FEA40370052F741 /* ARImagePreviewView.m in Sources */,
445AAA6B1FEA21F00052F741 /* ViewController.m in Sources */,
445AAA761FEA21F00052F741 /* main.m in Sources */,
44EB313D2046A0AF00324C8C /* ARTextNode.m in Sources */,
445AAA821FEA3F710052F741 /* ARImageNode.m in Sources */,
445AAA681FEA21F00052F741 /* AppDelegate.m in Sources */,
445AAA7F1FEA224B0052F741 /* ARImageViewerViewController.m in Sources */,
Expand Down
Binary file not shown.
41 changes: 28 additions & 13 deletions ARImageViewer/ARViewer/ARImageViewerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#import "ARImageNode.h"
#import "ARImagePreviewView.h"
#import "ARImageViewerConfig.h"
#import "ARTextNode.h"


static const CGFloat StartY = -0.5;

static const NSInteger MAXImageCountInRow = 6;

Expand All @@ -30,10 +34,17 @@ - (void)viewDidLoad {

self.view.backgroundColor = [UIColor whiteColor];

[self viewSetups];

_imageConfig = [ARImageViewerConfig new];

NSArray *imgList = @[@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg",@"z.jpg"];

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScreenAction:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[self.sceneView addGestureRecognizer:tapGestureRecognizer];


//计算行数、 行内最大图片数
float rowF = 1.0 * imgList.count / MAXImageCountInRow;
int rows = ceil(rowF);
Expand All @@ -48,17 +59,7 @@ - (void)viewDidLoad {
[self setupSceneImageWithImages:showList rowIndex:i];
}

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScreenAction:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[self.sceneView addGestureRecognizer:tapGestureRecognizer];

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(20, 20, 40, 40);
backBtn.layer.cornerRadius = 40 / 2;
backBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
[backBtn setTitle:@"Back" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[self.sceneView addSubview:backBtn];
[self addTitleWithText:@"傻孩子! 你怎么会是傻孩子呢?"];
}

- (void)viewWillAppear:(BOOL)animated {
Expand All @@ -79,6 +80,15 @@ - (void)didReceiveMemoryWarning {
// Dispose of any resources that can be recreated.
}

- (void)viewSetups {
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(20, 20, 40, 40);
backBtn.layer.cornerRadius = 40 / 2;
backBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
[backBtn setTitle:@"Back" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[self.sceneView addSubview:backBtn];
}

#pragma mark - Events
- (void)tapScreenAction:(UITapGestureRecognizer *)recognizer {
Expand Down Expand Up @@ -117,7 +127,7 @@ - (ARImageNode *)virtualObjectAtPoint:(CGPoint)point {

- (void)setupSceneImageWithImages:(NSArray *)imgList rowIndex:(NSInteger)rowIndex {

CGFloat yOffset = -0.5;
CGFloat yOffset = StartY;
CGFloat yMargin = 0.06;

NSMutableArray <SCNNode *>*nodeArr = [NSMutableArray arrayWithCapacity:imgList.count];
Expand Down Expand Up @@ -151,7 +161,7 @@ - (void)setupSceneImageWithImages:(NSArray *)imgList rowIndex:(NSInteger)rowInde
[self.sceneView.scene.rootNode addChildNode:node];
[node addChildNode:obj];
node.rotation = SCNVector4Make(0, 1, 0, flag * angleDU);

angleDU += arctan;
angleDU += marginAngle;
}];
Expand All @@ -177,6 +187,11 @@ - (ARImageNode *)fetchNodeWithMaterialImageName:(NSString *)imgName {
return boxNode;
}

- (void)addTitleWithText:(NSString *)text {
ARTextNode *textNode = [[ARTextNode alloc] initWithText:text];
textNode.position = SCNVector3Make(0, 0 , -_imageConfig.imageDepth);
[self.sceneView.scene.rootNode addChildNode:textNode];
}

#pragma mark - Lazy Init
- (ARSCNView *)sceneView {
Expand Down
17 changes: 17 additions & 0 deletions ARImageViewer/ARViewer/ARTextNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ARTextNode.h
// ARImageViewer
//
// Created by fk on 2017/12/20.
// Copyright © 2017年 fk. All rights reserved.
//

#import <SceneKit/SceneKit.h>

@interface ARTextNode : SCNNode

- (instancetype) initWithText:(NSString *)text;

@property (nonatomic, assign) CGFloat textHeight;

@end
72 changes: 72 additions & 0 deletions ARImageViewer/ARViewer/ARTextNode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// ARTextNode.m
// ARImageViewer
//
// Created by fk on 2017/12/20.
// Copyright © 2017年 fk. All rights reserved.
//

#import "ARTextNode.h"

@interface ARTextNode ()

@property (nonatomic, strong) NSString *text;

@end


@implementation ARTextNode

- (instancetype) initWithText:(NSString *)text {

self = [super init];
if (self) {
[self updateTextContent:text];
}
return self;
}

- (void)updateTextContent:(NSString *)text {

_text = text;

CGFloat bubbleDepth = 0.01;

SCNText *textGeo = [SCNText textWithString:text extrusionDepth:bubbleDepth];

textGeo.font = [self getTextFont];
textGeo.alignmentMode = kCAAlignmentCenter;
textGeo.firstMaterial.diffuse.contents = [UIColor blackColor];
textGeo.firstMaterial.specular.contents = [UIColor whiteColor];
textGeo.firstMaterial.doubleSided = YES;
textGeo.chamferRadius = bubbleDepth;

SCNVector3 minBound,maxBound;
[textGeo getBoundingBoxMin:&minBound max:&maxBound];

SCNNode *textNode = [SCNNode nodeWithGeometry:textGeo];
textNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, bubbleDepth/2);
textNode.scale = SCNVector3Make(0.8, 0.8, 0.8);

_textHeight = minBound.y;

// SCNSphere *sphere = [SCNSphere sphereWithRadius:0.005];
// sphere.firstMaterial.diffuse.contents = [UIColor redColor];
// SCNNode *sphereNode = [SCNNode nodeWithGeometry:sphere];
// sphereNode.position = SCNVector3Make(0, 0, 0);
// [textNode addChildNode:sphereNode];

SCNBillboardConstraint *billboardConstraint = [SCNBillboardConstraint billboardConstraint];
billboardConstraint.freeAxes = SCNBillboardAxisY;


[self addChildNode:textNode];
// [textNode addChildNode:sphereNode];
self.constraints = @[billboardConstraint];
}

- (UIFont *)getTextFont {
return [UIFont fontWithName:@"TrebuchetMS-Bold" size:0.15];
}

@end

0 comments on commit 6586735

Please sign in to comment.