oc手势解锁密码demo
时间: 2023-08-02 07:11:27 浏览: 59
以下是一个使用 Objective-C 编写的手势解锁密码的示例代码:
首先,在你的项目中创建一个名为 "GestureLockView" 的 UIView 子类。这个类将用于绘制手势解锁的视图。
GestureLockView.h:
```objective-c
#import <UIKit/UIKit.h>
@protocol GestureLockViewDelegate <NSObject>
- (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path;
@end
@interface GestureLockView : UIView
@property (nonatomic, weak) id<GestureLockViewDelegate> delegate;
@end
```
GestureLockView.m:
```objective-c
#import "GestureLockView.h"
@interface GestureLockView()
@property (nonatomic, strong) NSMutableArray *selectedButtons;
@end
@implementation GestureLockView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.selectedButtons = [NSMutableArray array];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
for (int i = 0; i < 9; i++) {
CGFloat x = (i % 3) * 100 + 25;
CGFloat y = (i / 3) * 100 + 25;
CGRect frame = CGRectMake(x, y, 50, 50);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
button.tag = i + 1;
button.layer.cornerRadius = 25.0;
button.layer.borderWidth = 1.0;
button.layer.borderColor = [UIColor blackColor].CGColor;
[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateSelected];
[button setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
if (self.selectedButtons.count > 0) {
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i = 0; i < self.selectedButtons.count; i++) {
UIButton *button = self.selectedButtons[i];
CGPoint point = button.center;
if (i == 0) {
[path moveToPoint:point];
} else {
[path addLineToPoint:point];
}
}
[path addLineToPoint:self.currentPoint];
[[UIColor blueColor] set];
path.lineWidth = 5.0;
CGContextAddPath(context, path.CGPath);
CGContextStrokePath(context);
}
}
- (void)buttonTapped:(UIButton *)button {
button.selected = YES;
[self.selectedButtons addObject:button];
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self handleTouches:touches];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self handleTouches:touches];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSMutableString *path = [NSMutableString string];
for (UIButton *button in self.selectedButtons) {
[path appendFormat:@"%ld", button.tag];
}
[self.delegate gestureLockView:self didFinishWithPath:path];
[self reset];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self reset];
}
- (void)handleTouches:(NSSet<UITouch *> *)touches {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
for (UIButton *button in self.subviews) {
if (CGRectContainsPoint(button.frame, point)) {
if (![self.selectedButtons containsObject:button]) {
button.selected = YES;
[self.selectedButtons addObject:button];
}
break;
}
}
self.currentPoint = point;
[self setNeedsDisplay];
}
- (void)reset {
for (UIButton *button in self.selectedButtons) {
button.selected = NO;
}
[self.selectedButtons removeAllObjects];
[self setNeedsDisplay];
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
```
在你的视图控制器中,你可以添加一个 GestureLockView 实例,并设置它的 delegate。当手势解锁完成时,GestureLockView 将调用代理方法 gestureLockView:didFinishWithPath:,你可以在这个方法中处理解锁的逻辑。
ViewController.h:
```objective-c
#import <UIKit/UIKit.h>
#import "GestureLockView.h"
@interface ViewController : UIViewController <GestureLockViewDelegate>
@end
```
ViewController.m:
```objective-c
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) GestureLockView *lockView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.lockView = [[GestureLockView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
self.lockView.center = self.view.center;
self.lockView.delegate = self;
[self.view addSubview:self.lockView];
}
- (void)gestureLockView:(UIView *)lockView didFinishWithPath:(NSString *)path {
NSLog(@"Path: %@", path);
if ([path isEqualToString:@"123"]) {
NSLog(@"Unlock success!");
} else {
NSLog(@"Unlock failed!");
}
}
@end
```
这里的解锁密码是 "123"。你可以根据需要修改这个密码。