Ranjana
Ranjana

Reputation: 43

Recognizing the Swiping pattern for screen lock n unlock ios

I want to create a swiping pattern for lock and unlock the screen(swiping without taking off the finger). How can I do that with UISwipeGestureRecognizer. I want to save it and match it when I again try to login. How can I save that? As an image or something else?. Please do help me with this. Thank you.

Upvotes: 0

Views: 1380

Answers (2)

ctietze
ctietze

Reputation: 2932

What do you mean when you talk about "pattern", and what exactly do you want to save?

You should use a UIPanGestureRecognizer because a swipe is just a quick flip gesture, wheras a pan is a controlled movement for which you can track the whole distance.

This is how I handle it, moving from right to left (opposite direction of the lock screen):

- (IBAction)slide:(UIPanGestureRecognizer *)gesture
{
    CGPoint translate = [gesture translationInView:gesture.view];
    translate.y = 0.0; // Just doing horizontal panning

    if (gesture.state == UIGestureRecognizerStateChanged)
    {
        // Sliding left only
        if (translate.x < 0.0)
        {
            self.slideLane.frame = [self frameForSliderLabelWithTranslate:translate];
        }
    }
    else if ([gesture stoppedPanning])
    {
        if (translate.x < 0.0 && translate.x < -(gesture.view.frame.size.width / 2.5))
        {
            // Panned enough distance to unlock
        }
        else
        {
            // Movement was too short, put it back again
            NSTimeInterval actualDuration = [self animationDuration:0.25 forWidth:gesture.view.frame.size.width withTranslation:(-translate.x)];

            [UIView animateWithDuration:actualDuration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                self.slideLane.frame = [self frameForSliderLabelWithTranslate:CGPointZero];
            } completion:^(BOOL finished) {

            }];
        }
    }
}

- (CGRect)frameForSliderLabelWithTranslate:(CGPoint)translate
{
    return CGRectMake(translate.x, self.slideLane.frame.origin.y, self.slideLane.bounds.size.width, self.slideLane.bounds.size.height);
}

Because I don't care why a gesture has stopped, I added this category to make the else-if-clause more readable. It's optional, though:

@implementation UIPanGestureRecognizer (Stopped)
- (BOOL)stoppedPanning
{
    return (   self.state == UIGestureRecognizerStateCancelled
            || self.state == UIGestureRecognizerStateEnded
            || self.state == UIGestureRecognizerStateFailed);
}
@end

Ideally, you'd take into account the velocity of the movement, because a quick flick into the right direction could suffice. In my case, I wanted the user to move the block, no matter how fast.

Upvotes: 0

Related Questions