swift uiview 没有名为 center 的成员,无法转换为 UIView
swift uiview does not have a member named center and not convertible to UIView
我创建了一个 de 模型数组 class 'fish' 并希望在我的视图加载时显示在视图上。我收到错误消息:
'Fish' 没有名为 'center' 的成员
'Fish' 不能转换为 'UIView'
请看下面我的代码。我将不胜感激任何帮助。谢谢
这是我的习惯 class:
class Fish {
var imageView = UIImageView(image: UIImage(named: "fish_3"))
}
这是我的视图控制器:
class ViewController: UIViewController {
var fishes:[Fish]
required init(coder aDecoder: NSCoder) {
fishes = [Fish]()
var fish3 = Fish()
fish3.imageView = UIImageView(image: UIImage(named: "fish_3"))
fishes.append(fish3)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
for fish in fishes {
var x = CGFloat(arc4random_uniform(120))
var y = CGFloat(arc4random_uniform(200))
fish.center.x = CGPoint(x: x, y: y)
view.addSubview(fish)
}
}
fish.imageView.center = CGPoint(x: x, y: y)
view.addSubview(fish.imageView)
Fish 应该以某种方式从 UIView 继承,最好的方法是使其成为 UIImageView 的子类
class Fish: UIImageView {
override init(frame: CGRect) {
super.init(frame: frame)
// Setup
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Setup
}
}
现在当你初始化它时:
var fish3 = Fish(frame: CGRectMake(0, 0, 50, 50))
fish3.image = UIImage(named: "fish_3")
fishes.append(fish3)
我创建了一个 de 模型数组 class 'fish' 并希望在我的视图加载时显示在视图上。我收到错误消息:
'Fish' 没有名为 'center' 的成员 'Fish' 不能转换为 'UIView'
请看下面我的代码。我将不胜感激任何帮助。谢谢
这是我的习惯 class:
class Fish {
var imageView = UIImageView(image: UIImage(named: "fish_3"))
}
这是我的视图控制器:
class ViewController: UIViewController {
var fishes:[Fish]
required init(coder aDecoder: NSCoder) {
fishes = [Fish]()
var fish3 = Fish()
fish3.imageView = UIImageView(image: UIImage(named: "fish_3"))
fishes.append(fish3)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
for fish in fishes {
var x = CGFloat(arc4random_uniform(120))
var y = CGFloat(arc4random_uniform(200))
fish.center.x = CGPoint(x: x, y: y)
view.addSubview(fish)
}
}
fish.imageView.center = CGPoint(x: x, y: y)
view.addSubview(fish.imageView)
Fish 应该以某种方式从 UIView 继承,最好的方法是使其成为 UIImageView 的子类
class Fish: UIImageView {
override init(frame: CGRect) {
super.init(frame: frame)
// Setup
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Setup
}
}
现在当你初始化它时:
var fish3 = Fish(frame: CGRectMake(0, 0, 50, 50))
fish3.image = UIImage(named: "fish_3")
fishes.append(fish3)