Pages

Handling touch events with Swift

Well guys as I explained about how the events are handled in my post "Responder chain", here today we will be working on how to handle touch events. There are basically four touch functions that works on handling the touch events in any application. Here are these functions:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
override func touchesEnded(touches: NSSet, withEvent event: UIEvent)
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!)

Let me explain these functions a bit. The first function touchesBegan() is self explanatory will get called when user touch the screen.You can perform any action in it. Second function touchesMoved() will be called when user will drag finger on the screen and as usual corresponding action will be performed.The third function touchesEnded() in one which will be called when the touch will be ended means user will remove her finger form the screen. The last function touchesCancelled() is called whenever any event happen to interrupt the touch.One important things is to note about these touches events is that these are determined by Hit Testing.


Let us understand these events with an example: In this example we will draw a label programmatically && the change the text of this label according to these touch events. So go ahead && create a new xCode project with usual setting && then in viewDidLoad() method of you ViewController.Swift file write the below code:



    override func viewDidLoad() {
        super.viewDidLoad()
        var label = UILabel()
        label.frame = CGRectMake(20, 50, 330,30)
        label.backgroundColor = UIColor.greenColor()
        label.textColor = UIColor.whiteColor()
        label.textAlignment = NSTextAlignment.Left
        view.addSubview(label)
    }

Here in this code all we are doing is just drawing a label when the view loads of your app's first run.
Now in touchesBegan() method write the following code:


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {        
        var touch = touches.anyObject() as UITouch
        let location = touch.locationInView(view)
        label.text = "Touch Began"

    }

Here in this function we are calculating the location wherever the user touch her finger on the screen. And then setting the text of the label whenever this function get called, according to user touch label's text will get changed.


    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {        
        label.text = "Touch Moved"
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {        
        label.text = "Touch Ended"
    }

Here in these two functions again all we are doing is just setting the text of the label. Whenever user drag on the screen label text will be changed && whenever user lift her finger off the screen the third function get called && changes the title of the label.

No comments:

Post a Comment