Objective-C 手勢解鎖App,objective-capp
Objective-C 手勢解鎖App
IOS 手勢鎖屏實現邏輯分析: 1、通過迴圈在頁面中添加透明背景白色邊框的按鈕(預設為九個)並設定 tag 值,以便與原始密碼核對 2、撲捉手勢,主要用到touchesBegan、touchesMoved 和 touchesEnded 3、將手勢經過的按鈕劃線連結起來,主要用到 CGContextRef 2D繪畫對象如下代碼所示為 IOS 手勢鎖屏全部 App源碼,僅供學習:ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController//圖片控制項(畫板)@property (weak, nonatomic) IBOutlet UIImageView *imgView;//起止點@property (nonatomic,assign) CGPoint lineStartPoint;@property (nonatomic,assign) CGPoint lineEndPoint;//所有按鈕和選中的按鈕@property (nonatomic,strong) NSMutableArray *buttonArray;@property (nonatomic,strong) NSMutableArray *selectedButtons;@property (nonatomic,assign) BOOL drawFlag;@end
ViewController.m
#import "ViewController.h"@implementation ViewController#pragma mark- Class property@synthesize drawFlag,imgView;@synthesize lineStartPoint,lineEndPoint;@synthesize buttonArray,selectedButtons;#pragma mark-Global variablesNSArray *arrImgs = nil;#pragma mark- Class method/** * 視圖載入 */- (void)viewDidLoad { [super viewDidLoad]; //設定定位 self.imgView.frame = self.view.frame; //背景圖片數組 arrImgs = [NSArray arrayWithObjects:@"bg_01.jpg",@"bg_02.jpg",@"bg_03.jpeg", @"bg_04.png",@"bg_05.jpg",@"bg_06.jpeg",@"bg_07.jpeg", nil]; //載入按鈕 [self loadButtons]; //隨機設定背景圖片 [self ChangeBgImg];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/** * shouldAutorotate * 是否允許內容跟著螢幕切換而改變 * * @return Bool */-(BOOL) shouldAutorotate{ return NO;}/** * 載入按鈕 */-(void) loadButtons{ float w = 70; float h = 70; float x = 15; float y = 150; int space = 40; self.buttonArray = [[NSMutableArray alloc] init]; for (int i = 0; i < 9; i++) { if ((i % 3 == 0) && i > 0) { //另起一行 y += h + space; x = 15; } //添加按鈕 [self CreateButton:(i+1) x: x y:y h:h w:w]; x += w + space; }}/** * touchesBegan 觸摸開始事件 * * @param touches touches description * @param event event description */-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if (touch) { CGPoint touchPoint; for (UIButton *btn in self.buttonArray) { touchPoint = [touch locationInView:btn]; if ([btn pointInside:touchPoint withEvent:nil]) { self.lineStartPoint = btn.center; self.drawFlag = YES; if (!self.selectedButtons) self.selectedButtons = [NSMutableArray arrayWithCapacity:9]; [self.selectedButtons addObject:btn]; } } }}/** * touchesMoved 一根或者多根手指在螢幕上移動 * * @param touches touches description * @param event event description */-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch =[touches anyObject]; if (touch && self.drawFlag) { self.lineEndPoint = [touch locationInView:imgView]; CGPoint touchPoint; BOOL btnContained; for (UIButton *btn in self.buttonArray) { touchPoint = [touch locationInView:btn]; if ([btn pointInside:touchPoint withEvent:nil]) { btnContained = NO; //如要實現比較複雜手勢,可考慮注釋如下迴圈 for (UIButton *selectedBtn in self.selectedButtons) { if (btn == selectedBtn) { btnContained = YES; break; } } if (!btnContained) [self.selectedButtons addObject:btn]; } } //劃線 UIColor *color = [UIColor whiteColor]; [self drawLineWithColor:color width:8.0f]; }}/** * touchesEnded 滑動手勢結束 * * @param touches touches description * @param event event description */-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ self.drawFlag = NO; self.selectedButtons = nil; //判斷手勢密碼是否正確,……}/** * drawLineWithColor * 自訂劃線功能 * * @param color 顏色 * @param width 寬度 * @param startPoint 開始位置 * @param endPoint 結束位置 * * @return UIImage */-(void) drawLineWithColor:(UIColor *)color width:(CGFloat)width{ CGPoint btnCenter; UIImage *image = nil; UIGraphicsBeginImageContext(self.imgView.frame.size); //一個不透明類型的Quartz 2D繪畫環境,相當於一個畫布,你可以在上面任意繪畫 CGContextRef context = UIGraphicsGetCurrentContext(); //設定劃線寬度和顏色 CGContextSetLineWidth(context, width); CGContextSetStrokeColorWithColor(context, [color CGColor]); /** * CGLineCap * kCGLineCapButt //和第三個一樣 * kCGLineCapRound //端點是圓的 * kCGLineCapSquare //正方形 */ //設定線的端點形狀 CGContextSetLineCap(context, kCGLineCapRound); //設定線的起始位置 CGContextMoveToPoint(context, self.lineStartPoint.x, self.lineStartPoint.y); //將選中的點串連起來 for (UIButton *selectBtn in self.selectedButtons) { btnCenter = selectBtn.center; CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y); CGContextMoveToPoint(context, btnCenter.x, btnCenter.y); } //結束位置 CGContextAddLineToPoint(context, self.lineEndPoint.x, self.lineEndPoint.y); CGContextStrokePath(context); image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //追加到畫板 self.imgView.image = image; image = nil;}/** * CButton 自訂按鈕 * * @param int 按鈕的值 * @param float x * @param float y * @param float width * @param float height * * @return void */-(void) CreateButton:(int) v x:(float) x y:(float) y h:(float) h w:(float) w{ //初始化按鈕 UIButton *cbtn = [[UIButton alloc] init]; //設定位置和尺寸 cbtn.frame = CGRectMake(x, y, w, h); //設定按鈕資訊 cbtn.userInteractionEnabled = NO; cbtn.tag = v; //設定字型顏色 [cbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; //設定背景顏色 [cbtn setBackgroundColor:[UIColor clearColor]]; //設定為圓形 cbtn.layer.cornerRadius = 35; cbtn.layer.borderWidth = 3.5; cbtn.layer.borderColor = [UIColor whiteColor].CGColor; cbtn.clipsToBounds = TRUE;//去除邊界 //添加到當前視圖 [self.view addSubview:cbtn]; //添加到數組 [self.buttonArray addObject:cbtn]; //釋放 cbtn = nil;};/** * ChangeBgImg * 隨機設定背景圖片 */-(void) ChangeBgImg{ //擷取背景圖片控制項 UIImageView *bgImgV = (UIImageView*)[self.view viewWithTag:11]; if (bgImgV) { bgImgV.frame = self.view.frame; //擷取隨機索引 int rd = 0 + (arc4random() % ([arrImgs count] - 1)); NSString *strImgName = (NSString*)[arrImgs objectAtIndex:rd]; bgImgV.image = [UIImage imageNamed:strImgName]; }}@end
源碼:http://pan.baidu.com/s/1bnhLWIF
在本項目製作過程中,如下文章給予了相當大的協助,以示感謝:
http://blog.csdn.net/jasonblog/article/details/8024014
http://blog.csdn.net/jasonblog/article/details/8024674
http://blog.sina.com.cn/s/blog_7a162d000101dag2.html
如下為: