日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS图案解锁(九宫格)

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS图案解锁(九宫格) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//創建初始化函數;通過touches事件,隨手指位置移動,畫出相應的密碼解鎖的連線。#import "OXLockView.h" #import "OXLockViewController.h"#define kOXBaseCircleNumber 10000 // tag基數(請勿修改) #define kCircleMargin 32.0 // 圓點離屏幕左邊距 #define kCircleDiameter 68.0 // 圓點直徑 #define kOXCircleAlpha 1.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

總結

以上是生活随笔為你收集整理的iOS图案解锁(九宫格)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。