bc... [密码]
Home » Source Code » » LLLockView.m

LLLockView.m ( File view )

From:
Description:...
  • By lzwandnju 2017-02-09
  • View(s):0
  • Download(s):0
  • Point(s): 1
			//
//  LLLockView.m
//  LockSample
//
//  Created by Lugede on 14/11/11.
//  Copyright (c) 2014年 lugede.cn. All rights reserved.
//

#import "LLLockView.h"
#import "LLLockViewController.h"

#define kLLBaseCircleNumber 10000       // tag基数(请勿修改)
#define kCircleMargin 32.0              // 圆点离屏幕左边距
#define kCircleDiameter 68.0            // 圆点直径
#define kLLCircleAlpha 1.0              // 圆点透明度
#define kLLLineWidth 4.0               // 线条宽
#define kLLLineColor [UIColor colorWithRed:77.0/255.0 green:76.0/255.0 blue:156.0/255.0 alpha:0.8] // 线条色蓝
#define kLLLineColorWrong [UIColor colorWithRed:201.0/255.0 green:9.0/255.0 blue:22.0/255.0 alpha:0.8] // 线条色红

@interface LLLockView ()
{

    NSMutableArray* buttonArray;
    NSMutableArray* selectedButtonArray;
    NSMutableArray* wrongButtonArray;
    CGPoint nowPoint;
    
    NSTimer* timer;
    
    BOOL isWrongColor;
    BOOL isDrawing; // 正在画中

}

@end

@implementation LLLockView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {

 // Drawing code
 
}
 */

- (id)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {

        
        self.clipsToBounds = YES;
        [self initCircles];
    
}
    return self;

}

- (id)initWithCoder:(NSCoder *)aDecoder
{

    self = [super initWithCoder:aDecoder];
    if (self) {

        self.clipsToBounds = YES;
        [self initCircles];
    
}
    return self;

}

- (void)initCircles
{

    buttonArray = [NSMutableArray array];
    selectedButtonArray = [NSMutableArray array];
    
    // 初始化圆点
    for (int i = 0; i < 9; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        int x = kCircleMargin + (i%3) * (kCircleDiameter+(320-kCircleMargin*2- kCircleDiameter *3)/2);
        int y = kCircleMargin + (i/3) * (kCircleDiameter+(320-kCircleMargin*2- kCircleDiameter *3)/2);
//        LLLog(@"每个圆点位置 %d,%d", x, y);
        [button setFrame: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 = kLLCircleAlpha;
        button.tag = i + kLLBaseCircleNumber + 1; // tag从基数+1开始,
        [self addSubview:button];
        [buttonArray addObject:button];
    
}
    
    self.backgroundColor = [UIColor clearColor];

}

#pragma mark - 事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    LLLog(@"touch began");
    isDrawing = NO;
    // 如果是错误色才重置(timer重置过了)
    if (isWrongColor) {

        [self clearColorAndSelectedButton];
    
}
    CGPoint point = [[touches anyObject] locationInView:self];
    [self updateFingerPosition:point];
    

}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    isDrawing = YES;
    
    CGPoint point = [[touches anyObject] locationInView:self];
    [self updateFingerPosition:point];

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    LLLog(@"输入密码结束");
    [self endPosition];

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{

    LLLog(@"输入密码取消");
    [self endPosition];

}


#pragma mark - 绘制连线
- (void)drawRect:(CGRect)rect
{

    LLLog(@"drawRect - %@", [NSString stringWithFormat:@"%d", isWrongColor]);
    
    if (selectedButtonArray.count > 0) {

        CGContextRef context = UIGraphicsGetCurrentContext();
        
        isWrongColor ? [kLLLineColorWrong set] : [kLLLineColor set]; // 正误线条色
        CGContextSetLineWidth(context, kLLLineWidth);

        // 画之前线s
        CGPoint addLines[9];
        int count = 0;
        for (UIButton* button in selectedButtonArray) {

            CGPoint point = CGPointMake(button.center.x, button.center.y);
            addLines[count++] = point;
            
            // 画中心圆
            CGRect circleRect = CGRectMake(button.center.x- kLLLineWidth/2,
                                           button.center.y - kLLLineWidth/2,
                                           kLLLineWidth,
                                           kLLLineWidth);
            CGContextSetFillColorWithColor(context, kLLLineColor.CGColor);
            CGContextFillEllipseInRect(context, circleRect);
        
}
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextAddLines(context, addLines, count);
        CGContextStrokePath(context);
         //*/
        
        // 画当前线
        UIButton* lastButton = selectedButtonArray.lastObject;
        CGContextMoveToPoint(context, lastButton.center.x, lastButton.center.y);
        CGContextAddLineToPoint(context, nowPoint.x, nowPoint.y);
        CGContextStrokePath(context);
    
}
    

}

#pragma mark - 处理
// 当前手指位置
- (void)updateFingerPosition:(CGPoint)point{

    
    nowPoint = point;
    
//    LLLog(@"point x=%f, y=%f", point.x, point.y);
    
    for (UIButton *thisbutton in buttonArray) {

        CGFloat xdiff =point.x-thisbutton.center.x;
        CGFloat ydiff=point.y - thisbutton.center.y;
        
        if (fabsf(xdiff) <36 &&fabsf (ydiff) <36){

            
//            LLLog(@"now point is %d th",thisbutton.tag-kLLBaseCircleNumber);
            //             LLLog(@"%@", [resulttext.text stringByAppendingString:resulttext.text]);
            
            // 未选中的才能加入
            if (!thisbutton.selected) {

       
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
.gitignore495.00 B2015-08-21|08:09
01.97 kB
01.97 kB
entries419.00 B2015-08-21|08:09
01.97 kB
project.pbxproj.svn-base20.23 kB2015-08-21|08:09
project.pbxproj20.42 kB2015-08-21|08:09
01.97 kB
01.97 kB
entries420.00 B2015-08-21|08:09
01.97 kB
contents.xcworkspacedata.svn-base155.00 B2015-08-21|08:09
contents.xcworkspacedata155.00 B2015-08-21|08:09
01.97 kB
AppDelegate.h470.00 B2015-08-21|08:09
AppDelegate.m2.93 kB2015-08-21|08:09
01.97 kB
LaunchScreen.xib3.65 kB2015-08-21|08:09
Main.storyboard10.38 kB2015-08-21|08:09
FirstViewController.h258.00 B2015-08-21|08:09
FirstViewController.m808.00 B2015-08-21|08:09
01.97 kB
01.97 kB
Contents.json585.00 B2015-08-21|08:09
01.97 kB
01.97 kB
Contents.json165.00 B2015-08-21|08:09
circle_indecator.pdf17.13 kB2015-08-21|08:09
01.97 kB
Contents.json162.00 B2015-08-21|08:09
circle_normal.pdf44.45 kB2015-08-21|08:09
01.97 kB
Contents.json164.00 B2015-08-21|08:09
circle_selected.pdf45.65 kB2015-08-21|08:09
01.97 kB
Contents.json161.00 B2015-08-21|08:09
circle_wrong.pdf22.26 kB2015-08-21|08:09
01.97 kB
Contents.json3.02 kB2015-08-21|08:09
01.97 kB
Contents.json154.00 B2015-08-21|08:09
first.pdf2.41 kB2015-08-21|08:09
01.97 kB
Contents.json155.00 B2015-08-21|08:09
second.pdf2.37 kB2015-08-21|08:09
Info.plist1.39 kB2015-08-21|08:09
01.97 kB
LLLockConfig.h297.00 B2015-08-21|08:09
LLLockIndicator.h301.00 B2015-08-21|08:09
LLLockIndicator.m2.54 kB2015-08-21|08:09
LLLockPassword.h433.00 B2015-08-21|08:09
LLLockPassword.m1.27 kB2015-08-21|08:09
LLLockView.h530.00 B2015-08-21|08:09
LLLockView.m8.56 kB2015-08-21|08:09
LLLockViewController.h884.00 B2015-08-21|08:09
LLLockViewController.m13.94 kB2015-08-21|08:09
LLLockViewController.xib7.58 kB2015-08-21|08:09
SecondViewController.h300.00 B2015-08-21|08:09
SecondViewController.m2.25 kB2015-08-21|08:09
main.m335.00 B2015-08-21|08:09
01.97 kB
Info.plist748.00 B2015-08-21|08:09
LockSampleTests.m869.00 B2015-08-21|08:09
...

LLLockView.m (87.24 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D