Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. class DragImages: UIImageView {
  2.  
  3. var originalPos : CGPoint!
  4. var dropTarget: UIView?
  5. //var endPos : CGPoint!
  6. // var endPosX : CGFloat
  7. // var endPosY : CGFloat
  8.  
  9. override init (frame : CGRect){
  10. super.init(frame: frame)
  11. }
  12.  
  13. required init?(coder aDecoder : NSCoder){
  14. super.init(coder : aDecoder)
  15. }
  16.  
  17. override func touchesBegan(_ touches : Set<UITouch>,with event: UIEvent?){
  18. originalPos = self.center
  19. }
  20.  
  21. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  22. if let touch = touches.first{
  23. let position = touch.location(in: self.superview)
  24. self.center = CGPoint(x : position.x, y : position.y)
  25. }
  26. }
  27.  
  28. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  29.  
  30. if let touch = touches.first, let target = dropTarget{
  31. let position = touch.location(in: self.superview)
  32. if target.frame.contains(position){
  33.  
  34. NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "onTargetDropped"), object: nil))
  35. }else {
  36. self.center = originalPos
  37. }
  38. }
  39.  
  40. print(self.center.x, self.center.y)
  41. self.center = originalPos
  42. }
  43.  
  44. func getEndPosX() -> CGFloat{
  45. return self.center.x
  46. }
  47.  
  48. func getEndPosY() -> CGFloat {
  49. return self.center.y
  50. }
  51.  
  52. }
  53.  
  54. ornament1.dropTarget = xmasTree
  55. ornament2.dropTarget = xmasTree
  56. ornament3.dropTarget = xmasTree
  57. ornament4.dropTarget = xmasTree
  58.  
  59. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.itemDroppedOnTree(_:)), name: NSNotification.Name(rawValue: "onTargetDropped"), object: nil)
  60.  
  61. }
  62.  
  63.  
  64. func itemDroppedOnTree(_ notif : AnyObject){
  65.  
  66.  
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement