最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實現(xiàn)手勢密碼功能

 更新時間:2017年03月10日 14:07:45   作者:YunHe_Lee  
這篇文章主要為大家詳細介紹了iOS實現(xiàn)手勢密碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

手勢密碼實現(xiàn)

手勢密碼 一般常常用于金融項目,做的是安全相關的業(yè)務。具體實現(xiàn)如下思路,我把它分為view層和邏輯層。我將數據層合并到view層中了,最好是加上數據層用于處理加密的密碼和密碼的存儲

view層

view層主要處理,包括(九個按鈕)touchesBegan,touchesMoved,touchesEnded,點與點之間畫線,手指滑動畫線,畫線主要是在drawRect中重繪,提到這里必須不能忘記setNeedsDisplay這個方法。還要記錄經過的按鈕btnsArray(存放按鈕的數組),這個 可以和相關的具體值做映射,也可以直接設置btn 的tag,還要添加完成繪畫的回調。提供給邏輯層去處理。

邏輯層

用于處理完成交互后的業(yè)務,包括(請求接口,異常邏輯顯示,等等)
具體的demo點這里

具體的code:
view.h

//
// YHGesturePasswordView.h
// 手勢密碼
//
// Created by mrlee on 2017/3/5.
// Copyright © 2017年 mrlee. All rights reserved.
//
typedef enum {
  GestureSetPassword, //設置手勢密碼
  GestureResultPassword //已有手勢密碼教驗
} PasswordState;
//設置密碼的3種狀態(tài)
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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iOS界面布局簡化UIStackView使用詳解

    iOS界面布局簡化UIStackView使用詳解

    這篇文章主要為大家介紹了iOS界面布局簡化UIStackView使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • iOS開發(fā)網絡篇—實現(xiàn)大文件的多線程斷點下載

    iOS開發(fā)網絡篇—實現(xiàn)大文件的多線程斷點下載

    iOS開發(fā)中經常會用到文件的下載功能,這篇文章主要介紹了iOS開發(fā)網絡篇—實現(xiàn)大文件的多線程斷點下載,今天咱們來分享一下思路。
    2016-11-11
  • IOS開發(fā)中禁止NavigationController的向右滑動返回

    IOS開發(fā)中禁止NavigationController的向右滑動返回

    這篇文章主要介紹了IOS開發(fā)中禁止NavigationController的向右滑動返回的相關資料,需要的朋友可以參考下
    2017-03-03
  • iOS獲取手機通訊錄方式方法(最新)

    iOS獲取手機通訊錄方式方法(最新)

    本篇文章主要介紹了iOS獲取手機通訊錄方式方法(最新),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • iOS13適配三指撤銷和文案限長實例詳解

    iOS13適配三指撤銷和文案限長實例詳解

    這篇文章主要為大家介紹了iOS13適配三指撤銷和文案限長實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • iOS UICollectionView實現(xiàn)橫向滑動

    iOS UICollectionView實現(xiàn)橫向滑動

    這篇文章主要為大家詳細介紹了iOS UICollectionView實現(xiàn)橫向滑動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • iOS開發(fā)總結之UILabel常用屬性介紹

    iOS開發(fā)總結之UILabel常用屬性介紹

    下面小編就為大家分享一篇iOS開發(fā)總結之UILabel常用屬性介紹,具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • IOS 獲取已連接的wifi信息的實現(xiàn)代碼

    IOS 獲取已連接的wifi信息的實現(xiàn)代碼

    這篇文章主要介紹了IOS 獲取已連接的wifi信息的實現(xiàn)代碼的相關資料,這里提供實現(xiàn)代碼幫助大家學習理解這部分內容,需要的朋友可以參考下
    2017-08-08
  • IOS Cache設計詳細介紹及簡單示例

    IOS Cache設計詳細介紹及簡單示例

    這篇文章主要介紹了IOS Cache設計詳細介紹及簡單示例的相關資料,Cache的目的是為了追求更高的速度體驗,Cache的源頭是兩種數據讀取方式在成本和性能上的差異,需要的朋友可以參考下
    2017-01-01
  • iOS13即將到來,iOS推送DeviceToken適配方案詳解

    iOS13即將到來,iOS推送DeviceToken適配方案詳解

    這篇文章主要介紹了iOS13即將到來,iOS推送DeviceToken適配方案詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09

最新評論

罗田县| 东乌| 钦州市| 舞钢市| 沿河| 会东县| 盈江县| 金堂县| 岳池县| 乳山市| 樟树市| 河北区| 台山市| 宜阳县| 藁城市| 盐城市| 海淀区| 建昌县| 济源市| 宁波市| 四川省| 读书| 伊春市| 正安县| 延川县| 新宁县| 盈江县| 富顺县| 陆川县| 会泽县| 柳江县| 马边| 永年县| 且末县| 甘肃省| 克拉玛依市| 太湖县| 依安县| 疏勒县| 青州市| 新平|