现在的位置: 首页 > 综合 > 正文

IOS之手势锁屏

2013年10月01日 ⁄ 综合 ⁄ 共 5834字 ⁄ 字号 评论关闭

在iOS上增加手势锁屏、解锁功能

具体的代码实现如下:

  1. //  
  2. //  ViewController.m  
  3. //  GestureLock  
  4. //  
  5. //  Created by Jason Lee on 12-9-26.  
  6. //  Copyright (c) 2012年 Jason Lee. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. #define LOCK_POINT_TAG      1000  
  12.   
  13. @interface ViewController ()  
  14.   
  15. @property (nonatomic, strong) UIImageView *imageView;  
  16.   
  17. @property (nonatomic, assign) CGPoint lineStartPoint;  
  18. @property (nonatomic, assign) CGPoint lineEndPoint;  
  19.   
  20. @property (nonatomic, strong) NSMutableArray *buttonArray;  
  21. @property (nonatomic, strong) NSMutableArray *selectedButtons;  
  22.   
  23. @property (nonatomic, assign) BOOL drawFlag;  
  24.   
  25. @property (nonatomic, strong) UIImage *pointImage;  
  26. @property (nonatomic, strong) UIImage *selectedImage;  
  27.   
  28. @end  
  29.   
  30. @implementation ViewController  
  31.   
  32. - (void)dealloc  
  33. {  
  34.     [super dealloc];  
  35.     //  
  36.     [_imageView release];  
  37.     [_buttonArray release];  
  38.     [_selectedButtons release];  
  39.     [_pointImage release];  
  40.     [_selectedImage release];  
  41. }  
  42.   
  43. - (void)viewDidLoad  
  44. {  
  45.     [super viewDidLoad];  
  46.     // Do any additional setup after loading the view, typically from a nib.  
  47.       
  48.     _imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];  
  49.     [self.view addSubview:self.imageView];  
  50.     self.imageView.backgroundColor = [UIColor whiteColor];  
  51.       
  52.     [self createLockPoints];  
  53. }  
  54.   
  55. - (void)didReceiveMemoryWarning  
  56. {  
  57.     [super didReceiveMemoryWarning];  
  58.     // Dispose of any resources that can be recreated.  
  59. }  
  60.   
  61. #pragma mark - Trace Touch Point  
  62.   
  63. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  64. {  
  65.     UITouch *touch = [touches anyObject];  
  66.     if (touch) {  
  67.         for (UIButton *btn in self.buttonArray) {  
  68.             CGPoint touchPoint = [touch locationInView:btn];  
  69.             if ([btn pointInside:touchPoint withEvent:nil]) {  
  70.                 self.lineStartPoint = btn.center;  
  71.                 self.drawFlag = YES;  
  72.                   
  73.                 if (!self.selectedButtons) {  
  74.                     self.selectedButtons = [NSMutableArray arrayWithCapacity:9];  
  75.                 }  
  76.                   
  77.                 [self.selectedButtons addObject:btn];  
  78.                 [btn setImage:self.selectedImage forState:UIControlStateNormal];  
  79.             }  
  80.         }  
  81.     }  
  82. }  
  83.   
  84. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  85. {  
  86.     UITouch *touch = [touches anyObject];  
  87.     if (touch && self.drawFlag) {  
  88.         self.lineEndPoint = [touch locationInView:self.imageView];  
  89.           
  90.         for (UIButton *btn in self.buttonArray) {  
  91.             CGPoint touchPoint = [touch locationInView:btn];  
  92.               
  93.             if ([btn pointInside:touchPoint withEvent:nil]) {  
  94.                 BOOL btnContained = NO;  
  95.                   
  96.                 for (UIButton *selectedBtn in self.selectedButtons) {  
  97.                     if (btn == selectedBtn) {  
  98.                         btnContained = YES;  
  99.                         break;  
  100.                     }  
  101.                 }  
  102.                   
  103.                 if (!btnContained) {  
  104.                     [self.selectedButtons addObject:btn];  
  105.                     [btn setImage:self.selectedImage forState:UIControlStateNormal];  
  106.                 }  
  107.             }  
  108.         }  
  109.           
  110.         self.imageView.image = [self drawUnlockLine];  
  111.     }  
  112. }  
  113.   
  114. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  115. {  
  116.     [self outputSelectedButtons];  
  117.       
  118.     self.drawFlag = NO;  
  119.     self.imageView.image = nil;  
  120.     self.selectedButtons = nil;  
  121. }  
  122.   
  123. - (void)createLockPoints  
  124. {  
  125.     self.pointImage = [UIImage imageNamed:@"blue_circle"];  
  126.     self.selectedImage = [UIImage imageNamed:@"yellow_circle"];  
  127.       
  128.     float marginTop = 100;  
  129.     float marginLeft = 45;  
  130.       
  131.     float y;  
  132.     for (int i = 0; i < 3; ++i) {  
  133.         y = i * 100;  
  134.         float x;  
  135.         for (int j = 0; j < 3; ++j) {  
  136.             x = j * 100;  
  137.             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  138.             [btn setImage:self.pointImage forState:UIControlStateNormal];  
  139.             [btn setImage:self.selectedImage forState:UIControlStateHighlighted];  
  140.             btn.frame = (CGRect){x+marginLeft, y+marginTop, self.pointImage.size};  
  141.             [self.imageView addSubview:btn];  
  142.             btn.userInteractionEnabled = NO;  
  143.             btn.tag = LOCK_POINT_TAG + i * 3 + j;  
  144.               
  145.             if (!self.buttonArray) {  
  146.                 self.buttonArray = [NSMutableArray arrayWithCapacity:9];  
  147.             }  
  148.             [self.buttonArray addObject:btn];  
  149.         }  
  150.     }  
  151. }  
  152.   
  153. #pragma mark - Draw Line  
  154.   
  155. - (UIImage *)drawUnlockLine  
  156. {  
  157.     UIImage *image = nil;  
  158.       
  159.     UIColor *color = [UIColor yellowColor];  
  160.     CGFloat width = 5.0f;  
  161.     CGSize imageContextSize = self.imageView.frame.size;  
  162.       
  163.     UIGraphicsBeginImageContext(imageContextSize);  
  164.       
  165.     CGContextRef context = UIGraphicsGetCurrentContext();  
  166.       
  167.     CGContextSetLineWidth(context, width);  
  168.     CGContextSetStrokeColorWithColor(context, [color CGColor]);  
  169.       
  170.     CGContextMoveToPoint(context, self.lineStartPoint.x, self.lineStartPoint.y);  
  171.     for (UIButton *selectedBtn in self.selectedButtons) {  
  172.         CGPoint btnCenter = selectedBtn.center;  
  173.         CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y);  
  174.         CGContextMoveToPoint(context, btnCenter.x, btnCenter.y);  
  175.     }  
  176.     CGContextAddLineToPoint(context, self.lineEndPoint.x, self.lineEndPoint.y);  
  177.       
  178.     CGContextStrokePath(context);  
  179.       
  180.     image = UIGraphicsGetImageFromCurrentImageContext();  
  181.       
  182.     UIGraphicsEndImageContext();  
  183.       
  184.     return image;  
  185. }  
  186.   
  187. #pragma mark -   
  188.   
  189. - (void)outputSelectedButtons  
  190. {  
  191.     for (UIButton *btn in self.selectedButtons) {  
  192.         [btn setImage:self.pointImage forState:UIControlStateNormal];  
  193.         NSLog(@"Selected-button's tag : %d\n", btn.tag);  
  194.     }  
  195. }  
  196.   
  197. @end  

抱歉!评论已关闭.