[iOS UI进阶 - 5.0] 手势解锁Demo

- 使用按钮来处理每个圆点
- 使用代码生成按钮
- 取消按钮点击事件
- 设置普通状态和选中状态的背景图片
- CGRectContainsPoint,移动到按钮范围内改变按钮为选中状态
- 按钮的连接:使用数组存储被选中的所有按钮,画上连线
- 已经连线的按钮不需要再连线
- 触摸结束清空连线和按钮选中状态
- 移动中也要画出线,最后的点用来辅助画移动中的线
- 解决bug:每次触摸开始重置当前画笔位置
- 设置触摸触发选中的按钮内部范围
- 使用tag记录按钮的选中顺序轨迹,触摸结束取得轨迹
- 封装整个手势解锁view成为一个自定义控件
- 封装按钮称为自定类

//
// ViewController.m
// HVWLockView
//
// Created by hellovoidworld on 15/1/12.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 设置背景
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_refresh_bg"]];
} /** 设置状态栏样式 */
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//
// HVWLockView.m
// HVWLockView
//
// Created by hellovoidworld on 15/1/12.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLockView.h"
#import "HVWLockButton.h" #define BUTTON_COUNT 9
#define BUTTON_COL_COUNT 3 @implementation HVWLockView #pragma mark - 初始化方法
/** 使用文件初始化 */
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self initView];
}
return self;
} /** 使用代码初始化 */
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initView];
}
return self;
} /** 初始化view内的控件(按钮) */
- (void) initView {
for (int i=; i<BUTTON_COUNT; i++) {
HVWLockButton *button = [HVWLockButton buttonWithType:UIButtonTypeCustom]; // 取消点击时间
button.userInteractionEnabled = NO; // 设置指标tag,用来记录轨迹
button.tag = i; // 设置普通状态图片
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal]; // 设置选中状态图片
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected]; // 加入按钮到lock view
[self addSubview:button];
}
} /** 设置按钮位置尺寸 */
- (void)layoutSubviews {
[super layoutSubviews]; // 取出所有按钮
for (int i=; i<self.subviews.count; i++) {
HVWLockButton *button = self.subviews[i];
CGFloat buttonWidth = ;
CGFloat buttonHeight = ; // 此按钮所在列号
int col = i % BUTTON_COL_COUNT;
// 此按钮所在行号
int row = i / BUTTON_COL_COUNT;
// 等分水平多余空间,计算出间隙
CGFloat marginX = (self.frame.size.width - BUTTON_COL_COUNT * buttonWidth) / (BUTTON_COL_COUNT + );
CGFloat marginY = marginX; // x坐标
CGFloat buttonX = marginX + col * (buttonWidth + marginX);
// y坐标
CGFloat buttonY = marginY + row * (buttonHeight + marginY); button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
}
} @end

#pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view]; // 检测哪个按钮被点中了
for (HVWLockButton *button in self.subviews) {
if (CGRectContainsPoint(button.frame, touchLocation)) {
button.selected = YES;
}
} } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view]; // 检测哪个按钮被点中了
for (HVWLockButton *button in self.subviews) {
if (CGRectContainsPoint(button.frame, touchLocation)) {
button.selected = YES;
}
}
} - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 消除所有按钮选中状态
for (HVWLockButton *button in self.subviews) {
button.selected = NO;
}
}

//
// HVWLockButton.m
// HVWLockView
//
// Created by hellovoidworld on 15/1/12.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLockButton.h" @implementation HVWLockButton /** 使用文件创建会调用 */
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self initLockButton];
}
return self;
} /** 使用代码创建会调用 */
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initLockButton];
}
return self;
} /** 初始化 */
- (void) initLockButton {
// 取消交互事件(点击)
self.userInteractionEnabled = NO; // 设置普通状态图片
[self setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal]; // 设置选中状态图片
[self setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
} @end
//
// HVWLockView.m
// HVWLockView
//
// Created by hellovoidworld on 15/1/12.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWLockView.h"
#import "HVWLockButton.h" #define BUTTON_COUNT 9
#define BUTTON_COL_COUNT 3 @interface HVWLockView() /** 已选择按钮数组 */
@property(nonatomic, strong) NSMutableArray *selectedButtons; /** 触摸位置 */
@property(nonatomic, assign) CGPoint currentTouchLocation; @end @implementation HVWLockView /** 初始化数组 */
- (NSMutableArray *)selectedButtons {
if (nil == _selectedButtons) {
_selectedButtons = [NSMutableArray array];
}
return _selectedButtons;
} #pragma mark - 初始化方法
/** 使用文件初始化 */
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self initView];
}
return self;
} /** 使用代码初始化 */
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initView];
}
return self;
} /** 初始化view内的控件(按钮) */
- (void) initView {
// 设置透明背景
self.backgroundColor = [[UIColor alloc] initWithRed: green: blue: alpha:]; for (int i=; i<BUTTON_COUNT; i++) {
HVWLockButton *button = [HVWLockButton buttonWithType:UIButtonTypeCustom]; // 设置指标tag,用来记录轨迹
button.tag = i; // 加入按钮到lock view
[self addSubview:button];
}
} /** 设置按钮位置尺寸 */
- (void)layoutSubviews {
[super layoutSubviews]; // 取出所有按钮
for (int i=; i<self.subviews.count; i++) {
HVWLockButton *button = self.subviews[i];
CGFloat buttonWidth = ;
CGFloat buttonHeight = ; // 此按钮所在列号
int col = i % BUTTON_COL_COUNT;
// 此按钮所在行号
int row = i / BUTTON_COL_COUNT;
// 等分水平多余空间,计算出间隙
CGFloat marginX = (self.frame.size.width - BUTTON_COL_COUNT * buttonWidth) / (BUTTON_COL_COUNT + );
CGFloat marginY = marginX; // x坐标
CGFloat buttonX = marginX + col * (buttonWidth + marginX);
// y坐标
CGFloat buttonY = marginY + row * (buttonHeight + marginY); button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
}
} #pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self touchesMoved:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view]; // 检测哪个按钮被点中了
for (HVWLockButton *button in self.subviews) { // 如果触碰到了此按钮
if (CGRectContainsPoint(button.touchFrame, touchLocation)) {
button.selected = YES; // 如果此按钮没有被触碰过才进行处理
if (![self.selectedButtons containsObject:button]) {
// 加入到数组
[self.selectedButtons addObject:button];
}
} // 当前触摸位置
self.currentTouchLocation = touchLocation;
} // 重绘
[self setNeedsDisplay];
} - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 轨迹序列
NSMutableString *passPath = [NSMutableString string]; // 合成轨迹序列
for (HVWLockButton *button in self.selectedButtons) {
// 添加到轨迹序列
[passPath appendFormat:@"%d", button.tag];
} // 调用代理方法
if ([self.delegate respondsToSelector:@selector(hvwLockView:didFinishedWithPath:)]) {
[self.delegate hvwLockView:self didFinishedWithPath:passPath];
} // 清除选中状态
[self.selectedButtons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)]; // 清空数组
[self.selectedButtons removeAllObjects]; // 重绘
[self setNeedsDisplay];
} #pragma mark - 绘图方法
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPath]; // 遍历已选择按钮数组
for (int i=; i<self.selectedButtons.count; i++) {
HVWLockButton *button = self.selectedButtons[i]; if ( == i) {
[path moveToPoint:button.center];
} else {
[path addLineToPoint:button.center];
}
} if (self.selectedButtons.count) {
[path addLineToPoint:self.currentTouchLocation];
} // 设置画笔
[[UIColor redColor] set];
[path setLineWidth:];
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinBevel]; [path stroke];
} @end

[iOS UI进阶 - 5.0] 手势解锁Demo的更多相关文章
- [iOS UI进阶 - 4.0] 涂鸦app Demo
A.需求 1.超简易画图,只有一种画笔 2.清屏功能 3.回退功能 4.保存功能 5.使用了cocos2D code source: https://github.com/hellovoidwor ...
- iOS UI进阶-6.0 手势
给每个页面添加手势,只需要统一设置不是根控制器的页面,都增加手势.需要自定义导航控制器 1.继承代理 @interface BSNavigationController ()<UIGesture ...
- [iOS UI进阶 - 3.0] 触摸事件的基本处理
A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
A.需求 1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo 2.v1.0 版本搭建基本框架 code source:https://github.com/hellovoidworld/H ...
- iOS UI进阶-1.0 Quartz2D
概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- iOS UI进阶-4.0 地图与定位
在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能 ...
- [iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
A.系统提供的手势识别器 1.敲击手势 UITapGestureRecognizer numberOfTapsRequired: 敲击次数 numberOfTouchesRequired: 同时敲 ...
- iOS UI进阶-3.0 核心动画
Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<Quar ...
随机推荐
- hdu 2986 Ballot evaluation (模拟)
题目 上次比赛的题目,好长时间了. 这几天感冒了很难受, 直到现在才整理, 上次比赛的时候,出了各种错误, ,,,样例都没过,题目读的也很差,今天做的时候, 看了一下网上的,发现一个代码特别简洁, ...
- ACM - ICPC World Finals 2013 A Self-Assembly
原题下载 : http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 这道题其实是2013年我AC的第一道题,非常的开心,这 ...
- Java与正则表达式
Java与正则表达式 标签: Java基础 正则 正如正则的名字所显示的是描述了一个规则, 通过这个规则去匹配字符串. 学习正则就是学习正则表达式的语法规则 正则语法 普通字符 字母, 数字, 汉字, ...
- 当ASP.NET MVC模型验证遇上CKEditor
项目需要,使用到了CKEditor编辑器.这是个很不错的富文本编辑器,但是当它绑定的字段需要进行模型验证的时候,却会出现验证失效的问题.因此本文旨在记录这个问题和给出解决办法.以下以Validatio ...
- 【JS】<c:foreach>用法
<c:foreach>类似于for和foreach循环 以下是我目前见过的用法: 1.循环遍历,输出所有的元素. <c:foreach items="${list}&q ...
- 消息提示和消息推送插件toastr
http://www.jq22.com/yanshi476 比较棒的消息提示和消息推送插件toastr function myIntervalshow() { // showPopup1(300, 1 ...
- ylbtech-SubwayNav(地铁线路导航)-数据库设计
ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计 DatabaseName:SubwayNav(地铁线路导航) Type:线路导航 1.A, ...
- DevExpress licenses.licx 的解决方法 z
在 使用DevExpress控件的时候.每次对窗体进行更改的时候,都会出现一个对话框.发布的时候 也会出现一个对话框.之前的解决方法是在发布的时候把licenses.licx给删除掉,但是这个方法治标 ...
- HDU5777 domino (BestCoder Round #85 B) 思路题+排序
分析:最终的结果肯定会分成若干个区间独立,这些若干个区间肯定是独立的(而且肯定是一边倒,左右都一样) 这样想的话,就是如何把这n-1个值分成 k份,使得和最小,那么就是简单的排序,去掉前k大的(注意l ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...