之前偶然之間在博客上看到一篇自定義手勢鍵盤的文章,但是感覺太生硬,在此做了優化.
首先自定義一個gestureview
@interface GestureView2 : UIView
//存放手指劃過的直線的點位置,是效果更加流暢
@property(nonatomic,strong)NSMutableArray *dataArr;
//存放手指劃過的按鈕的位置
@property(nonatomic,strong)NSMutableArray *btArr;
@property(nonatomic,assign)id<GestureViewDelegate>delegate;
@end
聲明代理
@protocol GestureViewDelegate <NSObject>
-(void)guetureEndedWithBtArr:(NSMutableArray *)btarr;
@end
接下來就開始實現這個view
重寫初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self makeUI];
}
return self;
}
界面搭建
-(void)makeUI{
self.dataArr = [NSMutableArray array];
self.btArr = [NSMutableArray array];
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(((self.frame.size.width-40)/3+20)*j+20, ((self.frame.size.width-40)/3+20)*i, (self.frame.size.width-40)/3-20, (self.frame.size.width-40)/3-20);
[bt setBackgroundImage:[UIImage imageNamed:@"star_gray.png"] forState:UIControlStateNormal];
[bt setBackgroundImage:[UIImage imageNamed:@"star_red.png"] forState:UIControlStateSelected];
[self addSubview:bt];
bt.userInteractionEnabled=NO;
}
}
}
在開始觸摸方法中開始繪制
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//判斷當前觸摸的點是否是在某一個button的范圍內,如果在說明觸摸到了button,則設置相應的按鈕變亮,并把它加入到_btArr中
[self btContainsPoint:point];
if (_btArr.count>0) {
[_dataArr addObjectsFromArray:_btArr];
}
else{
[self.dataArr addObject:[self cgpointToObjiec:point]];
}
//通知view重新繪制
[self setNeedsDisplay];
}
判斷觸摸是否在某個按鈕的范圍內
-(void)btContainsPoint:(CGPoint)point{
for (UIButton *bt in self.subviews) {
if (CGRectContainsPoint(bt.frame, point)) {
//包含說明點中按鈕,設置為高亮
if (bt && !bt.selected) {
bt.selected = YES;
//由于觸摸點是一個結構體,因此創建一個model,把結構體轉化成model并存入數組中
[self.btArr addObject:[self cgpointToObjiec:CGPointMake(bt.center.x, bt.center.y)]];
}
}
}
}
-(Point_ftt *)cgpointToObjiec:(CGPoint)point{
Point_ftt *p = [[Point_ftt alloc]init];
p.x = point.x;
p.y = point.y;
return p;
}
觸摸移動時操作
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
[self btContainsPoint:point];
[self.dataArr addObject:[self cgpointToObjiec:point]];
//通知view重新繪制
[self setNeedsDisplay];
}
觸摸結束時
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_dataArr removeAllObjects];
[self setNeedsDisplay];
[_delegate guetureEndedWithBtArr:_btArr];
}
最重要的繪制函數
-(void)drawRect:(CGRect)rect{
//獲取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//繪圖(線段)
if (_dataArr.count>0) {
Point_ftt *point = self.btArr[0];
CGContextMoveToPoint(ctx, point.x, point.y);
for (Point_ftt *p in _btArr) {
CGContextAddLineToPoint(ctx, p.x, p.y);
}
Point_ftt *p = [_dataArr lastObject];
CGContextAddLineToPoint(ctx, p.x, p.y);
}
else{
if (_btArr.count>0) {
Point_ftt *point = self.btArr[0];
CGContextMoveToPoint(ctx, point.x, point.y);
for (Point_ftt *p in _btArr) {
CGContextAddLineToPoint(ctx, p.x, p.y);
}
}
}
//設置線條的屬性
CGContextSetLineWidth(ctx, 5);
CGContextSetRGBStrokeColor(ctx, 20/255.0, 107/255.0, 153/255.0, 1);
CGContextStrokePath(ctx);
}
使用時在控制器中,你可以在代理方法中處理自己想要的邏輯
self.gestureView = [[GestureView2 alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.width)];
[self.view addSubview:_gestureView];
_gestureView.delegate = self;
_gestureView.backgroundColor = [UIColor lightGrayColor];
#pragma mark - 代理
-(void)guetureEndedWithBtArr:(NSMutableArray *)btarr{
[UIView transitionWithView:_gestureView duration:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
_gestureView.hidden = YES;
} completion:nil];
}