//创建一个ClockView继承UIView
#import "ClockView.h"
@interface ClockView()
@property (nonatomic,strong) NSMutableArray * selectedBtn;
@property (nonatomic,assign) CGPoint curP;
@end
@implementation ClockView
- (NSMutableArray *)selectedBtn{
if (!_selectedBtn) {
_selectedBtn = [[NSMutableArray alloc]init];
}
return _selectedBtn;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self setUP];
self.backgroundColor = [UIColor clearColor];
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setUP];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
//添加按钮
- (void)setUP{
for (int i = 0 ; i < 9; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = i;
[btn setImage:[UIImage imageNamed:@"未选中时的图片"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"选中时的图片"] forState:UIControlStateSelected];
btn.userInteractionEnabled = NO;
[self addSubview:btn];
}
}
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat x = 0;
CGFloat y = 0;
CGFloat btnWH = 74;
int clomn = 3;
CGFloat margin = (self.bounds.size.width - btnWH * clomn) / (clomn + 1);
for (int i = 0; i<self.subviews.count; i++) {
int curCloumn = i % clomn;
int curRow = i / clomn;
x = margin + curCloumn * (btnWH + margin);
y = margin + curRow * (btnWH + margin);
UIButton * btn = self.subviews[i];
btn.frame = CGRectMake(x, y, btnWH, btnWH);
}
}
//获取当前手指所在的点
- (CGPoint)getCurrentPoint:(NSSet *)touches{
UITouch * touch = [touches anyObject];
return [touch locationInView:self];
}
//判断当前手指是否在按钮上
- (UIButton *)btnRectContainsPoint:(CGPoint)point{
for (UIButton * btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取当前手指所在的点
CGPoint curP = [self getCurrentPoint:touches];
//判断当前手指是否在按钮上
UIButton * btn = [self btnRectContainsPoint:curP];
if (btn) {
btn.selected = YES;
[self.selectedBtn addObject:btn];
}
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//获取当前手指所在的点
CGPoint curP = [self getCurrentPoint:touches];
self.curP = curP;
//判断当前手指是否在按钮上
UIButton * btn = [self btnRectContainsPoint:curP];
if (btn && btn.selected == NO) {
btn.selected = YES;
[self.selectedBtn addObject:btn];
}
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSMutableString * str = [NSMutableString string];
for (UIButton * btn in self.selectedBtn) {
[str appendFormat:@"%d",(int)btn.tag];
btn.selected = NO;
}
NSLog(@"%@",str);
[self.selectedBtn removeAllObjects];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect{
if (self.selectedBtn.count <= 0) {
return;
}
UIBezierPath * path = [UIBezierPath bezierPath];
for (int i = 0 ; i < self.selectedBtn.count; i++) {
UIButton * btn = self.selectedBtn[i];
if (i == 0) {
[path moveToPoint:btn.center];
}else{
[path addLineToPoint:btn.center];
}
}
[path addLineToPoint:self.curP];
path.lineWidth = 10;
[[UIColor redColor] set];
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinRound];
[path stroke];
}
@end