swift uiview没有名为center的成员

打火机

这是我的RectangleView

import UIKit

class RectangleView: UIView
{
    init(frame: CGRect)
    {
        // Initialization code
        super.init(frame: frame)
    }

    /*
    override func drawRect(rect: CGRect)
    {
        // Drawing code
    }
    */
}

我将此添加RectangleView到我的CustomViewController

import UIKit

class CustomViewController: UIViewController
{
    var ballX:Int = 0
    var ballY:Int = 0
    let BALLWIDTH:Int = 50
    let BALLHEIGHT:Int = 50
    var navigationHeight:Double = 0.0
    var pos:CGPoint?
    var rectangleView:RectangleView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.ballX = Int(arc4random_uniform(320));
        self.ballY = Int(arc4random_uniform(480));
        self.navigationHeight = Double(self.navigationController.navigationBar.frame.size.height)
        var rect = CGRect(x: 0, y: 0, width: self.BALLWIDTH, height: self.BALLHEIGHT)
        self.pos = CGPoint(x: self.ballX, y: self.ballY);

        println("x: \(self.ballX), y: \(self.ballY)")

        self.rectangleView = RectangleView(frame: rect)
        self.rectangleView!.backgroundColor = UIColor.redColor()

        self.view.addSubview(self.rectangleView!)

        NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("moveBall"), userInfo: nil, repeats: true)
    }

    func moveBall()
    {
        self.rectangleView!.center = CGPointMake(self.rectangleView!.center + self.pos!.x, self.rectangleView!.center + self.pos!.y)
    }
...

我收到一条错误消息

'RectangleView?' does not have a member named 'center'

我应该定义这center对我RectangleView我的RectangleView继承人是UIView,所以我感到非常困惑。感谢您的帮助。


编辑

我用这个代码

func moveBall()
{
    if let rv = self.rectangleView {
        rv.center = CGPointMake(rv.center + self.pos!.x, rv.center + self.pos!.y)
    }

我收到新的错误消息

'CGPoint' is not convertible to 'UInt8'

我认为rv.center类型是UInt8,但是self.post!.xnsnumber,所以我如何转换为CGPointMake接受类型?我已经转换UInt8(self.pos!.x)CGPointMake再次将计算结果转换为接受类型?

还有其他最佳方法吗?感谢您的帮助。

账单

!-unwrapping可选参数返回的值当前是不可变的(请参阅Lattner的说明)。

尝试:

if let rv = self.rectangleView {
  rv.center = ...
}

我认为这可以视为Swift中的已知错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swift'没有名为的成员'

Swift ViewController没有名为的成员

Swift-桥接头失败。“ *”没有名为“ *”的成员

Swift-'String'没有名为'isEqualToString'的成员

Swift'WKInterfaceLabel'没有名为'text'的成员

'[细绳]?' 在Swift中没有名为“ count”的成员

Swift 1.2 [AnyObject]没有名为'objectForKey'的成员

Swift NSURLConnection没有名为start的成员

Swift'String.Type'没有名为'stringWithContentsOfFile'的成员

Swift:'((NSObject,AnyObject)'没有名为'subscript'的成员

SWIFT字符串?没有名为“ element”的成员

“ NSURLRequest?” 没有名为“ URL”的成员-Swift

'UICollectionViewCell'没有名为'image'的成员[Swift]

模块“Swift”没有名为“Task”的成员

Swift数组“对象”没有名为下标的成员

Swift SpriteKit:“ MCTFruit没有名为append的成员吗?

Swift代码错误:String.Type没有名为'stringWithContentOfURL'的成员

$ T4没有名为Generator ERROR swift的成员

将变量声明为可选值时,Swift枚举没有名为“ rawValue”的成员

Swift 2.0'Element.Protocol'没有名为'Hour'的成员,NSCalendar.components

为什么AVPlayerView在Swift中没有名为“ player”的成员

Swift iOS8.1选择器没有名为'convertFromStringLiteral'的成员

UIView没有名为'delegate的成员

UIView没有名为“ setTranslatesAutoresizingMaskIntoConstraints”的成员

没有名为的成员

iOS 9 Swift UIView没有成员

“ UINavigationController?” 没有名为“ popToRootViewControllerAnimated”的成员

[AnyObject]?没有名为“下标”的成员

NSURLResponse没有名为allHeaderFields的成员