#import "LSbackView.h"
@interface LSbackView()
@property(nonatomic,strong)NSMutableArray *btnArray;
@property(nonatomic,strong)NSMutableArray
@property(nonatomic,assign)CGPoint lastPoint;
@property(nonatomic,strong)UIColor *lineColor;
@end
@implementation LSbackView
-(UIColor *)lineColor{
if (!_lineColor) {
_lineColor = [UIColor whiteColor];
}
return _lineColor;
}
-(NSMutableArray *)selectionArray{
if (!_selectionArray) {
_selectionArray = [NSMutableArray array];
}
return _selectionArray;
}
-(NSMutableArray *)btnArray{
if (!_btnArray) {
_btnArray = [NSMutableArray array];
for (NSInteger i=0; i<9; i++) {
UIButton *button = [[UIButton alloc]init];
button.tag = i;
NSLog(@"%ld",i);
button.userInteractionEnabled = NO;
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_error"] forState:UIControlStateSelected];
[_btnArray addObject:button];
}
}
return _btnArray;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self=[super initWithFrame:frame]) {
CGFloat W = self.frame.size.width;
CGFloat H = self.frame.size.height;
CGFloat btnW = 74;
CGFloat btnH = 74;
NSInteger column = 3;
CGFloat marginW = (W-column*btnW)/(column-1);
CGFloat marginH = (H-column*btnH)/(column-1);
for (NSInteger i=0; i NSInteger row = i/column; NSInteger col = i%column; CGFloat btnX = (btnW + marginW)*col; CGFloat btnY = (btnH + marginH)*row; UIButton *btn = self.btnArray[i]; btn.frame = CGRectMake(btnX, btnY, btnW, btnH); [self addSubview:btn]; } } return self; } -(void)drawRect:(CGRect)rect{ UIBezierPath *path = [UIBezierPath bezierPath]; for (NSInteger i=0;i if (i==0) { [path moveToPoint:self.selectionArray[i].center]; }else{ [path addLineToPoint:self.selectionArray[i].center]; } } if (self.selectionArray.count>0) { [path addLineToPoint:self.lastPoint]; } [self.lineColor set]; [path stroke]; } -(void)touchesBegan:(NSSet // UITouch *touch = touches.anyObject; // CGPoint loc = [touch locationInView:touch.view]; // for (UIButton *btn in self.btnArray) { // if (CGRectContainsPoint(btn.frame, loc)) { // btn.highlighted = YES; // } // } for (UITouch *touch in touches) { CGPoint loc = [touch locationInView:touch.view]; self.lastPoint = loc; for (UIButton *btn in self.btnArray) { if (CGRectContainsPoint(btn.frame, loc)&&!btn.highlighted) { btn.highlighted = YES; [_selectionArray addObject:btn]; } } } [self setNeedsDisplay]; } -(void)touchesMoved:(NSSet UITouch *touch = touches.anyObject; CGPoint loc = [touch locationInView:touch.view]; self.lastPoint = loc; for (UIButton *btn in self.btnArray) { if (CGRectContainsPoint(btn.frame, loc)&&!btn.highlighted) { btn.highlighted = YES; [_selectionArray addObject:btn]; } } [self setNeedsDisplay]; } //-(void)touchesEnded:(NSSet // //} -(void)touchesEnded:(NSSet self.lastPoint = [self.selectionArray lastObject].center; NSMutableString *str = [NSMutableString string]; for (UIButton *button in self.selectionArray) { [str appendFormat:@"%@",@(button.tag)]; } if ([str isEqualToString:@"012"]) { [self clear]; }else{ self.lineColor = [UIColor redColor]; [self setNeedsDisplay]; self.userInteractionEnabled = NO; for (UIButton *button in self.selectionArray) { button.highlighted = NO; button.selected = YES; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self clear]; self.userInteractionEnabled = YES; }); } } -(void)clear{ for (UIButton *button in self.selectionArray) { button.highlighted = NO; button.selected = NO; self.lineColor = [UIColor whiteColor]; } [self.selectionArray removeAllObjects]; [self setNeedsDisplay]; } @end 最容易出坑的地方 CGRectContainPoint方法 还有在drawRect方法里添加线段的方法 设置数组添加线段的方法 最后血淋淋呀,还有添加选择按钮进数组的时候,如果没有!btn.highlighted这个条件,密码始终是打印一堆,人都要疯了 添加线段注意写在括号的里外问题,注意呀 注意