//1.下面是實(shí)現(xiàn)的步驟,基本上下面的注釋?xiě)?yīng)該都寫(xiě)明白了,多謝大牛們指點(diǎn),如果需要素材和源工程文件,可以索要,謝謝支持 ?
//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 ()
//定義可變數(shù)組加載需要的button
@property (nonatomic,strong) NSArray *buttons;
//設(shè)置數(shù)組接收畫(huà)的線
@property (nonatomic,strong) NSMutableArray *lineButton;
//定義一個(gè)點(diǎn),保存手指當(dāng)前的位置
@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對(duì)象
UITouch *touch = [touches anyObject];
// 獲取點(diǎn)擊的點(diǎn)
CGPoint point = [touch locationInView:touch.view];
// 遍歷所有的按鈕
for(int i=0;i<self.buttons.count;i++){
UIButton *button = self.buttons[i];
// 按鈕的frame是否包含了點(diǎn)擊的點(diǎn)
if(CGRectContainsPoint(button.frame, point)){
// 開(kāi)始高亮狀態(tài)
button.highlighted = YES;
// 判斷這個(gè)按鈕是不是已經(jīng)添加到了數(shù)組當(dāng)中,如果沒(méi)有在添加
if(![self.lineButton containsObject:button]){
[self.lineButton addObject:button];
}
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// 獲取touch對(duì)象
UITouch *touch = [touches anyObject];
// 獲取點(diǎn)擊的點(diǎn)
CGPoint point = [touch locationInView:touch.view];
// 獲取移動(dòng)的時(shí)候手指位置
self.currentPoint = point;
// 遍歷所有的按鈕
for(int i=0;i<self.buttons.count;i++){
UIButton *button = self.buttons[i];
// 按鈕的frame是否包含了點(diǎn)擊的點(diǎn)
if(CGRectContainsPoint(button.frame, point)){
// 開(kāi)始高亮狀態(tài)
button.highlighted = YES;
// 判斷這個(gè)按鈕是不是已經(jīng)添加到了數(shù)組當(dāng)中,如果沒(méi)有在添加
if(![self.lineButton containsObject:button]){
[self.lineButton addObject:button];
}
}
}
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// 解決錯(cuò)誤的時(shí)候,最后手指的位置不連接
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;
}
// 在恢復(fù)之前不能進(jìn)行連線
[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]]];
}
// 輸出當(dāng)前VIew作為image
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
// 獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 渲染
[self.layer renderInContext:ctx];
// 通過(guò)上下文獲取圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 關(guān)閉上下文
UIGraphicsEndImageContext();
if(self.myblock){
if(self.myblock(image,passWord)){
[SVProgressHUD showSuccessWithStatus:@"密碼正確"];
}else{
[SVProgressHUD showErrorWithStatus:@"密碼錯(cuò)誤"];
}
}
// 顯示錯(cuò)誤的樣式 1秒鐘
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 恢復(fù)之后再把用戶交互打開(kāi)
[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;
}
// 恢復(fù)原始狀態(tài)
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect{
// 創(chuàng)建路徑對(duì)象
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];
}
// 設(shè)置顏色
[[UIColor redColor] set];
// 設(shè)置線寬
path.lineWidth = 10;
// 設(shè)置連接處的樣式
[path setLineJoinStyle:kCGLineJoinRound];
// 設(shè)置頭尾的樣式
[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


新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注