更改UIImageView位置的问题

时间:2018-01-11 09:31:59

标签: ios swift uiimageview

我想在ios设备上移动UIImageView。我能够移动它但是偏移有问题,因为当我点击图像时我将ImageView中心设置为锚点移动点。 如何将触摸屏幕CGPOint设置为锚点以移动UIImageView?

override func touchesBegan( . . . ){
   location = touch.location(in : self.view)
   . . . 
   imageView.center = location
   . . .
}

override func touchesMoved( . . . ){
   location = touch.location(in : self.view)
   . . . 
   imageView.center = location
   . . .
}

1 个答案:

答案 0 :(得分:1)

试试这个:

var offset: CGPoint = CGPoint.zero

override func touchesBegan( . . . ){
    offset = touch.location(in: imageView)
    offset.x = (imageView.bounds.width / 2) - offset.x
    offset.y = (imageView.bounds.height / 2) - offset.y

    location = touch.location(in : self.view)
    location.x = location.x + offset.x
    location.y = location.y + offset.y
    . . .
    imageView.center = location
    . . .
}

override func touchesMoved( . . . ){
    location = touch.location(in : self.view)
    location.x = location.x + offset.x
    location.y = location.y + offset.y
    . . .
    imageView.center = location
    . . .
}