How can I programmatically determine if a touchUpInside event has occurred?

It seems like if this method gets called anytime the user touches my view:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == self && [touch tapCount] >= 1) {
        // do something...
    }
}

      

I have implemented this in UIScrollView. The documentation for UIEvent does not mention those touchUpInside, touchUpOutside, etc. events that are available in Interface Builder when connected to action methods.

Actually, I want the method to be called on only one of these touchUpInside events, not on any touch.

+1


a source to share


4 answers


You want touchesEnded:withEvent:

. The method touchesBegan

is called when a touch or series of touches begins in the UIResponder; the method touchesEnded

is called when this batch of strokes is executed (i.e. your user stops touching the screen / lifts their finger).



+3


a source


The canonical way is to create an IBAction and hook it up to the touchUpInside event in InterfaceBuilder.app.

As long as touchUpInside fires, IBAction is called.



If you build programmatically outside of IB, you can still hook up IBAction.

+1


a source


This is how I bind the IBAction to the textbox:

[yourTextField addTarget:self action:@selector(your method:) forControlEvents:UIControlEventTouchUpInside];

      

+1


a source


Wadoff's technique is neat, however remember to clear the sensory event by triggering the super function. Here is the complete code.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    BOOL touchedUpInside = NO;
    if (self.isTouchUpInside) {
        touchedUpInside = YES;
    }

    [super touchesEnded:touches withEvent:event];    

    if (touchedUpInside) {
        // touched up inside
    } 
}

      

0


a source







All Articles