//1.下面是實現的步驟,基本上下面的注釋應該都寫明白了,多謝大牛們指點,如果需要素材和源工程文件,可以索要,謝謝支持 ?
//2.在最下面附有效果圖
#import "ViewController.h"
#import "FFFGestureView.h"
@interface ViewController ()
@PRoperty (weak, nonatomic) IBOutlet UIImageView *smallView;
@property (weak, nonatomic) IBOutlet FFFGestureView *gestureView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_refresh_bg"]];
self.gestureView.myblock = ^(UIImage *image,NSString *pass){
NSString *turePass = @"012";
if([pass isEqualToString:turePass]){
self.smallView.image = nil;
return YES;
}else{
self.smallView.image = image;
return NO;
}
};
}
***************************************************************************
#import <UIKit/UIKit.h>
@interface FFFGestureView : UIView
@property (nonatomic,copy) BOOL(^myblock)(UIImage *,NSString *);
@end
***************************************************************************
#import "FFFGestureView.h"
#import "SVProgressHUD.h"
#define SUMCOUNT 9
@interface FFFGestureView ()
//定義可變數組加載需要的button
@property (nonatomic,strong) NSArray *buttons;
//設置數組接收畫的線
@property (nonatomic,strong) NSMutableArray *lineButton;
//定義一個點,保存手指當前的位置
@property(nonatomic,assign) CGPoint currentPoint;
@end
@implementation FFFGestureView
-(NSMutableArray *)lineButton{
if(_lineButton==nil){
_lineButton = [NSMutableArray array];
}
return _lineButton;
}
//懶加載button
-(NSArray *)buttons{
if(_buttons==nil){
NSMutableArray *arrayM = [NSMutableArray array];
for(int i=0;i<SUMCOUNT;i++){
UIButton *button = [[UIButton alloc] init];
button.tag = i;
// button.backgroundColor = [UIColor redColor];
[button setUserInteractionEnabled:NO];
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"gesture_node_error"] forState:UIControlStateSelected];
[self addSubview:button];
[arrayM addObject:button];
}
_buttons = [arrayM copy];
}
return _buttons;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 獲取touch對象
UITouch *touch = [touches anyObject];
// 獲取點擊的點
CGPoint point = [touch locationInView:touch.view];
// 遍歷所有的按鈕
for(int i=0;i<self.buttons.count;i++){
UIButton *button = self.buttons[i];
// 按鈕的frame是否包含了點擊的點
if(CGRectContainsPoint(button.frame, point)){
// 開始高亮狀態
button.highlighted = YES;
// 判斷這個按鈕是不是已經添加到了數組當中,如果沒有在添加
if(![self.lineButton containsObject:button]){
[self.lineButton addObject:button];
}
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 獲取touch對象
UITouch *touch = [touches anyObject];
// 獲取點擊的點
CGPoint point = [touch locationInView:touch.view];
// 獲取移動的時候手指位置
self.currentPoint = point;
// 遍歷所有的按鈕
for(int i=0;i<self.buttons.count;i++){
UIButton *button = self.buttons[i];
// 按鈕的frame是否包含了點擊的點
if(CGRectContainsPoint(button.frame, point)){
// 開始高亮狀態
button.highlighted = YES;
// 判斷這個按鈕是不是已經添加到了數組當中,如果沒有在添加
if(![self.lineButton containsObject:button]){
[self.lineButton addObject:button];
}
}
}
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// 解決錯誤的時候,最后手指的位置不連接
self.currentPoint = [[self.lineButton lastObject] center];
[self setNeedsDisplay];
for (int i=0; i<self.lineButton.count; i++) {
UIButton *button = self.lineButton[i];
button.selected = YES;
button.highlighted = NO;
}
// 在恢復之前不能進行連線
[self setUserInteractionEnabled:NO];
NSString *passWord = @"";
for (int i=0; i<self.lineButton.count; i++) {
// 拼接按鈕的tag
passWord = [passWord stringByAppendingString:[NSString stringWithFormat:@"%ld",[self.lineButton[i] tag]]];
}
// 輸出當前VIew作為image
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
// 獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 渲染
[self.layer renderInContext:ctx];
// 通過上下文獲取圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 關閉上下文
UIGraphicsEndImageContext();
if(self.myblock){
if(self.myblock(image,passWord)){
[SVProgressHUD showSuccessWithStatus:@"密碼正確"];
}else{
[SVProgressHUD showErrorWithStatus:@"密碼錯誤"];
}
}
// 顯示錯誤的樣式 1秒鐘
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 恢復之后再把用戶交互打開
[self setUserInteractionEnabled:YES];
[self clearScreen];
});
}
-(void)clearScreen{
[self.lineButton removeAllObjects];
for (int i=0; i<self.buttons.count ; i++) {
UIButton *button = self.buttons[i];
button.highlighted = NO;
button.selected = NO;
}
// 恢復原始狀態
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect{
// 創建路徑對象
UIBezierPath *path = [UIBezierPath bezierPath];
for(int i=0;i<self.lineButton.count;i++){
if(i==0){
[path moveToPoint:[self.lineButton[i] center]];
}else{
[path addLineToPoint:[self.lineButton[i] center]];
}
}
if(self.lineButton.count){
// 連接到手指的位置
[path addLineToPoint:self.currentPoint];
}
// 設置顏色
[[UIColor redColor] set];
// 設置線寬
path.lineWidth = 10;
// 設置連接處的樣式
[path setLineJoinStyle:kCGLineJoinRound];
// 設置頭尾的樣式
[path setLineCapStyle:kCGLineCapRound];
// 渲染
[path stroke];
}
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat w = 74;
CGFloat h = w;
CGFloat margin = (self.frame.size.width-3*w)/4;
for(int i=0;i<self.buttons.count;i++){
UIButton *button = self.buttons[i];
CGFloat row = i % 3;
CGFloat col = i / 3;
CGFloat x = row * (margin + w) + margin;
CGFloat y = col * (margin + h) + margin;
button.frame = CGRectMake(x, y, w, h);
}
}
@end
新聞熱點
疑難解答