【问题标题】:Issue with changing UIImageView Position更改 UIImageView 位置的问题
【发布时间】:2018-01-11 09:31:58
【问题描述】:

我正在尝试在 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
   . . .
}

【问题讨论】:

  • 您必须在 UIImageView 上的 touchesBegan 期间进行命中测试,以检查触摸在 UIImageView 上的哪个点并记住该偏移量。然后在 touchesMoved 期间,将您的 imageView.center 更改为 touch 加上首次注册的偏移量。
  • 我尝试这样做,但无法找到可行的解决方案

标签: ios swift uiimageview


【解决方案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
    . . .
}

【讨论】:

    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2020-11-23
    • 2019-06-25
    相关资源
    最近更新 更多