1
UIView
はターゲット、ドラッグ可能オブジェクトはUIImage
です。仕事はUIView
の中にUIImage
を置くことです。このコードでターゲットオブジェクトの中心にドラッグ可能オブジェクトを配置する方法
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first, let target = dropTarget {
let position = touch.location(in: self.superview)
let isOnTarget = target.frame.contains(position)
if isOnTarget {
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue:"onTarget")))
// Make it stay there
let targetHeight = target.frame.size.height
let targetWidth = target.frame.size.width
self.center = CGPoint(x: targetHeight, y: targetWidth)
} else {
self.center = originalPosition
}
}
}
問題はUIImage
がUIView
外で、左下隅に取り付けて、それはそれの中心であるべきであるということである。
は、ここに私のコードです。
私はそれがそれに触れている場合、それはターゲットの真ん中にとどまることができますか?
のUIViewとUIImageViewは、共通のスーパーを持っている場合:self.center = target.center –
これはそれです!ありがとう! – Kira