话不多说了,直接上代码:
是不是很好用呢!
.h文件
//
// GestureCodeViewController.h
// DailyRecord
//
// Created by sven on 13-6-20.
// Copyright (c) 2013年 sven. All rights reserved.
//
#import <UIKit/UIKit.h>
#define LOCK_POINT_TAG 1000
@interface GestureCodeViewController : UIViewController
{
UIImageView *m_imageView;
CGPoint m_lineStartPoint;
CGPoint m_lineEndPoint;
NSMutableArray *m_mutArrButtons;
NSMutableArray *m_mutArrSelectedButtons;
BOOL m_drawFlag;
UIImage *m_pointImage;
UIImage *m_selectedImage;
}
@property (retain, nonatomic) UIImageView *m_imageView;
@property (assign, nonatomic) CGPoint m_lineStartPoint;
@property (assign, nonatomic) CGPoint m_lineEndPoint;
@property (retain, nonatomic) NSMutableArray *m_mutArrButtons;
@property (retain, nonatomic) NSMutableArray *m_mutArrSelectedButtons;
@property (assign, nonatomic) BOOL m_drawFlag;
@property (retain, nonatomic) UIImage *m_pointImage;
@property (retain, nonatomic) UIImage *m_selectedImage;
@end
.m文件
//
// GestureCodeViewController.m
// DailyRecord
//
// Created by sven on 13-6-20.
// Copyright (c) 2013年 sven. All rights reserved.
//
#import "GestureCodeViewController.h"
@interface GestureCodeViewController ()
@end
@implementation GestureCodeViewController
@synthesize m_imageView;
@synthesize m_lineStartPoint;
@synthesize m_lineEndPoint;
@synthesize m_drawFlag;
@synthesize m_mutArrButtons;
@synthesize m_mutArrSelectedButtons;
@synthesize m_pointImage;
@synthesize m_selectedImage;
#pragma mark -life cycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.backgroundColor = [UIColor whiteColor];
self.m_imageView = imageView;
[imageView release];
[self.view addSubview:m_imageView];
[self createLockPoints];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[m_imageView release], m_imageView = nil;
[m_mutArrButtons release], m_mutArrButtons = nil;
[m_mutArrSelectedButtons release], m_mutArrSelectedButtons = nil;
[m_pointImage release], m_pointImage = nil;
[m_selectedImage release], m_selectedImage = nil;
[super dealloc];
}
#pragma mark - Trace Touch Point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch)
{
for (UIButton *btn in self.m_mutArrButtons)
{
CGPoint touchPoint = [touch locationInView:btn];
if ([btn pointInside:touchPoint withEvent:nil])
{
self.m_lineStartPoint = btn.center;
self.m_drawFlag = YES;
if (!self.m_mutArrSelectedButtons)
{
self.m_mutArrSelectedButtons = [NSMutableArray arrayWithCapacity:9];
}
[self.m_mutArrSelectedButtons addObject:btn];
[btn setImage:self.m_selectedImage forState:UIControlStateNormal];
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch && self.m_drawFlag)
{
self.m_lineEndPoint = [touch locationInView:self.m_imageView];
for (UIButton *btn in self.m_mutArrButtons)
{
CGPoint touchPoint = [touch locationInView:btn];
if ([btn pointInside:touchPoint withEvent:nil])
{
BOOL btnContained = NO;
for (UIButton *selectedBtn in self.m_mutArrSelectedButtons)
{
if (btn == selectedBtn)
{
btnContained = YES;
break;
}
}
if (!btnContained)
{
[self.m_mutArrSelectedButtons addObject:btn];
[btn setImage:self.m_selectedImage forState:UIControlStateNormal];
}
}
}
self.m_imageView.image = [self drawUnlockLine];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self outputSelectedButtons];
self.m_drawFlag = NO;
self.m_imageView.image = nil;
self.m_mutArrSelectedButtons = nil;
}
- (void)createLockPoints
{
self.m_pointImage = [UIImage imageNamed:@"blue_circle"];
self.m_selectedImage = [UIImage imageNamed:@"yellow_circle"];
float marginTop = 150;
float marginLeft = 40;
float y;
for (int i = 0; i < 3; ++i)
{
y = i * 100;
float x;
for (int j = 0; j < 3; ++j)
{
x = j * 100;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:self.m_pointImage forState:UIControlStateNormal];
[btn setImage:self.m_selectedImage forState:UIControlStateHighlighted];
btn.frame = (CGRect){x+marginLeft, y+marginTop, self.m_pointImage.size};
[self.m_imageView addSubview:btn];
btn.userInteractionEnabled = NO;
btn.tag = LOCK_POINT_TAG + i * 3 + j;
if (!self.m_mutArrButtons) {
self.m_mutArrButtons = [NSMutableArray arrayWithCapacity:9];
}
[self.m_mutArrButtons addObject:btn];
}
}
}
#pragma mark - Draw Line
- (UIImage *)drawUnlockLine
{
UIImage *image = nil;
UIColor *color = [UIColor yellowColor];
CGFloat width = 5.0f;
CGSize imageContextSize = self.m_imageView.frame.size;
UIGraphicsBeginImageContext(imageContextSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, width);
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextMoveToPoint(context, self.m_lineStartPoint.x, self.m_lineStartPoint.y);
for (UIButton *selectedBtn in self.m_mutArrSelectedButtons)
{
CGPoint btnCenter = selectedBtn.center;
CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y);
CGContextMoveToPoint(context, btnCenter.x, btnCenter.y);
}
CGContextAddLineToPoint(context, self.m_lineEndPoint.x, self.m_lineEndPoint.y);
CGContextStrokePath(context);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#pragma mark -
- (void)outputSelectedButtons
{
for (UIButton *btn in self.m_mutArrSelectedButtons)
{
[btn setImage:self.m_pointImage forState:UIControlStateNormal];
NSLog(@"Selected-button's tag : %d\n", btn.tag);
}
}
@end
是不是很好用呢!