http://code4app.com/ios/Pattern-Lock-App/4f9795e506f6e7f068000000

解决方案 »

  1.   

    分享一下code,写的不是很优美,不过能用 @interface SettingViewController () {
    }
    @property (strong) UIImageView *backView;
    @property (strong) NSMutableArray *pointArr;
    @property (strong) NSMutableArray *keyArr;
    @property (strong) NSString *rightKeyString;
    @end@implementation SettingViewController- (id)init
    {
        self = [super init];
        if (self) {
    _pointArr = [[NSMutableArray alloc]initWithCapacity:9];
    _keyArr = [[NSMutableArray alloc]initWithCapacity:9];
    _rightKeyString = @"14678";
        }
        return self;
    }-(BOOL)shouldAutomaticallyForwardRotationMethods
    {
        return NO;
    }- (void)loadView
    {
        [super loadView];
        CGRect bounds = [[UIScreen mainScreen] applicationFrame];
        bounds = bounds;

        //View
        //self.view = [[UIView alloc] initWithFrame:bounds];
        self.view.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1];
        
    //back
    _backView=[[UIImageView alloc] initWithFrame:self.view.frame];
        [self.view addSubview:_backView]; //other
    float begin_x = bounds.size.width/4;
    float begin_y = bounds.size.height/4;
    float inteval = bounds.size.width/4;

    float point_radius = bounds.size.width/16;


    for(int iY=0;iY<3;iY++)
    {
    for(int iX=0;iX<3;iX++)
    {
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,point_radius*2, point_radius*2)];
    [img setCenter:CGPointMake(begin_x+inteval*iX , begin_y+inteval*iY)];
    [_pointArr addObject:img];
    [self.view addSubview:img];
    }
    }
    }-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    NSLog(@"touches Began");
    [_keyArr removeAllObjects];
    //CGPoint startPoint = [self getCurrentPoint:touches];
    }-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {

    CGPoint movePoint = [self getCurrentPoint:touches];
    NSUInteger indexTouched = [self touchPoint2Index:movePoint];

    if(indexTouched!=-1)
    {
    [self addtoKeyArr:indexTouched];
    }

    //DrawLines and change png
    [self drawUI];

    }-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    if(_keyArr.count<1)
    return;

    CGPoint endPoint = [self getCurrentPoint:touches];
    NSLog(@"x:%f ;  y:%f",endPoint.x,endPoint.y);

    NSString *strKeyInput = @"";
    for(int i=0;i<_keyArr.count;i++)
    {
    strKeyInput = [strKeyInput stringByAppendingString:[_keyArr objectAtIndex:i]];
    }
    if([strKeyInput isEqual:_rightKeyString])
    {
    [GeneralFunction.inst AlertTemporayString:@"Success" tick:0.5f];
    }
    else
    {
    [GeneralFunction.inst AlertString:@"Fail,Please Retry"];
    }
    [self ClearKeyScreen];}-(CGPoint)getCurrentPoint:(NSSet*)touches
    {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];
    return point;
    }-(void)addtoKeyArr:(NSUInteger)val
    {
    NSString *str = [NSString stringWithFormat:@"%d",val];
    if(![_keyArr containsObject:str])
    {
    [_keyArr addObject:str];
    }

    //test
    NSString *strKey = @"";
    for(int i=0;i<_keyArr.count;i++)
    {
    strKey = [strKey stringByAppendingString:[_keyArr objectAtIndex:i]];
    }
    NSLog(@"%@",strKey);
    }-(NSUInteger)touchPoint2Index:(CGPoint)movePoint
    {
    for(int i=0;i<_pointArr.count;i++)
    {

    UIImageView *img = [_pointArr objectAtIndex:i];
    CGRect r = img.frame;
    if(r.origin.x < movePoint.x &&
       r.origin.y < movePoint.y &&
       r.size.width+r.origin.x > movePoint.x &&
       r.size.height+r.origin.y > movePoint.y
       )
    {
    return i;
    }
    }
    return -1;
    }-(void)drawUI
    {
    //change png
    for(int i=0;i<_keyArr.count;i++)
    {
    NSString *strKey = [_keyArr objectAtIndex:i];
    int iKey = strKey.intValue;

    UIImageView *imgView = [_pointArr objectAtIndex:iKey];
    [imgView setImage:[UIImage imageNamed:@"point_2.png"]];
    }

    //draw lines
    if(_keyArr.count>1)
    {
    UIGraphicsBeginImageContext(_backView.frame.size);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0);  //线宽
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0);  //颜色
    CGContextBeginPath(UIGraphicsGetCurrentContext());// CGContextClearRect(UIGraphicsGetCurrentContext(), _backView.frame); for(int i=0;i<_keyArr.count-1;i++)
    {
    NSString *strKey1 = [_keyArr objectAtIndex:i];
    int iKey1 = strKey1.intValue;
    UIImageView *imgView1 = [_pointArr objectAtIndex:iKey1];
    CGFloat x1 = imgView1.center.x;
    CGFloat y1 = imgView1.center.y;

    NSString *strKey2 = [_keyArr objectAtIndex:i+1];
    int iKey2 = strKey2.intValue;
    UIImageView *imgView2 = [_pointArr objectAtIndex:iKey2];
    CGFloat x2 = imgView2.center.x;
    CGFloat y2 = imgView2.center.y;

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), x1, y1);  //起点坐标
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), x2, y2);   //终点坐标
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    }
    _backView.image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    }
    }-(void)ClearKeyScreen
    {
    //Reset Point
    for(NSUInteger i=0;i<_pointArr.count;i++)
    {
    UIImageView *imgEle = [_pointArr objectAtIndex:i];
    [imgEle setImage:[UIImage imageNamed:@"point_1.png"]];
    }


    //Clear Screen
    UIGraphicsBeginImageContext(_backView.frame.size);
    CGContextClearRect(UIGraphicsGetCurrentContext(), _backView.frame);
    _backView.image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    }
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }- (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }- (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];

    [self ClearKeyScreen];

    }
    @end