背景:每次调用 addButtonClicked
操作时,我都需要在内部创建一个具有不同编号和名称的可拖动球。无法真正理解这里的方法。球正确显示并四处移动,但如何为每个新球添加不同的数据?
是否可以为 UIView 创建类似于原型(prototype)单元格的东西?
class ViewController: UIViewController {
//VAR
var playerName = ""
var number = ""
var balls = [UIView]()
@IBOutlet weak var ball: UIView!
@IBOutlet weak var addButton: UIBarButtonItem!
@IBOutlet weak var numberLabel: UILabel!
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
// ACTIONS
@IBAction func addButtonClicked(_ sender: Any) {
let newBall=UIView(frame: CGRect(x: 10, y: 100, width: 50, height: 50))
// Change UIView background colour
newBall.backgroundColor=UIColor.white
// Add rounded corners to UIView
newBall.layer.cornerRadius=25
// Add border to UIView
newBall.layer.borderWidth=0
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
newBall.addGestureRecognizer(gestureRecognizer)
// Add UIView as a Subview
self.view.addSubview(newBall)
balls.append(newBall)
print ([balls])
}
编辑:
当我添加 let player1 = mycustomUIview())
它说不能调用非函数类型 'mycustomUIView' 的值
如果我在 ViewController 中调用 mycustomUiView.instantiatefromNib
它会显示整个 customView。
我的想法只是按下一个 + 按钮并创建不同的可拖动球,其中包含要更改的数据(屏幕截图引用:https://imgur.com/a/fRiTE)
我尝试创建不同的 UIView,甚至为每个玩家创建一个不同的 viewController,但没有成功,我无法在网上找到一个可行的解决方案。
MyCustomUIViewController(从 MyCustomUIView.xib 连接的 socket )
class playerBall: UIView {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var contentViewBall: UIView!
@IBOutlet weak var contentViewNumberLabel: UILabel!
@IBOutlet weak var contentViewNameLabel: UILabel!
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// Performs the initial setup.
private func setupView() {
let view = viewFromNibForClass()
view.frame = bounds
// Auto-layout
view.autoresizingMask = [
UIViewAutoresizing.flexibleWidth,
UIViewAutoresizing.flexibleHeight
]
// Show view.
addSubview(view)
}
// Load XIB file into view and return this view.
func viewFromNibForClass() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
主视图 Controller
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
playerBall.contentViewNameLabel.text = "Player"
playerBall.contentViewNumberLabel.text = "10"
}
@IBOutlet weak var playerBall: playerBall!
@IBAction func buttonPressed(_ sender: Any) {
var customView = playerBall.viewFromNibForClass()
self.view.addSubview(customView)
}
}
最佳答案
我使用以下方法创建了您想要的效果:-
我创建了一个名为 PlayerBall.xib 的 xib 文件,我添加了标签并将背景颜色更改为清除颜色..
然后我创建了一个新的 coca touch 类 PlayerBall.swift,它是 UIView 的一个子类。
将 xib 文件的类更改为 PlayerBall 而不是标准的 UIView
将标签的导出连接到新创建的类。它应该如下所示:
class PlayerBall: UIView {
@IBOutlet weak var playerNumber: UILabel!{
didSet{
playerNumber.layer.cornerRadius = playerNumber.frame.width / 2
playerNumber.layer.masksToBounds = true
}
}
@IBOutlet weak var playerName: UILabel!
class func instanceFromNib() -> PlayerBall {
return UINib(nibName: "PlayerBall", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! PlayerBall
}
}
现在在主 viewController 中测试我添加的代码
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...5 {
let ball = PlayerBall.instanceFromNib()
ball.playerNumber.text = "\(i)"
ball.playerName.text = "Player " + ball.playerNumber.text!
let randomX = arc4random_uniform(UInt32(view.frame.width))
let randomY = arc4random_uniform(UInt32(view.frame.height))
ball.center = CGPoint(x: CGFloat(randomX), y: CGFloat(randomY))
view.addSubview(ball)
}
}
您可以向自定义类添加更多功能,我只是将其保留为基本概念以作为概念证明。
希望对你有帮助
关于ios - 以编程方式添加一个新的 UIView,其中包含不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46136304/