LockView.h
//
// LockView.h
// ProtectedData
//
// Created by on 7/6/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "LockButton.h"
@protocol LockViewDelegate <NSObject>
- (void)beforeUnlocking;
- (void)beganUnlock;
- (void)unlocking;
- (void)afterUnlock;
@end
@interface LockView : UIView
@property (nonatomic, retain, readonly) NSMutableArray *lastSelectedButtonsIndexArray;
+ (LockView *)instanceWithFrame:(CGRect)frame WithDelegate:(id <LockViewDelegate>)delegate;
// 重置Button,将buttons的image设置为normal
- (void)resetButtons;
@end
LockView.m
//
// LockView.m
// ProtectedData
//
// Created by on 7/6/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "LockView.h"
#ifndef BUTTONCOUNTPERLINE
#define BUTTONCOUNTPERLINE 4
#endif
#ifndef BUTTONROWCOUNT
#define BUTTONROWCOUNT 4
#endif
@interface LockView ()
@property (weak) id <LockViewDelegate> delegate;
@property (nonatomic, retain) NSMutableArray *buttons;
// 所有按键的中心坐标
@property (nonatomic, retain) NSMutableArray *buttonsCenterPointArray;
// 用户接触的Button的index队列
@property (nonatomic, retain) NSMutableArray *lastSelectedButtonsIndexArray;
// 计算buttons的中心坐标
- (void)calculateButtonsCenterPoint;
// 初始化Buttons
- (void)initButtons;
// 根据frame和delegate初始化
- (id)initWithFrame:(CGRect)frame WithDelegate:(id <LockViewDelegate>)delegate;
// 重置Button,将buttons的image设置为normal
- (void)resetButtons;
// 接收用户事件结束
- (void)touchEnd;
@end
@implementation LockView
@synthesize delegate;
@synthesize buttons;
@synthesize buttonsCenterPointArray;
@synthesize lastSelectedButtonsIndexArray;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// 计算buttons的中心坐标
- (void)calculateButtonsCenterPoint
{
// 设置坐标的基本信息
CGPoint firstButtoncenterPoint = CGPointMake(55.0, 120.0);
for (int i = 0; i < BUTTONROWCOUNT; i++) {
for (int j = 0; j < BUTTONCOUNTPERLINE; j++) {
CGPoint buttonCenterPoint = CGPointMake(firstButtoncenterPoint.x + j * BUTTONFRAMEWIDTH, firstButtoncenterPoint.y + i * BUTTONFRAMEHEIGHT);
[self.buttonsCenterPointArray addObject:[NSValue valueWithCGPoint:buttonCenterPoint]];
}
}
}
// 初始化Button
- (void)initButtons
{
NSUInteger index = 0;
for (NSValue *pointValue in self.buttonsCenterPointArray) {
LockButton *button = [LockButton instanceWithCenterPoint:[pointValue CGPointValue] AtIndex:index++];
[self addSubview:button];
[self.buttons addObject:button];
}
}
// 重置Button,将buttons的image设置为normal
- (void)resetButtons
{
[self.lastSelectedButtonsIndexArray removeAllObjects];
for (LockButton *button in self.buttons) {
[button resetButton];
}
[self setUserInteractionEnabled:YES];
SAFE_PERFORM_WITH_ARG(self.delegate, @selector(beforeUnlocking), nil);
}
// 接收用户事件结束
- (void)touchEnd
{
if (self.lastSelectedButtonsIndexArray.count > 0) {
SAFE_PERFORM_WITH_ARG(self.delegate, @selector(afterUnlock), nil);
}
}
// 根据frame和delegate初始化
- (id)initWithFrame:(CGRect)frame WithDelegate:(id <LockViewDelegate>)delegate
{
self = [self initWithFrame:frame];
self.delegate = delegate;
self.backgroundColor = RGBA(131.0f, 164.0f, 199.0f, 1.0f);
self.buttonsCenterPointArray = [[NSMutableArray alloc] init];
self.buttons = [[NSMutableArray alloc] init];
self.lastSelectedButtonsIndexArray = [[NSMutableArray alloc] init];
[self setUserInteractionEnabled:YES];
return self;
}
+ (LockView *)instanceWithFrame:(CGRect)frame WithDelegate:(id <LockViewDelegate>)delegate
{
LockView *lockView = [[LockView alloc] initWithFrame:frame WithDelegate:delegate];
return lockView;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[self calculateButtonsCenterPoint];
[self initButtons];
}
#pragma mark - TouchDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];
[self resetButtons];
for (LockButton *button in self.buttons) {
if (CGRectContainsPoint(button.frameForTouch, touchPoint)) {
[button activeButton];
[self.lastSelectedButtonsIndexArray addObject:[NSNumber numberWithInteger:button.index]];
SAFE_PERFORM_WITH_ARG(self.delegate, @selector(beganUnlock), nil);
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];
for (LockButton *button in self.buttons) {
if (CGRectContainsPoint(button.frameForTouch, touchPoint)) {
if (self.lastSelectedButtonsIndexArray.count > 0 && button.isActived && ![[self.lastSelectedButtonsIndexArray lastObject] isEqual:[NSNumber numberWithInteger:button.index]]) {
[self touchEnd];
} else if (!button.isActived) {
[button activeButton];
[self.lastSelectedButtonsIndexArray addObject:[NSNumber numberWithInteger:button.index]];
SAFE_PERFORM_WITH_ARG(self.delegate, @selector(unlocking), nil);
}
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//UITouch *touch = [touches anyObject];
//CGPoint touchPoint = [touch locationInView:touch.view];
[self touchEnd];
}
@end