This is my RectangleView:
import UIKit
class RectangleView: UIView
{
    init(frame: CGRect)
    {
        // Initialization code
        super.init(frame: frame)
    }
    /*
    override func drawRect(rect: CGRect)
    {
        // Drawing code
    }
    */
}
I add this RectangleView on my 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)
    }
...
And I get a error message
'RectangleView?' does not have a member named 'center'
Should I define this center on my RectangleView? My RectangleView inherits from UIView, so I feel very confused. Thanks your help.
Edit
I use this code
func moveBall()
{
    if let rv = self.rectangleView {
        rv.center = CGPointMake(rv.center + self.pos!.x, rv.center + self.pos!.y)
    }
I get the new error message
'CGPoint' is not convertible to 'UInt8'
I think the rv.center type is UInt8, but self.post!.x is nsnumber, so I how could I convert to CGPointMake accept type? I have convert UInt8(self.pos!.x) and convert calculate result to CGPointMake accept type again? 
Have any other best method? Thanks your help.
The values returned by !-unwrapping an optional are currently immutable (see Lattner's explanation).
Try:
if let rv = self.rectangleView {
  rv.center = ...
}
I think this can be considered a known bug in Swift.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With