0
我需要通過用戶觸摸在屏幕上將對象左右移動。我已經讓這個對象隨着用戶的觸摸而移動,但是我想這樣做只能讓對象或圖像向右或向左移動。我應該用什麼代碼來做到這一點?這是我現在所擁有的。通過觸摸來回移動對象
import UIKit
class ViewController: UIViewController {
@IBOutlet var Person: UIImageView!
var location = CGPoint (x: 0, y: 0)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
Person.center = location
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
Person.center = location
}
override func viewDidLoad() {
super.viewDidLoad()
Person.center = CGPointMake(160, 330)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}