iOSはジェスチャーパスワード機能を実現します。


ジェスチャーパスワードの実装
ジェスチャーのパスワードは普通はよく金融項目に使われます。安全関連の業務をしています。具体的に次のような考え方を実現して、私はそれをview層と論理層に分けます。データ層をview層に統合しました。暗号化されたパスワードとパスワードを処理するためにデータ層を追加したほうがいいです。
ビュー層
view層は主に(9つのボタン)touchesBegan、touches Moved、touches Eded、点と点の間に線を描き、指をスライドさせて線を描き、線を描くのは主にdrawRectで描き直すので、ここに言及するとsetNeedsDisplayという方法を忘れてはいけない。後のボタンbtnsArayも記録します。これは関連の具体的な値とマッピングしてもいいです。btnのタグを直接設定してもいいです。また、絵の回調を追加してもいいです。論理層に処理を提供する。
論理層
相互作用を完了した後の業務を処理するために、(要求インターフェース、異常論理表示など)
具体的なデモポイントここです
具体的なコード:
view.h

//
// YHGesturePasswordView.h
//     
//
// Created by mrlee on 2017/3/5.
// Copyright © 2017  mrlee. All rights reserved.
//
typedef enum {
  GestureSetPassword, //      
  GestureResultPassword //        
} PasswordState;
//     3   
typedef enum {
  FristPwd, //       
  PwdNoValue, //         
  SetPwdSuccess, //      
  Other
}SetPwdState;

#import <UIKit/UIKit.h>

@interface YHGesturePasswordView : UIView
/** btn  */
@property (nonatomic,strong)UIImage *btnImage;

///     
@property (nonatomic,strong)UIImage *btnSelectImage;

///    
@property (nonatomic,strong)UIColor *lineColor;

/**                 block */
@property (nonatomic,copy)BOOL (^sendReaultData)(NSString *str);

//      
@property(nonatomic,copy)void(^setPwdBlock)(SetPwdState pwdState);


// init
-(instancetype)initWithFrame:(CGRect)frame WithState:(PasswordState)state;

@end

view.m

//
// YHGesturePasswordView.m
//     
//
// Created by mrlee on 2017/3/5.
// Copyright © 2017  mrlee. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#import "YHCustomButton.h"
#import "YHGesturePasswordView.h"
#import <CommonCrypto/CommonDigest.h>
@interface YHGesturePasswordView(){
  /**          ,       */
  PasswordState Amode;
}
/**        */
@property (nonatomic,strong)NSMutableArray * allBtnsArray;

/**            btn  */
@property (nonatomic,strong)NSMutableArray * btnsArray;

/**          */
@property (nonatomic,assign)CGPoint currentPoint;

@end

@implementation YHGesturePasswordView

-(instancetype)initWithFrame:(CGRect)frame WithState:(PasswordState)state{
  self = [super initWithFrame:frame];
  if (self) {
     self.backgroundColor = [UIColor clearColor];
    Amode = state;
    for (int i = 0; i<9; i++) {
      YHCustomButton *btn = [[YHCustomButton alloc]init];
      [btn setTag:i];
      btn.userInteractionEnabled = NO;
      if (self.lineColor == nil) {
        self.lineColor = [UIColor greenColor];
      }
      [self addSubview:btn];
    }

  }
  return self;
}
-(void)drawRect:(CGRect)rect{
  //                   default     ,              ,          
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextClearRect(ctx, rect);//     
  for (int i = 0; i<self.btnsArray.count; i++) {
    UIButton *btn = self.btnsArray[i];
    if (i == 0) {
      CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);
    }else{
      CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
    }
  }
  if (!CGPointEqualToPoint(self.currentPoint, CGPointZero)) {//      CGPointZero      
    CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y);
  }

  CGContextSetLineWidth(ctx, 12);
  CGContextSetLineCap(ctx, kCGLineCapRound);
  CGContextSetLineJoin(ctx, kCGLineJoinRound);
  [self.lineColor set];
  CGContextStrokePath(ctx);

}
-(void)layoutSubviews{

   [self.allBtnsArray removeAllObjects];
  for (int index =0; index<self.subviews.count; index ++) {
    if ([self.subviews[index] isKindOfClass:[YHCustomButton class]]) {

      [self.allBtnsArray addObject:self.subviews[index]];
    }
  }
  // button      
  [self drawUi];


}
#pragma mark Private method
-(void)drawUi{
  for (int index = 0; index<self.allBtnsArray.count; index ++) {
    //    btn
    UIButton *btn = self.subviews[index];

    //  frame
    CGFloat btnW = 74;
    CGFloat btnH = 74;
    CGFloat margin = (SCREEN_WIDTH - (btnW *3))/4;
    //x =    +   *(  +btnW)
    CGFloat btnX = margin + (index % 3)*(margin + btnW);
    CGFloat btnY = margin + (index / 3)*(margin + btnH);

    btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
  }

}
//    
-(SetPwdState)pwdValue:(NSString *)str{
  if ([[NSUserDefaults standardUserDefaults] objectForKey:@"pwdValue"] == nil) {
    //     
    [[NSUserDefaults standardUserDefaults] setValue:str forKey:@"pwdValue"];
    return FristPwd;
  }
  if ([str isEqualToString: [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdValue"]]) {
    //    
    return SetPwdSuccess;
  }
  if (![str isEqualToString: [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdValue"]]) {
    //       
    return PwdNoValue;
  }

  return Other;

}
//  
-(void)clear{
  [self.btnsArray removeAllObjects];
  self.currentPoint = CGPointZero;
  [self setNeedsDisplay];
  self.lineColor = [UIColor greenColor];
  self.userInteractionEnabled = YES;
}
//      
-(CGPoint)getCurrentTouch:(NSSet<UITouch*> *)touches{
  UITouch *touch = [touches anyObject];
  CGPoint point = [touch locationInView:touch.view];
  return point;
}

-(UIButton *)getCurrentBtnWithPoint:(CGPoint) currentPoint{
  for (UIButton *btn in self.subviews) {
    if (CGRectContainsPoint(btn.frame, currentPoint)) {
      return btn;
    }
  }
  return nil;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  CGPoint point = [self getCurrentTouch:touches];
  UIButton *btn = [self getCurrentBtnWithPoint:point];
  if (btn && btn.selected != YES) {
    btn.selected = YES;
    [self.btnsArray addObject:btn];
    NSLog(@" array is value %@",self.btnsArray);
  }
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  CGPoint movePoint = [self getCurrentTouch:touches];
  UIButton *btn = [self getCurrentBtnWithPoint:movePoint];
  if (btn && btn.selected !=YES) {
    btn.selected = YES;
    [self.btnsArray addObject:btn];
    NSLog(@"btn is value %@",self.btnsArray);
  }
  self.currentPoint = movePoint;
  [self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  for (UIButton *btn in self.btnsArray) {
    [btn setSelected:NO];
  }
  NSMutableString *result = [NSMutableString string];
  for (UIButton *btn in self.btnsArray) {
    [result appendString: [NSString stringWithFormat:@"%ld",(long)btn.tag]];
  }
  switch (Amode) {
    case GestureSetPassword:{
      //      
      self.setPwdBlock([self pwdValue:result]);
    }
      break;
    case GestureResultPassword :{
      //        
      if (self.sendReaultData) {
        if (self.sendReaultData(result) == YES) {
           NSLog(@"success");
          [self clear];
        }else{
          NSLog(@"    ");
        }

      }

    }
      break;

    default:
      break;
  }
  //    
  [self clear];
}
#pragma mark     
-(NSMutableArray *)btnsArray{
  if (_btnsArray == nil) {
    _btnsArray = [NSMutableArray array];
  }
  return _btnsArray;
}
-(NSMutableArray *)allBtnsArray{
  if (_allBtnsArray == nil) {
    _allBtnsArray = [NSMutableArray array];
  }
  return _allBtnsArray;
}

@end
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。