iOS、OC 手势密码功能


在这里插入图片描述

  资源。
在这里插入图片描述

在这里插入图片描述

  效果。
在这里插入图片描述
在这里插入图片描述

  代码结构。
在这里插入图片描述

  主体代码。


// SsmmView.h

#import <UIKit/UIKit.h>
@class SsmmView;

NS_ASSUME_NONNULL_BEGIN

@protocol SsmmViewDelegate <NSObject>

- (void)ssmmView:(SsmmView *)view passwordString:(NSString *)string;

@end

@interface SsmmView : UIView

@property (nonatomic,weak) id<SsmmViewDelegate>delegate;

@end

NS_ASSUME_NONNULL_END


// SsmmView.m

#import "SsmmView.h"

@interface SsmmView () {
    NSMutableArray *arrButton_;
    NSMutableArray *arrSelButton_;
    CGPoint lastPoint_;
}

@end

@implementation SsmmView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initData];
        [self initView];
    }
    return self;
}

- (void)initData {
    arrButton_ = [NSMutableArray arrayWithCapacity:9];
    arrSelButton_ = [NSMutableArray arrayWithCapacity:9];
}

- (void)initView {
    
    for (NSInteger i = 0; i < 9; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"shoushi1"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"shoushi1_Sel"] forState:UIControlStateSelected];
        button.tag = i + 1;
        button.userInteractionEnabled = NO;
        [self addSubview:button];
        [arrButton_ addObject:button];
    }
    
}

- (void)resize {
    CGFloat w = 60;
    CGFloat sw = self.frame.size.width;
    CGFloat sh = self.frame.size.height;
    CGFloat pw = (sw - 3 * w) / 4;
    CGFloat ph = (sh - 3 * w) / 4;
    
    for (NSInteger i = 0; i < 9; i ++) {
        UIButton *button = arrButton_[i];
        button.frame = CGRectMake(pw + (pw + w) * (i % 3), ph + (ph + w) * (i / 3), w, w);
    }
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self resize];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    for (UIButton *button in arrButton_) {
        if (CGRectContainsPoint(button.frame, point)) {
            if (![arrSelButton_ containsObject:button]) {
                [arrSelButton_ addObject:button];
                button.selected = YES;
                [self setNeedsDisplay];
            }
            break;
        }
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    for (UIButton *button in arrButton_) {
        if (CGRectContainsPoint(button.frame, point)) {
            if (![arrSelButton_ containsObject:button]) {
                [arrSelButton_ addObject:button];
                button.selected = YES;
            }
            break;
        }
    }
    
    lastPoint_ = point;
    [self setNeedsDisplay];
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (arrSelButton_.count) {
        NSString *string = @"";
        for (UIButton *button in arrSelButton_) {
            string = [string stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)button.tag]];
            button.selected = NO;
        }
        [arrSelButton_ removeAllObjects];
        
        if (_delegate && [_delegate respondsToSelector:@selector(ssmmView:passwordString:)]) {
            [_delegate ssmmView:self passwordString:string];
        }
        
        lastPoint_ = CGPointZero;
        [self setNeedsDisplay];
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    if (arrSelButton_.count) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 2.0f);
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        NSInteger i = 0;
        for (UIButton *button in arrSelButton_) {
            if (i == 0) {
                CGContextMoveToPoint(context, button.center.x, button.center.y);
            } else {
                CGContextAddLineToPoint(context, button.center.x, button.center.y);
            }
            i ++;
        }
        if (!CGPointEqualToPoint(lastPoint_, CGPointZero)) {
            CGContextAddLineToPoint(context, lastPoint_.x, lastPoint_.y);
        }
        CGContextStrokePath(context);
    }
}

@end


// ViewController.m

#import "ViewController.h"
#import "SsmmView.h"

@interface ViewController ()<SsmmViewDelegate>{
    SsmmView *view_;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    SsmmView *view = [[SsmmView alloc] init];
    view.frame = CGRectMake(50, 200, self.view.frame.size.width - 100, 300);
    view.delegate = self;
    [self.view addSubview:view];
    
}

- (void)ssmmView:(SsmmView *)view passwordString:(NSString *)string {
    NSLog(@"%@",string);
}

@end


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值