我正在尝试使用UISlider创建值更改的子视图,但是addSubview()函数将不会执行。在下面的代码中,有一个addSubView()函数,它被添加为占位符。在我的测试中,第一个打印功能正确执行,但后续打印功能没有。如果两个函数都存在则没有错误,但是如果直接调用addSubview()函数则抛出错误。 代码:
import UIKit
import Foundation
import CoreGraphics
class selectorView: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var jointPicker: UIPickerView!
@IBOutlet weak var verticalSlider: UISlider!
@IBOutlet weak var horizontalSlider: UISlider!
var jointPickerData: [[String]] = [[String]]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.jointPicker.delegate = self
self.jointPicker.dataSource = self
jointPickerData = [["Neck","Shoulder","Elbow","Wrist","Spine","Hip","Knee","Ankle"],["Left","Right"]]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Number of columns
func numberOfComponents(in: UIPickerView) -> Int {
return 2
}
//Number of rows
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return jointPickerData[component].count
}
//Data to return for the row and column being read
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return jointPickerData[component][row]
}
//Catpure the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}
@IBAction func vertical(_ sender: Any) {
if jointPicker.selectedRow(inComponent: 1) == 0 {//left
if jointPicker.selectedRow(inComponent: 0) == 0 {//lNeck
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 1 {//lShoulder
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 2 {//lElbow
lElbowYGlobal.lElbowY = Int((verticalSlider.value - 0.5) * 200)
self.addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 3 {//lWrist
lWristYGlobal.rWristY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 4 {//lSpine
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 5 {//lHip
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 6 {//lKnee
lKneeYGlobal.lKneeY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 7 {//lAnkle
lAnkleYGlobal.lAnkleY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
}
if jointPicker.selectedRow(inComponent: 1) == 1 {//right
if jointPicker.selectedRow(inComponent: 0) == 0 {//rNeck
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 1 {//rShoulder
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 2 {//rElbow
rElbowYGlobal.rElbowY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 3 {//rWrist
rWristYGlobal.rWristY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 4 {//rSpine
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 5 {//rHip
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 6 {//rKnee
rKneeYGlobal.rKneeY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 7 {//rAnkle
rAnkleYGlobal.rAnkleY = Int((verticalSlider.value - 0.5) * 200)
addSubView()
}
}
}
@IBAction func horizontal(_ sender: Any) {
if jointPicker.selectedRow(inComponent: 1) == 0 {//left
if jointPicker.selectedRow(inComponent: 0) == 0 {//lNeck
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 1 {//lShoulder
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 2 {//lElbow
lElbowXGlobal.lElbowX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 3 {//lWrist
lWristXGlobal.lWristX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 4 {//lSpine
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 5 {//lHip
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 6 {//lKnee
lKneeXGlobal.lKneeX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 7 {//lAnkle
lAnkleXGlobal.lAnkleX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
}
if jointPicker.selectedRow(inComponent: 1) == 1 {//right
if jointPicker.selectedRow(inComponent: 0) == 0 {//rNeck
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 1 {//rShoulder
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 2 {//rElbow
rElbowXGlobal.rElbowX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 3 {//rWrist
rWristXGlobal.rWristX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 4 {//rSpine
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 5 {//rHip
//nothing yet
}
if jointPicker.selectedRow(inComponent: 0) == 6 {//rKnee
rKneeXGlobal.rKneeX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
if jointPicker.selectedRow(inComponent: 0) == 7 {//rAnkle
rAnkleXGlobal.rAnkleX = Int((horizontalSlider.value - 0.5) * 200)
addSubView()
}
}
}
/*func drawLineFromPointToPoint(startX: Int, toEndingX endX: Int, startingY startY: Int, toEndingY endY: Int, ofColor lineColor: UIColor, widthOfLine lineWidth: CGFloat, inView view: UIView) {
let path = UIBezierPath()
path.move(to: CGPoint(x: startX, y: startY))
path.addLine(to: CGPoint(x: endX, y: endY))
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = lineWidth
view.layer.addSublayer(shapeLayer)
}*/
func addSubView(){
print("subview requested")
func addSubview(_ preview: UIView) {
print("processing request")
let preview = UIView(frame: CGRect(x: 0, y: 127, width: 1198, height: 449))
preview.center = CGPoint(x: 1198/2, y: 127+449/2)
preview.backgroundColor = UIColor.white
preview.clipsToBounds = true
self.view.addSubview(preview)
let circle = UIView(frame: CGRect(x: 560, y: 10.0, width: 100.0, height: 100.0))
circle.center = CGPoint(x: 560, y: 60)
circle.layer.cornerRadius = 50
circle.backgroundColor = UIColor.black
circle.clipsToBounds = true
let circle2 = UIView(frame: CGRect(x: 25.0, y: 25.0, width: 90.0, height: 90.0))
circle2.center = circle.center
circle2.layer.cornerRadius = 45
circle2.backgroundColor = UIColor.white
circle2.clipsToBounds = true
/*
var darkBlur = UIBlurEffect(style: UIBlurEffectStyle.dark)
var blurView = UIVisualEffectView(effect: darkBlur)
blurView.frame = circle.bounds
circle.addSubview(blurView)
*/
preview.addSubview(circle)
preview.addSubview(circle2)
let spine = UIView(frame: CGRect(x: 560, y: 110, width: 10, height: 110))
func spineDraw(){
spine.center = CGPoint(x: 560, y: 165)
spine.layer.cornerRadius = 5
spine.backgroundColor = UIColor.black
spine.clipsToBounds = true
preview.addSubview(spine)
}
var shoulder = CGPoint(x: 400, y: 120)
var rElbow = CGPoint(x: 410 + rElbowXGlobal.rElbowX, y: 170 + rElbowYGlobal.rElbowY)
var rWrist = CGPoint(x: 410 + rWristXGlobal.rWristX, y: 195 + rWristYGlobal.rWristY)
let rArm = UIBezierPath()
rArm.lineWidth = 5
func rArmDraw() {
rArm.move(to: shoulder)
rArm.addLine(to: rElbow)
rArm.stroke()
rArm.addLine(to: rWrist)
rArm.stroke()
rArm.close()
}
var lElbow = CGPoint(x: 390 + lElbowXGlobal.lElbowX, y: 170 + lElbowYGlobal.lElbowY)
var lWrist = CGPoint(x: 390 + lWristXGlobal.lWristX, y: 195 + lWristYGlobal.lWristY)
let lArm = UIBezierPath()
lArm.lineWidth = 5
func lArmDraw(){
lArm.move(to: shoulder)
lArm.addLine(to: lElbow)
lArm.stroke()
lArm.addLine(to: lWrist)
lArm.stroke()
lArm.close()
}
var hip = CGPoint(x: 400, y: 215)
var rKnee = CGPoint(x: 410 + rKneeXGlobal.rKneeX, y: 250 + rKneeYGlobal.rKneeY)
var rAnkle = CGPoint(x: 410 + rAnkleXGlobal.rAnkleX, y: 270 + rAnkleYGlobal.rAnkleY)
let rLeg = UIBezierPath()
rLeg.lineWidth = 5
func rLegDraw(){
rLeg.move(to: hip)
rLeg.addLine(to: rKnee)
rLeg.stroke()
rLeg.addLine(to: rAnkle)
rLeg.stroke()
rLeg.close()
}
var lKnee = CGPoint(x: 390 + lKneeXGlobal.lKneeX, y: 250 + lKneeYGlobal.lKneeY)
var lAnkle = CGPoint(x: 390 + lAnkleXGlobal.lAnkleX, y: 270 + lAnkleYGlobal.lAnkleY)
let lLeg = UIBezierPath()
lLeg.lineWidth = 5
func lLegDraw(){
lLeg.move(to: hip)
lLeg.addLine(to: lKnee)
lLeg.stroke()
lLeg.addLine(to: lAnkle)
lLeg.stroke()
lLeg.close()
}
//debug grid
let path = UIBezierPath()
for i in 1...10 {
path.move(to: CGPoint(x: i*100, y: 0))
path.addLine(to: CGPoint(x: i*100, y: 1000))
path.stroke()
path.move(to: CGPoint(x: 0, y: i*100))
path.addLine(to: CGPoint(x: 2000, y: i*100))
}
spineDraw()
rArmDraw()
lArmDraw()
rLegDraw()
lLegDraw()
print("subview added")
}
}
}