如何在iOS中通过触摸前后移动对象
我需要根据用户的手指前后移动图像。我希望能够触摸屏幕,然后图像将移向我的触摸屏。图像应该只从左向右移动,而不是上下移动,我还想对图像可以向屏幕一侧移动的距离进行限制 我知道这听起来像是很多,但我尝试了很多事情,所有这些都导致了问题。我第一次能够点击并拖动图像,这很好,但是当我点击其他地方时,图像就会出现在那里,它不会移动到那里,它只是出现了 我尝试的第二件事允许我拖动图像,但当我点击图像时,它根本不会向手指移动。在这一点上,我非常沮丧,并将感谢任何帮助。这是我的密码如何在iOS中通过触摸前后移动对象,ios,swift,Ios,Swift,我需要根据用户的手指前后移动图像。我希望能够触摸屏幕,然后图像将移向我的触摸屏。图像应该只从左向右移动,而不是上下移动,我还想对图像可以向屏幕一侧移动的距离进行限制 我知道这听起来像是很多,但我尝试了很多事情,所有这些都导致了问题。我第一次能够点击并拖动图像,这很好,但是当我点击其他地方时,图像就会出现在那里,它不会移动到那里,它只是出现了 我尝试的第二件事允许我拖动图像,但当我点击图像时,它根本不会向手指移动。在这一点上,我非常沮丧,并将感谢任何帮助。这是我的密码 import UIKit
import UIKit
class ViewController: UIViewController {
@IBOutlet var Person: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ){
let location = touch.locationInView(self.view)
if Person.frame.contains(location){
Person.center = location
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ){
let location = touch.locationInView(self.view)
if Person.frame.contains(location){
Person.center = location
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
导入UIKit
类ViewController:UIViewController{
@IBVAR个人:UIImageView!
重写func viewDidLoad(){
super.viewDidLoad()
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
用于触摸输入(触摸){
让位置=touch.locationInView(self.view)
如果Person.frame.contains(位置){
Person.center=位置
}
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
用于触摸输入(触摸){
让位置=touch.locationInView(self.view)
如果Person.frame.contains(位置){
Person.center=位置
}
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
我假设您使用的是touchsbegind(:withEvent:)
和touchsmoved(:withEvent:)
来获取触摸事件。这些方法为您提供了一个UITouch
,您可以使用locationInView(:)
将其转换为CGPoint
当触摸开始时(即touchsbegind(:withEvent:)
),您应该将自定义视图设置为触摸的CGPoint
。例如:
UIView.animateWithDuration(0.3, animations: {
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
self.squareView.frame.origin.x = point.x
})
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
squareView.frame.origin.x = point.x
当触摸移动时(即touchsmoved(:withEvent:)
),您应该将自定义视图的位置设置为新触摸的CGPoint
。例如:
UIView.animateWithDuration(0.3, animations: {
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
self.squareView.frame.origin.x = point.x
})
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
squareView.frame.origin.x = point.x
一些建议
UITouch
,这样就可以摆脱for循环行是错误的,因为它只移动Person
,如果触摸在Person
的帧内,请删除该行并使用UITouch
的点设置帧的原点(或使用上面的代码设置动画)
我假设您正在使用
touchesBegind(\uu:withEvent:)
和touchesMoved(\u:withEvent:)
获取触摸事件。这些方法为您提供一个UITouch
,您可以使用locationInView(\u:)
将其转换为CGPoint
当触摸开始时(即touchsbegind(:withEvent:)
),您应该将自定义视图设置为触摸的CGPoint
。例如:
UIView.animateWithDuration(0.3, animations: {
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
self.squareView.frame.origin.x = point.x
})
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
squareView.frame.origin.x = point.x
当触摸移动时(即touchsmoved(:withEvent:)
),您应该将自定义视图的位置设置为新触摸的CGPoint
。例如:
UIView.animateWithDuration(0.3, animations: {
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
self.squareView.frame.origin.x = point.x
})
// Only adjust the x, not the y, to restrict movement to along the x-axis.
// You could also check the x value of point to see if reached some limit.
squareView.frame.origin.x = point.x
一些建议
UITouch
,这样就可以摆脱for循环行是错误的,因为它只移动Person
,如果触摸在Person
的帧内,请删除该行并使用UITouch
的点设置帧的原点(或使用上面的代码设置动画)
您是否可以编辑问题以显示尝试代码?您是否可以编辑问题以显示尝试代码?我添加代码是为了让事情更清楚。我应该如何直接在代码中实现这一点?我在回答中添加了一些建议。我添加代码是为了让事情更清楚。我应该如何直接在代码中实现这一点?我在回答中添加了一些建议。