码迷,mamicode.com
首页 > 其他好文 > 详细

手势解锁、锁屏Demo

时间:2015-11-30 11:36:33      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

先看效果图??

技术分享     技术分享

思路比较简单,话不多说,直接上代码。

控制器 ViewController.m 代码:

#import "ViewController.h"
#import "DDUnlockView.h"
@interface ViewController () <DDUnlockViewDelegate>
@property (strong, nonatomic) IBOutlet DDUnlockView *unlockView;

@end

@implementation ViewController

/**
 *  代理方法,设置正确密码,成功、失败在此处理
 */
- (BOOL)unlockView:(DDUnlockView *)unlockView withPassword:(NSString *)password
{
    // 横着数0~8对应各个按钮
    if ([password isEqualToString:@"2475"]) {
        UIViewController *nextVC = [[UIViewController alloc] init];
        nextVC.view.backgroundColor = [UIColor orangeColor];
        [self.navigationController pushViewController:nextVC animated:YES];
        return YES;
    } else {
        return NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置控制器 view 的背景颜色
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"HomeButtomBG"]];
    self.unlockView.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

 

DDUnlockView.h

#import <UIKit/UIKit.h>
@class DDUnlockView;
@protocol DDUnlockViewDelegate <NSObject>

- (BOOL)unlockView:(DDUnlockView *)unlockView withPassword:(NSString *)password;

@end

@interface DDUnlockView : UIView
@property (nonatomic, weak) id<DDUnlockViewDelegate> delegate;
@end

DDUnlockView.m

#import "DDUnlockView.h"
#define DDLineColor [UIColor colorWithRed:0.0 green:170/255.0 blue:255/255.0 alpha:1.0]

@interface DDUnlockView ()
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, strong) NSMutableArray *selectedButtons;
@property (nonatomic, strong) UIColor *lineColor;
@property (nonatomic, assign) CGPoint currentPoint;
@end


@implementation DDUnlockView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    if (self.selectedButtons.count == 0) {
        return;
    }
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    for (int i = 0; i < self.selectedButtons.count; i++) {
        
        UIButton *button = self.selectedButtons[i];
        if (i == 0) {
            [path moveToPoint:button.center];
        } else {
            [path addLineToPoint:button.center];
        }
    }
    
    // 添加与手指之间的连线
    [path addLineToPoint:self.currentPoint];
    path.lineWidth = 1;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    
    // 设置线颜色
    [self.lineColor set];
    
    [path stroke];
}

/**
 *  该方法被调用的时候,说明子控件已经创建好了。
 *  一般在该方法里面设置frame
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 布局子控件
    CGFloat w = 74;
    CGFloat h = 74;
    int columns = 3;
    CGFloat marginX = (self.bounds.size.width - (columns * w)) / (columns + 1);
    CGFloat marginY = (self.bounds.size.height - (columns * h)) / (columns + 1);
    
    // 在此处第一次执行了懒加载,加载button
    for (int i = 0; i < self.buttons.count; i++) {
        int row = i / columns;
        int col = i % columns;
        CGFloat x = marginX + col * (w + marginX);
        CGFloat y = marginY + row * (h + marginY);
        UIButton *button = self.buttons[i];
        button.frame = CGRectMake(x, y, w, h);
        
    }
    
}

// 获取某个 touch 的 point
- (CGPoint)pointWithTouches:(NSSet *)touches
{
    UITouch *touch = touches.anyObject;
    return [touch locationInView:touch.view];
}

// 根据触摸点找按钮
- (UIButton *)buttonWithPoint:(CGPoint)point
{
    for (UIButton *button in self.buttons) {
        //        // 判断点是否在按钮的矩形区域之内
        //        if (CGRectContainsPoint(button.frame, point) && !button.selected) {
        //            return button;
        //        }
        CGPoint centerP = button.center;
        CGFloat radius = MIN(button.frame.size.width, button.frame.size.height) * 0.4;
        
        BOOL xOk = (point.x >= centerP.x - radius) && (point.x <= centerP.x + radius);
        BOOL yOk = (point.y >= centerP.y - radius) && (point.y <= centerP.y + radius);
        if (xOk && yOk && !button.selected) {
            return button;
        }
    }
    return nil;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 获取每个触摸的触摸点
    CGPoint point = [self pointWithTouches:touches];
    
    // 找到这个触摸点所在的按钮
    UIButton *button = [self buttonWithPoint:point];
    
    if (button) {
        // 设置这个按钮为选中状态
        button.selected = YES;
        
        // 添加到 self.selectedButtons 中
        [self.selectedButtons addObject:button];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 获取每个触摸的触摸点
    CGPoint point = [self pointWithTouches:touches];
    
    // 找到这个触摸点所在的按钮
    UIButton *button = [self buttonWithPoint:point];
    
    if (button) {
        // 设置这个按钮为选中状态
        button.selected = YES;
        
        // 添加到 self.selectedButtons 中
        [self.selectedButtons addObject:button];
    }
    // 记录当前点
    self.currentPoint = point;
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    self.currentPoint = [[self.selectedButtons lastObject] center];
    
    NSMutableString *strPassword = [NSMutableString string];
    for (int i = 0; i < self.selectedButtons.count; i++) {
        [strPassword appendFormat:@"%@", @([self.selectedButtons[i] tag])];
        //[strPassword appendFormat:@"%d", [self.selectedButtons[i] tag]];
    }
    
    if ([self.delegate respondsToSelector:@selector(unlockView:withPassword:)]) {
        BOOL ok = [self.delegate unlockView:self withPassword:strPassword];
        if (ok) {
            // 清除 view
            [self clearView];
        } else {
            // 失败
            self.lineColor = [UIColor redColor];
            for (UIButton *button in self.selectedButtons) {
                button.selected = NO;
                button.enabled = NO;
            }
            // 重绘
            [self setNeedsDisplay];
            
            // 等待0.3秒之后, 恢复到默认状态
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.lineColor = DDLineColor;
                // 清除 view
                [self clearView];
            });
        }
    } else {
    
        // 清除 view
        [self clearView];
    }
}


- (void)clearView
{
    // 设置所有按钮的 selected 为 NO
    for (UIButton *button in self.selectedButtons) {
        button.selected = NO;
        button.enabled = YES;
    }
    // 清空 self.selectedButtons 集合
    [self.selectedButtons removeAllObjects];
    
    // 重新绘制
    [self setNeedsDisplay];
}

#pragma mark - 懒加载
- (UIColor *)lineColor
{
    if (_lineColor == nil) {
        _lineColor = DDLineColor;
    }
    return _lineColor;
}

- (NSMutableArray *)selectedButtons
{
    if (_selectedButtons == nil) {
        _selectedButtons = [NSMutableArray array];
    }
    return _selectedButtons;
}

- (NSArray *)buttons
{
    if (_buttons == nil) {
        
        NSMutableArray *arrayM = [NSMutableArray array];
        
        for (int i = 0; i < 9; i++) {
            UIButton *btn = [[UIButton alloc] init];
            
            [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"] forState:UIControlStateDisabled];
            
            // 设置按钮禁止与用户交互
            btn.userInteractionEnabled = NO;
            btn.tag = i;
            
            [arrayM addObject:btn];
            [self addSubview:btn];
        }
        _buttons = arrayM;
    }
    return _buttons;
}


@end

 暂时没图片,晚上回家上传代码到GitHub

手势解锁、锁屏Demo

标签:

原文地址:http://www.cnblogs.com/alwaysDB/p/5006655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!