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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

IOS开发基础之手势解锁项目案例

發布時間:2023/12/18 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之手势解锁项目案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOS開發基礎之手勢解鎖項目案例

項目最終實現效果。
由于缺少紅色的error背景圖。我自己從安卓項目找到一個手勢解鎖,然后通過ps添加粉紅色的紅圈,才得以解決。為了分享給大家源碼,github和本地都進行了備份。項目才100K左右。最后用到了block回調思想或者設置代理的方式。

github源碼地址:https://github.com/sunjunjunsun/Gesture-unlocking.git
CSDN資源地址:
https://download.csdn.net/download/A1521315qwss/15398441
具體的實現代碼:

// // ViewController.m // 27-手勢解鎖 // // Created by 魯軍 on 2021/2/20. // #import "ViewController.h" #import "LJView.h" @interface ViewController () @property (weak, nonatomic) IBOutlet LJView *passwordView; @end @implementation ViewController - (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_refresh_bg"]];self.passwordView.passwordBlock = ^(NSString * pwd) {if([pwd isEqualToString:@"123"]){NSLog(@"輸入密碼正確");return YES;}else{NSLog(@"輸入密碼錯誤");return NO;}}; } @end // LJView.h // 27-手勢解鎖 // Created by 魯軍 on 2021/2/20. #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface LJView : UIView @property(nonatomic,copy) BOOL (^passwordBlock)(NSString *); @end NS_ASSUME_NONNULL_END // LJView.m // 27-手勢解鎖 // Created by 魯軍 on 2021/2/20. #import "LJView.h" #define kButtonCount 9 @interface LJView () @property(nonatomic,strong)NSMutableArray *btns; //所有需要連線的數組 @property(nonatomic,strong)NSMutableArray *lineBtns; @property(nonatomic,assign)CGPoint currentPoint; @end @implementation LJView //畫線 - (void)drawRect:(CGRect)rect{//如果沒有需要畫線的按鈕。那么不需要的執行drawrect 方法if(!self.lineBtns.count){return;}UIBezierPath *path = [UIBezierPath bezierPath];for(int i=0;i<self.lineBtns.count;++i){UIButton *btn =self.lineBtns[i];if(i==0){[path moveToPoint:btn.center];}else{[path addLineToPoint:btn.center];}}//連線到手指的位置[path addLineToPoint:self.currentPoint];[[UIColor whiteColor] set];[path setLineWidth:10];// 設置連接處的樣式[path setLineJoinStyle:kCGLineJoinRound];//設置頭尾的樣式[path setLineCapStyle:kCGLineCapRound];[path stroke]; }- (NSMutableArray *)lineBtns{if(!_lineBtns){_lineBtns = [NSMutableArray array];}return _lineBtns; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//獲取觸摸對象UITouch *t=touches.anyObject;//獲取手指的位置CGPoint p=[t locationInView:t.view];self.currentPoint = p;//獲取btnfor(int i=0;i<self.btns.count;++i){UIButton *btn = self.btns[i];if(CGRectContainsPoint(btn.frame, p)){btn.selected = YES;if(![self.lineBtns containsObject:btn]){//添加到需要畫線的數組當中[self.lineBtns addObject:btn];}}}//重繪[self setNeedsDisplay]; }- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{self.currentPoint = [[self.lineBtns lastObject] center];[self setNeedsDisplay];NSString *password = @"";for(int i=0;i<self.lineBtns.count;++i){UIButton *btn =self.lineBtns[i];btn.selected = NO;btn.enabled = NO;password = [password stringByAppendingFormat:[NSString stringWithFormat:@"%ld",btn.tag]];}NSLog(@"%@",password);if(self.passwordBlock){if(self.passwordBlock(password)){NSLog(@"正確");}else{NSLog(@"錯誤");}}[self setUserInteractionEnabled:NO];dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[self setUserInteractionEnabled:YES];[self clear];});} //清空 恢復到最初始狀態 -(void)clear{for(int i=0;i<self.btns.count;++i){UIButton *btn = self.btns[i];btn.selected = NO;btn.enabled = YES;}//清空所有線[self.lineBtns removeAllObjects];[self setNeedsDisplay]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//獲取觸摸對象UITouch *t=touches.anyObject;//獲取手指的位置CGPoint p=[t locationInView:t.view];//獲取btnfor(int i=0;i<self.btns.count;++i){UIButton *btn = self.btns[i];if(CGRectContainsPoint(btn.frame, p)){btn.selected = YES;//添加到需要畫線的數組當中[self.lineBtns addObject:btn];}} }- (NSMutableArray *)btns{if(!_btns){_btns =[NSMutableArray array];}return _btns; } - (void)awakeFromNib{for(int i=0;i<kButtonCount;++i){UIButton *btn = [[UIButton alloc] init];// btn.backgroundColor = [UIColor redColor];btn.tag = i;[btn setUserInteractionEnabled:NO];[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_error-2"] forState:UIControlStateDisabled];[self addSubview:btn];[self.btns addObject:btn];} }- (void)layoutSubviews{[super layoutSubviews];CGFloat w = 74;CGFloat h = w;int colCount = 3;CGFloat margin =(self.frame.size.width - 3 *w)/4;for(int i=0;i<kButtonCount;++i){CGFloat x = (i%colCount)*(margin+w)+margin;CGFloat y = (i/colCount)*(margin+w)+margin;[self.btns[i] setFrame:CGRectMake(x, y, w, h)];} } @end 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的IOS开发基础之手势解锁项目案例的全部內容,希望文章能夠幫你解決所遇到的問題。

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