ios - Passing an existing and active gesture recognizer to a modal view controller -
i want launch modal view long press, , dismiss modal view when long press cancelled. how do this?
- i tried passing longpressrecognizer modal view , setting delegate, didn't work.
- i tried simpler- detecting touchesended, mean in modal view touch ended, doesn't fire either.
is there way tell modal view gesture has started; want recognize end/cancellation of gesture or touch?
viewcontroller.swift
import uikit class viewcontroller: uiviewcontroller, uigesturerecognizerdelegate { var buttonview:uiview! var longpressrecognizer:uilongpressgesturerecognizer! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. self.longpressrecognizer = uilongpressgesturerecognizer(target: self, action: "longpressed:") self.longpressrecognizer.delegate = self self.view.addgesturerecognizer(self.longpressrecognizer) // add button buttonview = uiview(frame: cgrect(x: 0, y: 0, width: self.view.frame.width, height: 100)) buttonview.userinteractionenabled = true; buttonview.backgroundcolor = uicolor.graycolor() self.view.addsubview(buttonview) } func longpressed(recognizer: uilongpressgesturerecognizer) { let point: cgpoint = recognizer.locationinview(self.view) if let pressedview = self.view.hittest(point, withevent: nil) { if pressedview == self.buttonview { switch recognizer.state { case .began: nslog("long pressed - began") var mediaviewcontroller = mediaviewcontroller() self.presentviewcontroller(mediaviewcontroller, animated: false, completion: nil) case .cancelled: nslog("long pressed - cancelled") case .ended: nslog("long pressed - ended") default: break } } } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
mediaviewcontroller.swift
import uikit class mediaviewcontroller: uiviewcontroller, uigesturerecognizerdelegate { var longpressrecognizer:uilongpressgesturerecognizer! override func viewdidload() { super.viewdidload() self.view.backgroundcolor = uicolor.lightgraycolor() self.longpressrecognizer = uilongpressgesturerecognizer(target: self, action: "longpressed:") self.longpressrecognizer.delegate = self self.view.addgesturerecognizer(self.longpressrecognizer) } func longpressed(recognizer: uilongpressgesturerecognizer) { switch recognizer.state { case .began: nslog("long pressed - began") case .cancelled: nslog("long pressed - cancelled") case .ended: nslog("long pressed - ended") self.dismissviewcontrolleranimated(false, completion: nil) default: break } } override func touchesbegan(touches: nsset, withevent event: uievent) { nslog("touches began") } override func touchesended(touches: nsset, withevent event: uievent) { nslog("touches ended") } }
hmmm, appears work , handle tap gesture simultaneously long press. not bad!
viewcontroller.swift
import uikit class viewcontroller: uiviewcontroller, uigesturerecognizerdelegate { var buttonview:uiview! var longpressrecognizer:uilongpressgesturerecognizer? override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. // add button buttonview = uiview(frame: cgrect(x: 0, y: 0, width: self.view.frame.width, height: 100)) buttonview.userinteractionenabled = true; buttonview.backgroundcolor = uicolor.graycolor() self.view.addsubview(buttonview) } override func viewdidappear(animated: bool) { if let recognizer = self.longpressrecognizer { // reuse existing recognizer self.longpressrecognizer = recognizer recognizer.removetarget(nil, action: nil) recognizer.addtarget(self, action: "longpressed:") recognizer.delegate = self self.view.addgesturerecognizer(recognizer) } else { // create new new recognizer self.longpressrecognizer = uilongpressgesturerecognizer(target: self, action: "longpressed:") self.longpressrecognizer!.delegate = self self.view.addgesturerecognizer(self.longpressrecognizer!) } } override func viewdiddisappear(animated: bool) { // remove recognizer if let recognizer = self.longpressrecognizer { self.view.removegesturerecognizer(recognizer) } } func longpressed(recognizer: uilongpressgesturerecognizer) { let point: cgpoint = recognizer.locationinview(self.view) if let pressedview = self.view.hittest(point, withevent: nil) { if pressedview == self.buttonview { switch recognizer.state { case .began: nslog("viewcontroller: long pressed - began") var mediaviewcontroller = mediaviewcontroller() mediaviewcontroller.addrecognizer(recognizer) self.presentviewcontroller(mediaviewcontroller, animated: false, completion: nil) case .cancelled: nslog("viewcontroller: long pressed - cancelled") case .ended: nslog("viewcontroller: long pressed - ended") case .changed: nslog("viewcontroller: long pressed - changed") default: break } } } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
mediaviewcontroller.swift
class mediaviewcontroller: uiviewcontroller, uigesturerecognizerdelegate { var longpressrecognizer: uilongpressgesturerecognizer! var taprecognizer: uitapgesturerecognizer! override func viewdidload() { super.viewdidload() self.view.backgroundcolor = uicolor.lightgraycolor() self.taprecognizer = uitapgesturerecognizer(target: self, action: "tap:") self.taprecognizer.cancelstouchesinview = false self.taprecognizer.delegate = self self.view.addgesturerecognizer(self.taprecognizer) self.longpressrecognizer.removetarget(nil, action: nil) self.longpressrecognizer.addtarget(self, action: "longpressed:") self.longpressrecognizer.delegate = self self.view.addgesturerecognizer(self.longpressrecognizer) } override func viewwilldisappear(animated: bool) { // remove gesture recognizers self.view.removegesturerecognizer(self.longpressrecognizer) self.longpressrecognizer.delegate = nil self.view.removegesturerecognizer(self.taprecognizer) self.taprecognizer.delegate = nil } func gesturerecognizer(gesturerecognizer: uigesturerecognizer, shouldrecognizesimultaneouslywithgesturerecognizer othergesturerecognizer: uigesturerecognizer) -> bool { return true; } func longpressed(recognizer: uilongpressgesturerecognizer) { switch recognizer.state { case .began: nslog("mediaviewcontroller: long pressed - began") case .cancelled: nslog("mediaviewcontroller: long pressed - cancelled") case .ended: nslog("mediaviewcontroller: long pressed - ended") self.dismissviewcontrolleranimated(false, completion: nil) case .changed: nslog("mediaviewcontroller: long pressed - changed") default: break } } func tap(recongizer: uitapgesturerecognizer) { if (self.view.backgroundcolor == uicolor.lightgraycolor()) { self.view.backgroundcolor = uicolor.greencolor() } else { self.view.backgroundcolor = uicolor.lightgraycolor() } } func addrecognizer(recognizer: uilongpressgesturerecognizer) { self.longpressrecognizer = recognizer } }