Recognizing the Swiping pattern for screen lock n unlock ios

▼魔方 西西 提交于 2019-12-25 02:47:01

问题


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.


回答1:


Android Pattern Lock on iPhone for iOS

A Pattern Lock for iOS similar to the one in Android




回答2:


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.



来源:https://stackoverflow.com/questions/23894054/recognizing-the-swiping-pattern-for-screen-lock-n-unlock-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!