Showing posts with label gesture. Show all posts
Showing posts with label gesture. Show all posts

Sunday, January 24, 2016

Objective C long press event

Sometimes, we need long press event, especially in games rather than just a tap or touch event.

For example, I need to change the template in an application on tapping the template icon and this change should be only a local change. If I need to change the template and also need to save the template ID in the server, Obviously I need another event on the template icon. 

One way is to write double tap event on that control, Which is not very handy. Another best approach is a long press event which users also feel very handy. Single tap local change and long press gives local as well as server change. In the usability point of view also this is a very good approach.


                         



Let's see how to write a long press event on a control (UIButton, UILabel..) in Objective-C.

Like UITapGestureRecognizer, we have UILongPressGestureRecognizer in Objective C. Let's see how to apply this to a control.

I have a label and I need to apply long press on it.


   
UILabel *lbl = [UILabel alloc] init];
   [lbl seTextColor:[UIColor whiteColor]];


As it is a static control, by default it's user interaction is disabled. We need to explicitly enable it. Otherwise, these labels won't respond to these recognizers.


  
[lbl setUserInteractionEnabled:YES];

  UILongPressGestureRecognizer *longPress =       
                           [[UILongPressGestureRecognizer allocinitWithTarget:self 
                                                                 action:@selector(longPressEvent:)];

  [longPress setMinimumPressDuration:3];

  longPress.delegate = (id <UIGestureRecognizerDelegate>)self;

  [lbl addGestureRecognizer:longPress];


I have added a long press gesture recognizer to the label. We can also set the minimum duration that user has to tap and hold on the control for this event. Unlike, tap events, these long press recognizers have states. Below are the UIGestureRecognizerState types.

    UIGestureRecognizerStatePossible,     
    UIGestureRecognizerStateBegan,    
    UIGestureRecognizerStateChanged,   
    UIGestureRecognizerStateEnded,     
    UIGestureRecognizerStateCancelled,      
    UIGestureRecognizerStateFailed,   
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded

Here, we are concerned about only 2 states. 


 
- (void) longPressEvent:(UILongPressGestureRecognizer*)gesture{
    
    if (gesture.state == UIGestureRecognizerStateBegan ) {        
        
        [self.titleLabel setTextColor:[UIColor redColor]];
    }
    
    else if ( gesture.state == UIGestureRecognizerStateEnded) {

        self.titleLabel.textColor = [UIColor whiteColor]
    }
    
 }



First one is UIGestureRecognizerStateBegan. This where you can confirm like long press is happening on the control. Here I am just changing the color of the label text control to highlight it.

Second one is, UIGestureRecognizerStateEnded. This is where you can confirm like user has stopped the long press on the control. Here I am rolling back the label's actual text color.




                         


Hope this post is useful. Feel free to comment incase of any queries.

Tuesday, January 12, 2016

Objective C touch events on UILabel and UIImageView and UIView

We all know that for touch events we have so many interactive UI controls like UIButton that UIKit provides. But what If we want to add interactions on controls like UILabel, UIView etc... 

These are static controls. That means these are given in the UIKit just only for the viewing purpose. Like UILabel to display static text and UIImagesView to display static images.


Let's see how we can add touch actions on these static things which doesn't have any touch events in built.



                         


Here comes gesture recognizers into picture. We can use UITapGestureRecognizer to add tap events on these static controls.

Let's say we have a label called myLabel. I want something to be happened when user taps on it.

UILabel *myLabel = [UILabel alloc] init];

Here I am initializing a UITapGestureRecognizer and adding it to my label.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapped:)];
[myLabel addGestureRecognizer:tapGesture];

Now, whenever user taps on this label a method named labelTapped will get called.

After checking this, Please don't come back here and scold me :)

We are not yet done with touch action on this label. We forgot the main thing here to complete this tap action. As I have said earlier, these are static controls and by default the user interaction for these controls is disabled. We need to explicitly enable user interaction for these static controls to make tap action work.

[myLabel setUserInteractionEnabled:YES];

We are done now. Add this interaction enabling. Your label starts taking user interactions.

Same piece of code works for UIImageView and UIView.


Hope this post is helpful. Feel free to comment in case of any queries.