900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > iOS图案解锁(九宫格)

iOS图案解锁(九宫格)

时间:2018-07-12 21:10:22

相关推荐

iOS图案解锁(九宫格)

//创建初始化函数;通过touches事件,随手指位置移动,画出相应的密码解锁的连线。#import "OXLockView.h"#import "OXLockViewController.h"#define kOXBaseCircleNumber10000 // tag基数(请勿修改)#define kCircleMargin 32.0 // 圆点离屏幕左边距#define kCircleDiameter 68.0 // 圆点直径#define kOXCircleAlpha1.0 // 圆点透明度#define kOXLineWidth 4.0 // 线条宽#define kOXLineColor [UIColor colorWithRed:77.0/255.0 green:76.0/255.0 blue:156.0/255.0 alpha:0.8] // 线条色蓝#define kOXLineColorWrong [UIColor colorWithRed:201.0/255.0 green:9.0/255.0 blue:22.0/255.0 alpha:0.8] // 线条色红@interface OXLockView () {NSMutableArray* _circleArray;NSMutableArray* _selectedCircleArray;NSMutableArray* _wrongCircleArray;CGPoint nowPoint;NSTimer* timer;BOOL isWrongColor;BOOL isDrawing;// 标记是否正在绘图中}@end@implementation OXLockView//No.1//开始写代码,重写父类创建方法,使当前view在代码、storyboard或xib下都能正常创建- (instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self) {//添加子控件[self initCircles];}return self;}//end_code- (void)initCircles {self.clipsToBounds = YES;_circleArray = [NSMutableArray array];_selectedCircleArray = [NSMutableArray array];for (int i = 0; i < 9; i++) {int x = kCircleMargin + (i%3) * (kCircleDiameter+(320-kCircleMargin*2- kCircleDiameter *3)/2);int y = kCircleMargin + (i/3) * (kCircleDiameter+(320-kCircleMargin*2- kCircleDiameter *3)/2);UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];button.frame = CGRectMake(x, y, kCircleDiameter, kCircleDiameter);[button setBackgroundColor:[UIColor clearColor]];[button setBackgroundImage:[UIImage imageNamed:@"circle_normal"] forState:UIControlStateNormal];[button setBackgroundImage:[UIImage imageNamed:@"circle_selected"] forState:UIControlStateSelected];button.userInteractionEnabled = NO;//禁止用户交互button.alpha = kOXCircleAlpha;button.tag = i + kOXBaseCircleNumber + 1; // tag从基数+1开始,[self addSubview:button];[_circleArray addObject:button];}self.backgroundColor = [UIColor clearColor];}#pragma mark - Touches Event- (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;CGPoint point = [[touches anyObject] locationInView:self];[self updatePositionWithTouches:touches];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {[self endPosition];}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {[self endPosition];}#pragma mark - Draw Line- (void)drawRect:(CGRect)rect {//No.2//开始写代码,使用CoreGraphics的方法画连接各点的线,根据上下文提示设置线条宽度、区分正误线颜色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];}}//添加一根线到当前手指所在的点[path addLineToPoint:nowPoint];//设置线的状态[path setLineWidth:10];[[UIColor greenColor] set];[path setLineJoinStyle:kCGLineJoinRound];[path stroke];}//end_code}#pragma mark - 处理- (void)updatePositionWithTouches:(NSSet *)touches{//No.3//开始写代码,根据上下文补全改变当前手指位置时相应的处理//记录当前手指的位置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];//end_code}/- (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-kOXBaseCircleNumber];}[self clearColorAndSelectedButton]; // 清除到初始样式if ([self.delegate respondsToSelector:@selector(lockString:)]) {if (string && string.length>0) {[self.delegate lockString:string];}}}/**清除至初始状态*/- (void)clearColor {if (isWrongColor) {// 重置颜色isWrongColor = NO;for (UIButton* button in _circleArray) {[button setBackgroundImage:[UIImage imageNamed:@"circle_selected"] forState:UIControlStateSelected];}}}- (void)clearSelectedButton {for (UIButton *thisButton in _circleArray) {[thisButton setSelected:NO];}[_selectedCircleArray removeAllObjects];[self setNeedsDisplay];}- (void)clearColorAndSelectedButton {if (!isDrawing) {[self clearColor];[self clearSelectedButton];}}#pragma mark - Error Show- (void)showErrorCircles:(NSString*)string {isWrongColor = YES;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];[_selectedCircleArray addObject:_circleArray[number.intValue]];}for (UIButton* button in _circleArray) {if (button.selected) {[button setBackgroundImage:[UIImage imageNamed:@"circle_wrong"] forState:UIControlStateSelected];}}[self setNeedsDisplay];timer = [NSTimer scheduledTimerWithTimeInterval:1.0target:selfselector:@selector(clearColorAndSelectedButton)userInfo:nilrepeats:NO];}@end

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。