手势密码-iOS

手势密码实现效果:


手势密码效果

#import <UIKit/UIKit.h>

/**
 手势密码视图
 */
@class RSHandlePWDView;


@protocol RSHandlePWDViewDelegate<NSObject>

@optional

/**
 手势完成回调

 @param handleView 手势视图对象
 @param complateResult 手势密码
 */
- (void)handlePWDView:(RSHandlePWDView*)handleView inputComplate:(NSArray*)complateResult;
@end

@interface RSHandlePWDView : UIView



/**
 大圆默认颜色
 */
@property(nonatomic,strong) UIColor*                bigCircleStrokeColor;

/**
 大圆选中颜色
 */
@property(nonatomic,strong) UIColor*                bigCircleSelectedStrokeColor;


/**
 小圆选中颜色
 */
@property(nonatomic,strong) UIColor*                smallCircleSelectedStrokeColor;

/**
 小圆填充色
 */
@property(nonatomic,strong) UIColor*                smallCircleFillColor;

/**
 线颜色
 */
@property(nonatomic,strong) UIColor*                lineStrokeColor;

/**
 代理
 */
@property(nonatomic,weak) id<RSHandlePWDViewDelegate>       delegate;
@end
#import "RSHandlePWDView.h"

#define RS_HandleBigCircleRadus     30.f
#define RS_HandleSmallCircleRadus   15.f
#define RS_CIRCLE_NOT_NEED_UPDATA   -1

@interface RSHandlePWDView()


/**
 显示默认圆(初始状态)
 */
@property(nonatomic,strong) CAShapeLayer*               circleLayer;

/**
 显示选中大圆
 */
@property(nonatomic,strong) CAShapeLayer*               selectedBigCircleLayer;

/**
 显示选中小圆
 */
@property(nonatomic,strong) CAShapeLayer*               selectedCircleLayer;

/**
 显示选中圆与圆之间的线
 */
@property(nonatomic,strong) CAShapeLayer*               linesLayer;

/**
 正在进行划线的起点
 */
@property(nonatomic,assign) CGPoint                     startPoint;

/**
 正在进行划线的终点
 */
@property(nonatomic,assign) CGPoint                     endPoint;

/**
 圆frame集合
 */
@property(nonatomic,strong) NSMutableArray*             bigCircleFrames;

/**
 保存手势数据
 */
@property(nonatomic,strong) NSMutableArray*             selectedValues;

/**
 选中小圆
 */
@property(nonatomic,strong) UIBezierPath*               selectedCirclePath;

/**
 选中大圆
 */
@property(nonatomic,strong) UIBezierPath*               selectedBigCirclePath;

/**
 选中圆之间的线
 */
@property(nonatomic,strong) UIBezierPath*               linePath;

/**
 临时layer,用于显示正在进行的划线
 */
@property(nonatomic,strong) CAShapeLayer*               tmpLayer;

/**
 临时线
 */
@property(nonatomic,strong) UIBezierPath*               tmpPath;
@end

@implementation RSHandlePWDView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
#pragma mark - override
- (void)layoutSubviews{
    [super layoutSubviews];
    self.circleLayer.frame = self.bounds;
    self.selectedCircleLayer.frame = self.bounds;
    self.selectedBigCircleLayer.frame = self.bounds;
    self.linesLayer.frame = self.bounds;
    self.tmpLayer.frame = self.bounds;
    if (!self.circleLayer.superlayer) {
        [self.layer addSublayer:self.circleLayer];
        [self RS_drawBigCircle];
    }
    if (!self.selectedCircleLayer.superlayer) {
        [self.layer addSublayer:self.selectedCircleLayer];
    }
    if (!self.selectedBigCircleLayer.superlayer) {
        [self.layer addSublayer:self.selectedBigCircleLayer];
    }
    if (!self.linesLayer.superlayer) {
        [self.layer addSublayer:self.linesLayer];
    }
    if (!self.tmpLayer.superlayer) {
        [self.layer addSublayer:self.tmpLayer];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch* touch = touches.anyObject;
    CGPoint startPoint = [touch locationInView:self];
    NSInteger index = [self RS_checkPointIfNeedSelect:startPoint];
    if (index != RS_CIRCLE_NOT_NEED_UPDATA) {
        self.startPoint = [self RS_setIndexSelectedAndReturnCircleCenterPoint:index];
    }else{
        self.startPoint = startPoint;
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.tmpPath removeAllPoints];
    [self.tmpPath moveToPoint:self.startPoint];
    UITouch* touch = touches.anyObject;
    CGPoint movePoint = [touch locationInView:self];
    NSInteger index = [self RS_checkPointIfNeedSelect:movePoint];
    if (index != RS_CIRCLE_NOT_NEED_UPDATA) {
        CGPoint center = [self RS_setIndexSelectedAndReturnCircleCenterPoint:index];
        self.startPoint = center;
    }else{
        self.endPoint = movePoint;
        if ([self RS_checkPointInsideCircle:self.startPoint]) {
            [self.tmpPath addLineToPoint:self.endPoint];
            self.tmpLayer.path = self.tmpPath.CGPath;
        }
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (self.delegate && [self.delegate respondsToSelector:@selector(handlePWDView:inputComplate:)]) {
        [self.delegate handlePWDView:self inputComplate:self.selectedValues.copy];
    }
    [self RS_reset];
}

#pragma mark - private

/**
 绘制初始状态大圆
 */
- (void)RS_drawBigCircle{
    UIBezierPath* path = [UIBezierPath bezierPath];
    CGFloat margin = (CGRectGetWidth(self.bounds) -3*2*RS_HandleBigCircleRadus)/2.0;
    for (int index = 0; index < 9; index++) {
        CGRect frame = CGRectMake(index%3*(2*RS_HandleBigCircleRadus+margin), (index/3)*(2*RS_HandleBigCircleRadus+margin), 2*RS_HandleBigCircleRadus, 2*RS_HandleBigCircleRadus);
        UIBezierPath* circlePath = [UIBezierPath bezierPathWithOvalInRect:frame];
        [path appendPath:circlePath];
        [self.bigCircleFrames addObject:NSStringFromCGRect(frame)];
    }
    self.circleLayer.path = path.CGPath;
    [self.circleLayer setNeedsDisplay];
}

/**
 重置状态
 */
- (void)RS_reset{
    [self.selectedValues removeAllObjects];
    self.startPoint = CGPointZero;
    self.endPoint = CGPointZero;
    [self.selectedCirclePath removeAllPoints];
    self.selectedCircleLayer.path = self.selectedCirclePath.CGPath;
    [self.selectedBigCirclePath removeAllPoints];
    self.selectedBigCircleLayer.path = self.selectedBigCirclePath.CGPath;
    [self.linePath removeAllPoints];
    self.linesLayer.path = self.linePath.CGPath;
    [self.tmpPath removeAllPoints];
    self.tmpLayer.path = self.tmpPath.CGPath;
}


/**
 检测坐标点位置是否需要更新圆的状态
 
 @param point 坐标点
 @return 需要更新->对应圆的索引;不需要->RS_CIRCLE_NOT_NEED_UPDATA
 */
- (NSInteger)RS_checkPointIfNeedSelect:(CGPoint)point{
    __block NSInteger index = RS_CIRCLE_NOT_NEED_UPDATA;
    [self.bigCircleFrames enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (CGRectContainsPoint(CGRectFromString(obj), point) && ![self.selectedValues containsObject:@(idx)]) {
            index = idx;
            return ;
        }
    }];
    return index;
}

/**
 验证点是否在任意园内
 
 @param point 坐标点
 @return YES/NO
 */
- (BOOL)RS_checkPointInsideCircle:(CGPoint)point{
    __block BOOL isInside = NO;
    [self.bigCircleFrames enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (CGRectContainsPoint(CGRectFromString(obj), point)) {
            isInside = YES;
            return ;
        }
    }];
    return isInside;
}

/**
 设置index索引所对用的圆为选中
 
 @param index 索引
 @return index所对用圆的中心点
 */
- (CGPoint)RS_setIndexSelectedAndReturnCircleCenterPoint:(NSInteger)index{
    CGRect frame = CGRectFromString(self.bigCircleFrames[index]);
    CGPoint center = CGPointMake( CGRectGetMaxX(frame)-RS_HandleBigCircleRadus, CGRectGetMaxY(frame)-RS_HandleBigCircleRadus);
    //绘制小圆
    UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:RS_HandleSmallCircleRadus startAngle:0 endAngle:2*M_PI clockwise:1];
    [self.selectedCirclePath appendPath:circlePath];
    self.selectedCircleLayer.path = self.selectedCirclePath.CGPath;
    //绘制大圆(选中状态)
    UIBezierPath* bigCirclePath = [UIBezierPath bezierPathWithOvalInRect:frame];
    [self.selectedBigCirclePath appendPath:bigCirclePath];
    self.selectedBigCircleLayer.path = self.selectedBigCirclePath.CGPath;
    //绘制圆与圆之间的线
    self.selectedValues.count == 0 ? [self.linePath moveToPoint:center] : [self.linePath addLineToPoint:center];
    self.linesLayer.path = self.linePath.CGPath;
    [self.selectedValues addObject:@(index)];
    return center;
}

#pragma mark - setter
- (void)setBigCircleStrokeColor:(UIColor *)bigCircleStrokeColor{
    _bigCircleStrokeColor = bigCircleStrokeColor;
    self.circleLayer.strokeColor = _bigCircleStrokeColor.CGColor;
}

- (void)setBigCircleSelectedStrokeColor:(UIColor *)bigCircleSelectedStrokeColor{
    _bigCircleSelectedStrokeColor = bigCircleSelectedStrokeColor;
    self.selectedBigCircleLayer.strokeColor = _bigCircleSelectedStrokeColor.CGColor;
}

- (void)setSmallCircleSelectedStrokeColor:(UIColor *)smallCircleSelectedStrokeColor{
    _smallCircleSelectedStrokeColor = smallCircleSelectedStrokeColor;
    self.selectedCircleLayer.strokeColor = _smallCircleSelectedStrokeColor.CGColor;
}

- (void)setSmallCircleFillColor:(UIColor *)smallCircleFillColor{
    _smallCircleFillColor = smallCircleFillColor;
    self.selectedCircleLayer.fillColor = _smallCircleFillColor.CGColor;
}

- (void)setLineStrokeColor:(UIColor *)lineStrokeColor{
    _lineStrokeColor = lineStrokeColor;
    self.linesLayer.strokeColor = _lineStrokeColor.CGColor;
    self.tmpLayer.strokeColor = _lineStrokeColor.CGColor;
}

#pragma mark - getter
- (CAShapeLayer *)circleLayer{
    if (!_circleLayer) {
        _circleLayer = [CAShapeLayer layer];
        _circleLayer.lineWidth = 2.0;
        _circleLayer.strokeColor = [UIColor lightGrayColor].CGColor;
        _circleLayer.fillColor = [UIColor clearColor].CGColor;
    }
    return _circleLayer;
}

- (CAShapeLayer *)selectedCircleLayer{
    if (!_selectedCircleLayer) {
        _selectedCircleLayer = [CAShapeLayer layer];
        _selectedCircleLayer.lineWidth = 2.0;
        _selectedCircleLayer.strokeColor = [UIColor whiteColor].CGColor;
        _selectedCircleLayer.fillColor = [UIColor colorWithRed:25/255.0 green:177/255.0 blue:1.0 alpha:1.0].CGColor;
    }
    return _selectedCircleLayer;
}

- (CAShapeLayer*)selectedBigCircleLayer{
    if (!_selectedBigCircleLayer) {
        _selectedBigCircleLayer = [CAShapeLayer layer];
        _selectedBigCircleLayer.lineWidth = 2.0;
        _selectedBigCircleLayer.strokeColor = [UIColor yellowColor].CGColor;
        _selectedBigCircleLayer.fillColor = [UIColor clearColor].CGColor;
    }
    return _selectedBigCircleLayer;
}

- (CAShapeLayer *)linesLayer{
    if (!_linesLayer) {
        _linesLayer = [CAShapeLayer layer];
        _linesLayer.lineWidth = 2.0;
        _linesLayer.strokeColor = [UIColor colorWithRed:25/255.0 green:177/255.0 blue:1.0 alpha:1.0].CGColor;
        _linesLayer.fillColor = [UIColor clearColor].CGColor;
    }
    return _linesLayer;
}

- (NSMutableArray *)bigCircleFrames{
    if (!_bigCircleFrames) {
        _bigCircleFrames = @[].mutableCopy;
    }
    return _bigCircleFrames;
}

- (NSMutableArray *)selectedValues{
    if (!_selectedValues) {
        _selectedValues = @[].mutableCopy;
    }
    return _selectedValues;
}

- (UIBezierPath *)selectedCirclePath{
    if (!_selectedCirclePath) {
        _selectedCirclePath = [UIBezierPath bezierPath];
    }
    return _selectedCirclePath;
}

- (UIBezierPath*)selectedBigCirclePath{
    if (!_selectedBigCirclePath) {
        _selectedBigCirclePath = [UIBezierPath bezierPath];
    }
    return _selectedBigCirclePath;
}

- (UIBezierPath *)linePath{
    if (!_linePath) {
        _linePath = [UIBezierPath bezierPath];
    }
    return _linePath;
}

- (CAShapeLayer *)tmpLayer{
    if (!_tmpLayer) {
        _tmpLayer = [CAShapeLayer layer];
        _tmpLayer.lineWidth = 2.0;
        _tmpLayer.strokeColor = [UIColor colorWithRed:25/255.0 green:177/255.0 blue:1.0 alpha:1.0].CGColor;
        _tmpLayer.fillColor = [UIColor clearColor].CGColor;
    }
    return _tmpLayer;
}

- (UIBezierPath *)tmpPath{
    if (!_tmpPath) {
        _tmpPath = [UIBezierPath bezierPath];
    }
    return _tmpPath;
}
@end
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容