2016-11-07 7 views
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 
      } 
     } 
    } 

問題はUIImageUIView外で、左下隅に取り付けて、それはそれの中心であるべきであるということである。

は、ここに私のコードです。

私はそれがそれに触れている場合、それはターゲットの真ん中にとどまることができますか?

+1

のUIViewとUIImageViewは、共通のスーパーを持っている場合:self.center = target.center –

+0

これはそれです!ありがとう! – Kira

答えて

0

試してみてください。

self.center = CGPoint(x: targetHeight/2, y: targetWidth/2) 
+0

私は最初の試みでこれを試みましたが、これは動作しません。イメージがUIViewの外に出てしまいました。 – Kira

関連する問題