资源。
效果。
代码结构。
主体代码。
// 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