UIImage去了我点击的地方(但我需要它,所以你只能拖动它)

时间:2016-10-24 00:48:34

标签: ios swift uiimage

我有一个UIImage,我把它链接到我的代码中并将其命名为#34; Person" 在我点击屏幕的那一刻,UIImage跳转到那个位置,但我想要它,所以如果我拖动它,图像只能移动。这是我的代码:

var location = CGPointMake(0, 0)

@IBOutlet var Person: UIImageView!

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()
    // Do any additional setup after loading the view, typically from a nib.

    Person.center = CGPointMake(160, 330)


}

4 个答案:

答案 0 :(得分:1)

我会使用手势识别器 - 更不用说代码了。此外,我使用“三步”手势,以防有多个元素移动。

步骤1:用户点击在子视图和虚线边框上表示子视图处于编辑模式。

步骤2:用户平移子视图到超级视图中的新位置。

步骤3:用户点击超级视图中的任何其他位置(包括新的子视图以进行下一次编辑),使第一个子视图退出编辑模式。

子视图代码:

one

这不是生产代码,所以我从来没有弄清楚如何让“移动的虚线”动画永远运行(这可能是好的,因为它可能是一个性能问题)。但10000秒似乎相当慷慨用于编辑。

这是superview代码,假设你有imageView1和imageView2作为子视图:

var _editMode = false
var editMode:Bool {
    return _editMode
}
var borderPath = UIBezierPath()
var dashedBorder = CAShapeLayer()

func drawDashedBorder() {
    borderPath = UIBezierPath()
    borderPath.move(to: CGPoint(x: 3, y: 3))
    borderPath.addLine(to: CGPoint(x: self.frame.width-3, y: 3))
    borderPath.addLine(to: CGPoint(x: self.frame.width-3, y: self.frame.height-3))
    borderPath.addLine(to: CGPoint(x: 3, y: self.frame.height-3))
    borderPath.close()
    dashedBorder = CAShapeLayer()
    dashedBorder.strokeColor = UIColor.white.cgColor
    dashedBorder.fillColor = UIColor.clear.cgColor
    dashedBorder.lineDashPattern = [2, 2]
    dashedBorder.lineDashPhase = 0.0
    dashedBorder.lineJoin = kCALineJoinMiter
    dashedBorder.lineWidth = 1.0
    dashedBorder.miterLimit = 10.0
    dashedBorder.path = borderPath.cgPath
    let animation = CABasicAnimation(keyPath: "lineDashPhase")
    animation.fromValue = 0.0
    animation.toValue = 15.0
    animation.duration = 0.75
    animation.repeatCount = 10000
    dashedBorder.add(animation, forKey: "linePhase")
    self.addSublayer(dashedBorder)
    _editMode = true
}

func removeDashedBorder() {
    dashedBorder.removeFromSuperlayer()
    _editMode = false
}

这可能不符合您的确切需求,但 tap&amp; amp; pan over 点击进行编辑似乎更自然。我敢肯定,至少做一个“沉默”的水龙头&amp;平移(在用户看来,他们只是拖动一些东西,但他们仍然必须首先点击它)是一个更好的选择。

答案 1 :(得分:0)

touchesBegan方法中的

:检查触摸是否触及UIImageView并将全局布尔变量设置为true。在touchesEnded上将其设置为false,如果在移动UIImageview之前变量为true,则检入touchesMoved

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

let touch : UITouch = touches.first as UITouch!

location = touch.locationInView(self.view)
if(person.frame.origin.x<location.x&&person.frame.origin.x+person.frame.size.width>location.x{
    // x value for touch is inside uiimageview
if(person.frame.origin.y<location.y&&person.frame.origin.y+person.frame.size.height>location.y{
    //x&y value inside 
    personHit=true
    }
}

   // Person.center = location

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if(personHit){
let touch : UITouch = touches.first as UITouch!

location = touch.locationInView(self.view)

Person.center = location
}

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    personHit=false
}

答案 2 :(得分:0)

您可以使用touchesMoved事件执行此操作。但我的建议是使用UIPanGestureRecognizer

  1. 声明UIPanGestureRecognizer对象并将其添加到图片视图中。
  2. 启用图像视图的用户交互。
  3. 按照这个漂亮的苹果记录的示例代码https://developer.apple.com/library/content/samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435

答案 3 :(得分:0)

解决问题的简单方法:

__str__ is supposed to return Bag(a[1], c[1], b[2], d[3])