0

I want to move an UIImageView, I already did this in Objective-C and it completely worked. This was the code:

tank.center = CGPointMake(tank.center.x + TankX, tank.center.y + TankY);

TankX is set in viewDidLoad to 2 and TankY is set to 0. So the tank moves to the right. But when i want to do this in Swift it doesn't work. Swift code:

tank.center = CGPointMake(tank.center.x + TankX, tank.center.y + TankY)

I also tried CGPoint but I also can't get it working. I already saw this question: CGPointMake in Swift but I couldn't manage to use it.

Does anyone have an idea how to do this?

Edit: Error: Could not find an overload for '+' that accepts the supplied arguments

Community
  • 1
  • 1
Bas
  • 4,423
  • 8
  • 36
  • 53

2 Answers2

3

I'd try this:

tank.center = CGPoint(x: tank.center.x + CGFloat(TankX), y: tank.center.y + CGFloat(TankY))

It converts TankX and TankY to CGFloats so they can be added to other CGFloats.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • This was the answer I was looking for, works perfectly! Thank you very much:-) – Bas Jun 22 '14 at 14:11
  • 5
    CGPointMake is deprecated in swift because it has structure initializers which do the same thing, jus as succinctly. The same is true of the other ...Make... functions as well. – David Berry Jun 22 '14 at 16:14
0

tank.center.x and TankX probably don't have the same type. Remember in Swift there is no implicity conversion between Int/Float/Double. You have to cast them explicitly, like: x = Double(y)

Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65