//
// MJLockView.m
// 03-手勢解鎖
//
// Created by apple on 14-4-20.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJLockView.h"
@implementation MJLockView
//當你通過代碼創建控件就會調用這個方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
//當你通過storyboared或者xib中創建控件就會調用這個方法
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setup];
}
return self;
}
/**
初始化
*/
- (void)setup
{
for (int index = 0; index<9; index++) {
// 創建按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.userInteractionEnabled = NO;
btn.backgroundColor = [UIColor blueColor];
// 設置默認的背景圖片
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
// 設置選中時的背景圖片(selected)
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
// 添加按鈕
[self addSubview:btn];
}
}
// 調整按鈕的frame最好在這個方法里面
- (void)layoutSubviews
{
[super layoutSubviews];
for (int index = 0; index<self.subviews.count; index++) {
// 取出按鈕
UIButton *btn = self.subviews[index];
// 設置frame
CGFloat btnW = 74;
CGFloat btnH = 74;
int totalColumns = 3;
int col = index % totalColumns;
int row = index / totalColumns;
CGFloat marginX = (self.frame.size.width - totalColumns * btnW) / (totalColumns + 1);
CGFloat marginY = marginX;
CGFloat btnX = marginX + col * (btnW + marginX);
CGFloat btnY = row * (btnH + marginY);
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pos = [touch locationInView:touch.view];
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, pos)) {
btn.selected = YES;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pos = [touch locationInView:touch.view];
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, pos)) {
btn.selected = YES;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
- (void)drawRect:(CGRect)rect
{
}
@end