LockView.h/LockView.m

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

 

转载于:https://my.oschina.net/yongbin45/blog/69558

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值