CodingMeSwiftly
CodingMeSwiftly

Reputation: 3261

Using touchesBegan & touchesEnded as Swipe

I'm trying to see if a user swiped all the way from one view, label, etc. to another.

Here is my relevant Code

ViewController.h :

@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UILabel *swipeBegin;
@property (strong, nonatomic) IBOutlet UILabel *swipeEnd;

TextField : To display if the whole thing was successful.
SwipeBegin : The Label from which the swipe begins.
SwipeEnd : The Label where the swipe is supposed to end.

ViewController.m :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == swipeBegin) {
    gestureStartPointTouched = YES;
}

If the swipe began at "swipeBegin" a bool value is set to YES.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if ([touch view] == swipeEnd) {        
    if (gestureStartPointTouched == YES) {
        [TextField setText:@"Success"];
    }
}

If I start swiping at "swipeBegin" the touchesBegan method is called -> works fine

The troubling part is the touchesEnded method.

if([touch view] == swipeEnd)

is false no matter what i do about it.

Does anybody have an advice ?

Thank You !


Solution


Editing the touchesEnded method :

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if ([self.view hitTest:[[touches anyObject] locationInView:self.view] withEvent:event] == SwipeEnd) {
    if (gestureStartPointTouched == YES) {
        [TextField setText:@"Success"];
    }
}

Upvotes: 0

Views: 824

Answers (1)

Alejandro
Alejandro

Reputation: 3746

The view of the touch never changes, if it starts in swipeBegin that will always be it's view. If you want to find out where it ended use hitTest:withEvent: with the touch's end location, that will return the view you are looking for.

Upvotes: 0

Related Questions