У меня есть один UIView
который является целью и одним UIImage
который является перетаскиваемым объектом. Задача состоит в том, чтобы поместить UIImage
внутрь UIView
, если это касается.Как разместить объект с перетаскиванием в центре целевого объекта
Вот мой код:
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 имеют общую SuperView: self.center = target.center –
Это он! Благодаря! – Kira