Subclass UIScrollView in Swift for touches Began & touches Moved -
i'm using swift 1.2
i'm trying use uiscrollview touchesbegan,touchesmoved,touchesended actions , link in 2nd comment of accepted answer.
i'm using storyboard auto layout, set custom class uiscrollview in custom class.
i'm not receiving either of these touch events in uiviewcontroller contains custom uiscrollview
edit: updated code how use @victor sigler 's answer.
custom scrollview code:
import uikit protocol passtouchesscrollviewdelegate { func scrolltouchbegan(touches: set<nsobject>, withevent event: uievent) func scrolltouchmoved(touches: set<nsobject>, withevent event: uievent) } class passtouchesscrollview: uiscrollview { var delegatepass : passtouchesscrollviewdelegate? override init(frame: cgrect) { super.init(frame: frame) } required init(coder adecoder: nscoder) { super.init(coder: adecoder) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { self.delegatepass?.scrolltouchbegan(touches, withevent: event) } override func touchesmoved(touches: set<nsobject>, withevent event: uievent) { self.delegatepass?.scrolltouchmoved(touches, withevent: event) } }
my viewcontroller:
import uikit class viewcontroller: uiviewcontroller, passtouchesscrollviewdelegate { @iboutlet weak var scrollview: uiscrollview! override func viewdidload() { super.viewdidload() scrollview.delegatepass = self } func scrolltouchbegan(touches: set<nsobject>, withevent event: uievent) { println("began \(touches)") } func scrolltouchmoved(touches: set<nsobject>, withevent event: uievent) { println("moved \(touches)") } }
i'm trying let user draw line on uiimage, got working using pangesture recognizer performance poor, on older hardware, followed ray wenderlich tutorial used touches began , performance better, on uiview , not scrollview. need uiscrollview since prior user drawing on image can zoom in , around it.
i thought you're thinking wrong. if want know when uiscrollview
moving, there's no need subclass it. ios has set methods need inside of uiscrollviewdelegate
.
you must implement uiscrollviewdelegate
notified actions in uiscrollview
, set delegate
interface builder or in code , you.
see following example know how example :
class viewcontroller: uiviewcontroller, uiscrollviewdelegate { @iboutlet weak var scrollview: uiscrollview! override func viewdidload() { super.viewdidload() self.scrollview.delegate = self } }
still if want know how touched in way explained above must follow following steps :
you must subclass
uiscrollview
class did in classpasstouchesscrollview
, implement delegate pattern notifieduiscrollview
in following way:import uikit protocol passtouchesscrollviewdelegate { func touchbegan() func touchmoved() } class passtouchesscrollview: uiscrollview { var delegatepass : passtouchesscrollviewdelegate? override init(frame: cgrect) { super.init(frame: frame) } required init(coder adecoder: nscoder) { super.init(coder: adecoder) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { // notify it's delegate touched self.delegatepass?.touchbegan() if self.dragging == true { self.nextresponder()?.touchesbegan(touches, withevent: event) } else { super.touchesbegan(touches, withevent: event) } } override func touchesmoved(touches: set<nsobject>, withevent event: uievent) { // notify it's delegate touched self.delegatepass?.touchmoved() if self.dragging == true { self.nextresponder()?.touchesmoved(touches, withevent: event) } else { super.touchesmoved(touches, withevent: event) } } }
your class
viewcontroller
must implemented in following way:class viewcontroller: uiviewcontroller, uiscrollviewdelegate { @iboutlet weak var scrollview: passtouchesscrollview! override func viewdidload() { super.viewdidload() scrollview.delegate = self scrollview.delegatepass = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func touchmoved() { println("touch moved") } func touchbegan() { println("touch began") } }
you must in interface builder select
uiscrollview
, set class classpasstouchesscrollview
, in identity inspector in class part.
and should see in console following:
touch began touch began touch began touch began touch began touch began touch moved touch move
i hope you.
Comments
Post a Comment