Ios 通过触摸前后移动对象
我需要用用户的触摸在屏幕上左右移动一个对象。我已经使用用户触摸的方式移动了对象,但我想让对象或图像只能左右移动。我应该使用什么代码来执行此操作?这是我现在拥有的Ios 通过触摸前后移动对象,ios,swift,Ios,Swift,我需要用用户的触摸在屏幕上左右移动一个对象。我已经使用用户触摸的方式移动了对象,但我想让对象或图像只能左右移动。我应该使用什么代码来执行此操作?这是我现在拥有的 import UIKit class ViewController: UIViewController { @IBOutlet var Person: UIImageView! var location = CGPoint (x: 0, y: 0) override func touchesBegan(to
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.
}
}
导入UIKit
类ViewController:UIViewController{
@IBVAR个人:UIImageView!
变量位置=CGPoint(x:0,y:0)
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
让触摸:UITouch!=触摸。首先!作为UITouch
location=touch!.locationInView(self.view)
Person.center=位置
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:UITouch!=触摸。首先!作为UITouch
location=touch!.locationInView(self.view)
Person.center=位置
}
重写func viewDidLoad(){
super.viewDidLoad()
Person.center=CGPointMake(160330)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
这应该行得通。您可以更改y以更改UIImageView始终保持打开的方式,但请确保两者相同!至少,这会给你一个想法或者让你走上正确的方向
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)
let point = location.x
Person.center = CGPoint(x: point, y: 160) // change 160 to set vertical height you desire
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
let point = location.x
Person.center = CGPoint(x: point, y: 160) // change 160 to set vertical height you desire
}
override func viewDidLoad() {
super.viewDidLoad()
Person.center = CGPointMake(160, 330)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
导入UIKit
类ViewController:UIViewController{
@IBVAR个人:UIImageView!
变量位置=CGPoint(x:0,y:0)
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
让触摸:UITouch!=触摸。首先!作为UITouch
location=touch!.locationInView(self.view)
设点=位置.x
Person.center=CGPoint(x:point,y:160)//更改160以设置所需的垂直高度
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
让触摸:UITouch!=触摸。首先!作为UITouch
location=touch!.locationInView(self.view)
设点=位置.x
Person.center=CGPoint(x:point,y:160)//更改160以设置所需的垂直高度
}
重写func viewDidLoad(){
super.viewDidLoad()
Person.center=CGPointMake(160330)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
我相信有更好的方法,但我仍然是一个初学者,所以