猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
源码:http://blog.csdn.net/u013357243/article/details/45542129
其实这个实现起来不难
第一步先放好主要的UI,一张背景图和一个View
第二部就是把9个button放到view中,设置好按钮的默认和选中图片。
注意:创建时候的模式是UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
/**
* 创建9个按钮添加到自定view中
*/
- (void)setup
{
for (int i = 0; i < 9; i++) {
// 1.创建按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 2.设置按钮的背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
// 3.添加按钮到View
[self addSubview:btn];
// btn.backgroundColor = [UIColor redColor];
// 4.禁止按钮的点击事件(因为我们需要监听触摸事件)
btn.userInteractionEnabled = NO;
// 5.设置按钮的tag作为唯一标识
btn.tag = i;
}
}
然后就是监听手指按下,移动,抬起时候的事件了,并且在相应的地方画线。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取按下的点
CGPoint startPoint = [self getCurrentTouchPoint:touches];
// 2.判断触摸的位置是否在按钮的范围内
UIButton *btn = [self getCurrentBtnWithPoint:startPoint];
// 存储按钮
if (btn)
{
// 设置选中状态
btn.selected = YES;
// 将按钮保存到数组中
[self.buttons addObject:btn];
}
btn.selected = YES;
// [self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取按下的点
CGPoint movePoint = [self getCurrentTouchPoint:touches];
// 2.获取触摸的按钮
UIButton *btn = [self getCurrentBtnWithPoint:movePoint];
// 存储按钮
if (btn && btn.selected != YES)
{
// 设置选中状态
btn.selected = YES;
// 将按钮保存到数组中
[self.buttons addObject:btn];
}
// 记录当前手指移动的位置
self.currentPoint = movePoint;
// 通知view绘制线段
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 取出用户输入的密码
NSMutableString *result = [NSMutableString string];
for (UIButton *btn in self.buttons) {
[result appendFormat:@"%d", btn.tag ];
}
// NSLog(@"result = %@", result);
// 通知代理,告诉代理用户输入的密码
if ([self.delegate respondsToSelector:@selector(lockViewDidClick:andPwd:)]) {
[self.delegate lockViewDidClick:self andPwd:result];
}
[self.buttons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
// 清空数组
[self.buttons removeAllObjects];
[self setNeedsDisplay];
// 清空currentPoint
self.currentPoint = CGPointZero;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 清空上下文
CGContextClearRect(ctx, rect);
// 从数组中取出所有的按钮, 连接所有按钮的中点
for (int i = 0; i < self.buttons.count; i++) {
// 取出按钮
UIButton *btn = self.buttons[i];
if (0 == i) {
CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);
}else
{
CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
}
}
// 判断如果当前点是00就不会只
// if (!CGPointEqualToPoint(self.currentPoint, CGPointZero)) {
//
// // 当所有的按钮的中点都连接号之后再连接手指当前的位置
// CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y);
// }
// 判断数组中是否有按钮, 如果有按钮就有起点, 有起点就不会报错
if (self.buttons.count != 0) {
CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y);
}
// [[UIColor greenColor] set];
[[UIColor colorWithRed:1/255.0 green:102/255.0 blue:172/255.0 alpha:0.5] set];
CGContextSetLineWidth(ctx, 10);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextStrokePath(ctx);
}
还有两个里面抽取出来的代码
/**
* 根据系统传入的UITouch集合获取当前触摸的点
* @return 当初触摸的点
*/
- (CGPoint)getCurrentTouchPoint:(NSSet *)touches
{
// 1.获取按下的点
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
return point;
}
/**
* 根据触摸点获取触摸到的按钮
* @return 触摸的按钮
*/
- (UIButton *)getCurrentBtnWithPoint:(CGPoint)point
{
// 2.判断触摸的位置是否在按钮的范围内
for (UIButton *btn in self.subviews) {
//
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
还有就是用代理来把设置好的密码传出去。用的是button的tag属性,可以看代码中。
#import <UIKit/UIKit.h>
@class NYLockView;
@protocol NYLockViewDelegate <NSObject>
- (void)lockViewDidClick:(NYLockView *)lockView andPwd:(NSString *)pwd;
@end
@interface NYLockView : UIView
@property (nonatomic, weak)IBOutlet id<NYLockViewDelegate> delegate;
@end
// 取出用户输入的密码
NSMutableString *result = [NSMutableString string];
for (UIButton *btn in self.buttons) {
[result appendFormat:@"%d", btn.tag ];
}
// NSLog(@"result = %@", result);
// 通知代理,告诉代理用户输入的密码
if ([self.delegate respondsToSelector:@selector(lockViewDidClick:andPwd:)]) {
[self.delegate lockViewDidClick:self andPwd:result];
}
猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。
原文地址:http://blog.csdn.net/u013357243/article/details/45542095