效果图
项目结构
常量的定义
//const 定义 只读的变量名,在其他的类中不能声明同样的变量名
CGFloat const btnCount = 9;
// define只是字符的转换
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
UIView的两种init方法
//通过代码创建会调用这个方法
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self AddButton];
}
return self;
}
//通过storyboard 或者 xib 文件创建的时候会调用
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[self AddButton];
}
return self;
}
常用插件
在github上查找,然后下载,完成之后运行
KSImageNamed
VVDocumenter-Xcode
ColorSense-for-Xcode-master
触摸方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesEnded:touches withEvent:event];
}
触摸点的坐标
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
触摸的按钮
- (UIButton *)buttonWithPoint:(CGPoint)point{
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
点是否在指定CGRect内
bool CGRectContainsPoint (
CGRect rect,
CGPoint point
);
.m 里声明私有属性
//匿名扩展(类目的特例) :可以声明属性,私有的
@interface JKLockView ()
@property (nonatomic, strong) NSMutableArray *selectedBtns;
@property (nonatomic, assign) CGPoint currentPoint;
@end
数组或字典添加对象需要先判断
if (btn) {
[self.selectedBtns addObject:btn];
}
开始绘图
[self setNeedsDisplay];
绘制贝塞尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 8;
path.lineJoinStyle = kCGLineJoinRound;
[[UIColor colorWithRed:32/255.0 green:210/255.0 blue:254/255.0 alpha:0.5] set];
//遍历按钮
for (int i = 0; i < self.selectedBtns.count; i++) {
UIButton *button = self.selectedBtns[i];
if (i == 0) {
//设置起点
[path moveToPoint:button.center];
}else{//连线
[path addLineToPoint:button.center];
}
}
[path addLineToPoint:self.currentPoint];
[path stroke];
数组中每个对象执行某方法
//makeObjectsPerformSelector
//每个按钮设为没有选中状态
//向数组中的每一个对象发送方法 setSelected:,方法参数为 NO
[self.selectedBtns makeObjectsPerformSelector:@selector(setSelected:) withObject:NO];
IBOutlet 和 delegate连接
@property (nonatomic, assign) IBOutlet id<JKLockViewDelegate> delegate;
UIAlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"密码正确" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
连接字符串
[path appendFormat:@"%ld",(long)btn.tag];