问题:项目需要自定义一个九宫格图案锁
解决问题:首先我们自定义一个LockView继承UIView
我们要做的就是在这个LockView中去铺设我们的9宫格图案
LockView.m中
// tag基数
#define BaseTag 10000
@interface LockView () {
NSMutableArray* _circleArray;//铺设的button
NSMutableArray* _selectedCircleArray;//已选择的button
CGPoint nowPoint;//当前触摸位置
BOOL isWrongColor;// 标记是否正在显示错误的绘图
BOOL isDrawing; // 标记是否正在绘图中
UIColor *tempLineColor; //临时的连接线的颜色
UIColor *wrongColor;//档图案错误的时候显示的颜色
UIColor *themeColor;//button未选择时显示的颜色
UIColor *lineColor;//连接线的颜色
UIColor *selectedPointColor;//button选中后颜色
CGFloat margin;//距离屏幕左右的距离
CGFloat buttonRaius;//button半径
CGFloat lineWidth;//连接线的宽度
CGFloat buttonBorderWidth;//button的连接线宽度
}
@end
@implementation LockView
// 自定义初始化方法
- (instancetype)initLockViewWith:(CGRect)frame forWrongColor:(UIColor *)wrongColor themeColor:(UIColor *)themeColor lineColor:(UIColor *)lineColor selectedPointColor:(UIColor *)selectedPointColor margin:(CGFloat)margin buttonRadius:(CGFloat)buttonRaius lineWidth:(CGFloat)lineWidth buttonBorderWidth:(CGFloat)buttonBorderWidth{
self = [super initWithFrame:frame];
if (self) {
self->wrongColor = wrongColor;
self->themeColor = themeColor;
self->lineColor = lineColor;
self->selectedPointColor = selectedPointColor;
self->margin = margin;
self->buttonRaius = buttonRaius;
self->lineWidth = lineWidth;
self->buttonBorderWidth = buttonBorderWidth;
tempLineColor = lineColor;
[self initCircles];
}
return self;
}
//初始化9宫格图案
- (void)initCircles {
self.clipsToBounds = YES;
_circleArray = [NSMutableArray array];
_selectedCircleArray = [NSMutableArray array];
for (int i = 0; i < 9; i++) {
//之所以用button 是因为 button有selected这个属性 用起来比较方便
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, buttonRaius*2, buttonRaius*2);
button.userInteractionEnabled = NO;//禁止用户交互(只做展示不做交互)
CGFloat pointWidth = (self.frame.size.width-margin*2-buttonRaius*2)/2;
button.center = CGPointMake(self.center.x + ((i%3)-1)*pointWidth, self.center.y+((i/3)-1)*pointWidth);
[button setBackgroundImage:[self getCircularSize:button.frame.size borderWith:buttonBorderWidth color:themeColor insideColor:lineColor solid:NO] forState:UIControlStateNormal];
[button setBackgroundImage:[self getCircularSize:button.frame.size borderWith:buttonBorderWidth color:themeColor insideColor:lineColor solid:YES] forState:UIControlStateSelected];
button.tag = i + BaseTag + 1; // tag从基数+1开始,
[self addSubview:button];
[_circleArray addObject:button];
}
self.backgroundColor = [UIColor clearColor];
}
//触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
isDrawing = NO;
if (isWrongColor) {
[self clearColorAndSelectedButton];
}
[self updatePositionWithTouches:touches];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
isDrawing = YES;
[self updatePositionWithTouches:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self endPosition];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self endPosition];
}
//在这里我们去连接路径
- (void)drawRect:(CGRect)rect {
if (_selectedCircleArray.count) {
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i = 0; i < _selectedCircleArray.count; i++) {
//如果说按钮是第一个,让按钮的中心点是路径的起点
UIButton *btn = _selectedCircleArray[i];
if (i == 0) {
[path moveToPoint:btn.center];
} else {
[path addLineToPoint:btn.center];
}
}
if (!isWrongColor) {
//添加一根线到当前手指所在的点
[path addLineToPoint:nowPoint];
//让线头看起来圆滑一点
UIBezierPath * rounds=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(nowPoint.x-lineWidth/2, nowPoint.y-lineWidth/2, lineWidth, lineWidth) cornerRadius:lineWidth/2];
//渲染
[[UIColor greenColor] setFill];
[rounds fill];
}
//设置线的状态
[path setLineWidth:lineWidth];
[tempLineColor set];
[path setLineJoinStyle:kCGLineJoinRound];
[path stroke];
}
}
//当手指移动时判断是否在九宫格button上
- (void)updatePositionWithTouches:(NSSet *)touches{
//记录当前手指的位置
UITouch *touch = [touches anyObject];
CGPoint curP = [touch locationInView:self];
nowPoint = curP;
//判断当前点在不在按钮身上 如果按钮不为空 保存选中的按钮
UIButton *btn = [self btnContainsPoint:curP];
if (btn && btn.selected == NO) {
btn.selected = YES;
[_selectedCircleArray addObject:btn];
}
[self setNeedsDisplay];
}
- (UIButton *)btnContainsPoint:(CGPoint)point
{
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
- (void)endPosition {
isDrawing = NO;
UIButton *strbutton;
NSString *string=@"";
for (int i=0; i < _selectedCircleArray.count; i++) {
strbutton = _selectedCircleArray[i];
string= [string stringByAppendingFormat:@"%ld",(long)strbutton.tag-BaseTag];
}
//这个string 就是我们解锁图案数字的排列顺序
[self.delegate sendLockString:string];//这里是我们的代理 用来把排列顺序string传递到我们的父视图
//也可以在这里判断
if ([string isEqualToString:self.rightString]) {
....
}else{
//这里我直接当错误处理
[self showErrorCircles:string];
}
}
/**
清除至初始状态
*/
- (void)clearColor {
if (isWrongColor) {// 重置颜色
isWrongColor = NO;
tempLineColor = lineColor;
}
}
- (void)clearSelectedButton {
for (UIButton *thisButton in _circleArray) {
[thisButton setSelected:NO];
[thisButton setBackgroundImage:[self getCircularSize:thisButton.frame.size borderWith:buttonBorderWidth color:themeColor insideColor:selectedPointColor solid:YES] forState:UIControlStateSelected];
}
[_selectedCircleArray removeAllObjects];
[self setNeedsDisplay];
}
- (void)clearColorAndSelectedButton {
if (!isDrawing) {
[self clearColor];
[self clearSelectedButton];
self.userInteractionEnabled = YES;
}
}
#pragma mark - Error Show
- (void)showErrorCircles:(NSString*)string {
isWrongColor = YES;
tempLineColor = wrongColor;
NSMutableArray* numbers = [[NSMutableArray alloc] initWithCapacity:string.length];
for (int i = 0; i < string.length; i++) {
NSRange range = NSMakeRange(i, 1);
NSNumber* number = [NSNumber numberWithInt:[string substringWithRange:range].intValue-1]; // 数字是1开始的
[numbers addObject:number];
[_circleArray[number.intValue] setSelected:YES];
}
for (UIButton* button in _circleArray) {
if (button.selected) {
[button setBackgroundImage:[self getCircularSize:button.frame.size borderWith:buttonBorderWidth color:themeColor insideColor:tempLineColor solid:YES] forState:UIControlStateSelected];
}
}
[self setNeedsDisplay];
self.userInteractionEnabled = NO;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(clearColorAndSelectedButton) userInfo:nil repeats:NO];
}
// 生成我们选中和未选中的图片
-(UIImage *)getCircularSize:(CGSize)size borderWith:(CGFloat)borderWith color:(UIColor *)color insideColor:(UIColor *)insideColor solid:(BOOL)solid{
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//获取颜色RGB
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
CGFloat alpha = 0.0;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
CGContextSetRGBStrokeColor(context,red,green,blue,1.0);//画笔线的颜色
CGContextSetLineWidth(context, borderWith);//线的宽度
if (solid) {
CGContextAddArc(context, size.width/2, size.height/2, size.width/2-size.width/4, 0, 2*M_PI, 0);
CGContextSetFillColorWithColor(context, insideColor.CGColor);
CGContextDrawPath(context, kCGPathFill);//绘制填充
}
CGContextAddArc(context, size.width/2, size.height/2, size.width/2-borderWith, 0, 2*M_PI, 0); //添加一个圆
CGContextDrawPath(context, kCGPathStroke); //绘制路径
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭图形上下文
UIGraphicsEndImageContext();
return newImage;
}
在LockView.h中来写我们的协议
@protocol LockViewDelegate
- (void)sendLockString:(NSString *)lockString;
@end
@interface LockView : UIView
- (instancetype)initLockViewWith:(CGRect)frame forWrongColor:(UIColor *)wrongColor themeColor:(UIColor *)themeColor lineColor:(UIColor *)lineColor selectedPointColor:(UIColor *)selectedPointColor margin:(CGFloat)margin buttonRadius:(CGFloat)buttonRaius lineWidth:(CGFloat)lineWidth buttonBorderWidth:(CGFloat)buttonBorderWidth;
@property id<LockViewDelegate>delegate;
@property NSString *rightString;
大功告成
QQ20180912-140117-HD.gif
网友评论