Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DragImages: UIImageView {
- var originalPos : CGPoint!
- var dropTarget: UIView?
- //var endPos : CGPoint!
- // var endPosX : CGFloat
- // var endPosY : CGFloat
- override init (frame : CGRect){
- super.init(frame: frame)
- }
- required init?(coder aDecoder : NSCoder){
- super.init(coder : aDecoder)
- }
- override func touchesBegan(_ touches : Set<UITouch>,with event: UIEvent?){
- originalPos = self.center
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- if let touch = touches.first{
- let position = touch.location(in: self.superview)
- self.center = CGPoint(x : position.x, y : position.y)
- }
- }
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
- if let touch = touches.first, let target = dropTarget{
- let position = touch.location(in: self.superview)
- if target.frame.contains(position){
- NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "onTargetDropped"), object: nil))
- }else {
- self.center = originalPos
- }
- }
- print(self.center.x, self.center.y)
- self.center = originalPos
- }
- func getEndPosX() -> CGFloat{
- return self.center.x
- }
- func getEndPosY() -> CGFloat {
- return self.center.y
- }
- }
- ornament1.dropTarget = xmasTree
- ornament2.dropTarget = xmasTree
- ornament3.dropTarget = xmasTree
- ornament4.dropTarget = xmasTree
- NotificationCenter.default.addObserver(self, selector: #selector(ViewController.itemDroppedOnTree(_:)), name: NSNotification.Name(rawValue: "onTargetDropped"), object: nil)
- }
- func itemDroppedOnTree(_ notif : AnyObject){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement