美文网首页iOSiOS Developer
使用CGContext自定义手势解锁控件

使用CGContext自定义手势解锁控件

作者: ys简单0 | 来源:发表于2017-04-27 18:02 被阅读38次

之前偶然之间在博客上看到一篇自定义手势键盘的文章,但是感觉太生硬,在此做了优化.
首先自定义一个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];
}

相关文章

网友评论

    本文标题:使用CGContext自定义手势解锁控件

    本文链接:https://www.haomeiwen.com/subject/hernzttx.html