Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions Label3D/Label3DView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Label3DView: UIView {
*/
public override init(frame: CGRect) {
super.init(frame: frame)
let panGesture = UIPanGestureRecognizer(target: self, action: "handelPan:")
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handelPan(gesture:)))
self.addGestureRecognizer(panGesture)
}

Expand All @@ -29,24 +29,30 @@ public class Label3DView: UIView {

public var perspective:Float = 2000
public var fontSize:CGFloat = 15
public var fontColor:UIColor = UIColor.blackColor()
public var fontColor:UIColor = UIColor.black
public var sphereRadius:Float = 0.5
public var onEachLabelClicked : ((label:UILabel)->Void)?
public var onEachLabelClicked : ((_ label:UILabel)->Void)?


var titles = [LabelSphere]()
public func loadLabelsFromFile(fpath:String) {
if let content = try? String(contentsOfFile: fpath, usedEncoding: nil) {
let lines = content.componentsSeparatedByString("\n")

do {
let content = try String(contentsOfFile:fpath, encoding: .utf8)
let lines = content.components(separatedBy: "\n")
for t in lines {
let label = LabelSphere(text: t)
label.textAlignment = .Center
label.textAlignment = .center
titles.append(label)
}
} catch {

}

}

public func resetLabelOnView() {
let PI = Float(M_PI)
let PI = Float(Double.pi)
let radius = sphereRadius*perspective
let cx = Float(self.frame.width/2)
let cy = Float(self.frame.height/2)
Expand All @@ -62,24 +68,27 @@ public class Label3DView: UIView {
sum_circle += c
}
let dc = Float(titles.count)/sum_circle
var circle_num = [Int](count: circles.count, repeatedValue: 0)
var circle_num = [Int].init(repeating: 0, count: circles.count)

var sum_label = 0
for i in 1 ..< Int(rowNum) {
circle_num[i-1] = (Int(dc*circles[i-1]) + 1)
sum_label += circle_num[i-1]
}
var locLabel = [CGPoint](count:sum_label, repeatedValue: CGPoint())

var locLabel = [CGPoint].init(repeating: CGPoint(), count: sum_label)
var li = 0
for (i, cn) in circle_num.enumerate() {
for (i, cn) in circle_num.enumerated() {
for j in 0 ..< cn {
locLabel[li].y = CGFloat(d_ry * Float(i+1))
locLabel[li++].x = CGFloat(2*PI/Float(cn)*Float(j))
locLabel[li].x = CGFloat(2*PI/Float(cn)*Float(j))
li+=1
}
}

for (i, label) in titles.enumerate() {
for (i, label) in titles.enumerated() {
label.perspective = perspective
label.font = UIFont.systemFontOfSize(fontSize)
label.font = UIFont.systemFont(ofSize: fontSize)
label.textColor = fontColor
label.sizeToFit()
let rxz = locLabel[i].x
Expand All @@ -94,18 +103,18 @@ public class Label3DView: UIView {
}
}
// 可以继承该函数,修改旋转速度
public func panGestureCallback(gesture: UIPanGestureRecognizer) ->(Float,Float) {
let point = gesture.translationInView(self)
public func panGestureCallback(_ gesture: UIPanGestureRecognizer) ->(Float,Float) {
let point = gesture.translation(in: self)

let d_rx = Float(point.x/self.frame.width)
let d_ry = Float(point.y/self.frame.height)

return (d_rx, d_ry)
}
func handelPan(gesture: UIPanGestureRecognizer) {
@objc func handelPan(gesture: UIPanGestureRecognizer) {
let (d_rx, d_ry) = panGestureCallback(gesture)
scrollX(d_rx)
scrollY(d_ry)
scrollX(th: d_rx)
scrollY(th: d_ry)
}
// 绕着y轴转,和xz平面的夹角变,但是ry不变
func scrollX(th:Float) {
Expand All @@ -120,25 +129,25 @@ public class Label3DView: UIView {
}
}

override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let pos = touch.locationInView(self)
let pos = touch.location(in: self)
var zIndex = -Float.infinity
var topLabel:UILabel?
for l in titles {
if CGRectContainsPoint(l.frame, pos) {
if l.frame.contains(pos) {
if Float(l.layer.zPosition) > zIndex {
zIndex = Float(l.layer.zPosition)
topLabel = l
}
}
}
if topLabel != nil {
self.onEachLabelClicked?(label: topLabel!)
self.onEachLabelClicked?(topLabel!)
}
}

}

}


Expand Down Expand Up @@ -182,7 +191,7 @@ class LabelSphere: UILabel {
let (_, py, pz) = getXYZ()
let pyz = sqrt(py*py + pz*pz)
let ryz = acos(pz/pyz)
let PI = Float(M_PI)
let PI = Float(Double.pi)
if py < 0 {
return 2*PI - ryz
} else {
Expand All @@ -197,7 +206,7 @@ class LabelSphere: UILabel {
let pxz = sqrt(px*px + pz*pz)

ry = acos(py/radius)
let PI = Float(M_PI)
let PI = Float(Double.pi)

let new_rxz = acos(pz/pxz)
if px < 0 {
Expand Down Expand Up @@ -238,12 +247,12 @@ class LabelSphere: UILabel {
}

required init?(coder aDecoder: NSCoder) {
rxz = aDecoder.decodeFloatForKey("rxz")
ry = aDecoder.decodeFloatForKey("ry")
cx = aDecoder.decodeFloatForKey("cx")
cy = aDecoder.decodeFloatForKey("cy")
radius = aDecoder.decodeFloatForKey("radius")
perspective = aDecoder.decodeFloatForKey("perspective")
rxz = aDecoder.decodeFloat(forKey: "rxz")
ry = aDecoder.decodeFloat(forKey: "ry")
cx = aDecoder.decodeFloat(forKey: "cx")
cy = aDecoder.decodeFloat(forKey: "cy")
radius = aDecoder.decodeFloat(forKey: "radius")
perspective = aDecoder.decodeFloat(forKey: "perspective")
super.init(coder: aDecoder)
}

Expand All @@ -252,7 +261,7 @@ class LabelSphere: UILabel {
self.layer.position.x = CGFloat(sin(rxz) * pxz + cx)
let old_pz = self.layer.zPosition
self.layer.zPosition = CGFloat(cos(rxz) * pxz)
setTransform3D(self.layer.zPosition - old_pz)
setTransform3D(dz: self.layer.zPosition - old_pz)
self.alpha = self.getAlpha()
}

Expand All @@ -278,4 +287,4 @@ class LabelSphere: UILabel {
return alpha
}

}
}
5 changes: 5 additions & 0 deletions Label3DTest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -373,6 +374,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = flybywind.Label3DTest;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -386,6 +388,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = flybywind.Label3DTest;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -406,6 +409,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -427,6 +431,7 @@
PRODUCT_BUNDLE_IDENTIFIER = flybywind.Label3D;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Label3D.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>Label3DTest.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
</dict>
</plist>
37 changes: 21 additions & 16 deletions Label3DTest/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ViewController: UIViewController {

var label3dView: Label3DView?
var labelDescription = [String:String]()
let fm = NSFileManager.defaultManager()
let bundle = NSBundle.mainBundle()
let fm = FileManager.default
let bundle = Bundle.main
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Expand All @@ -25,28 +25,33 @@ class ViewController: UIViewController {
let py = (frame.height - height)/2
frame = CGRect(x:px, y:py, width: width, height: height)
label3dView = Label3DView(frame: frame)
label3dView?.layer.backgroundColor = UIColor.blackColor().CGColor
label3dView?.layer.backgroundColor = UIColor.black.cgColor
self.view.addSubview(label3dView!)
if let fpath = bundle.pathForResource("108", ofType: "txt") {
label3dView?.loadLabelsFromFile(fpath)
if let fpath = bundle.path(forResource: "108", ofType: "txt") {
label3dView?.loadLabelsFromFile(fpath: fpath)
label3dView?.perspective = Float(width)
label3dView?.fontColor = UIColor.yellowColor()
label3dView?.fontColor = UIColor.yellow
label3dView?.sphereRadius = 0.3
label3dView?.fontSize = 25
label3dView?.fontSize = 10//25
label3dView?.onEachLabelClicked = self.clickEachLabel()
label3dView?.resetLabelOnView()
}

if let fpath = bundle.pathForResource("108.des", ofType: "txt") {
if let content = try? String(contentsOfFile: fpath, usedEncoding: nil) {
let lines = content.componentsSeparatedByString("\n")
if let fpath = bundle.path(forResource: "108.des", ofType: "txt") {
do {
let content = try String(contentsOfFile:fpath, encoding: .utf8)
let lines = content.components(separatedBy: "\n")
for l in lines {
let seg = l.componentsSeparatedByString(" ")
let seg = l.components(separatedBy: " ")
if seg.count > 1 {
labelDescription[seg[1]] = seg[0]
}
}
} catch {

}


}
}

Expand All @@ -55,15 +60,15 @@ class ViewController: UIViewController {
// Dispose of any resources that can be recreated.
}

func clickEachLabel() -> (UILabel->Void){
func clickEachLabel() -> ((UILabel)->Void){
return {[unowned self] (label:UILabel) in
let ac = UIAlertController(title: label.text!, message: self.labelDescription[label.text!],
preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "确定", style: .Cancel, handler: {
preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "确定", style: .cancel, handler: {
[unowned self] _ in
self.dismissViewControllerAnimated(true, completion: nil)
self.dismiss(animated: true, completion: nil)
}))
self.presentViewController(ac, animated: true, completion: nil)
self.present(ac, animated: true, completion: nil)
}
}
}
Expand Down