生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之手势解锁项目案例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS開發(fā)基礎(chǔ)之手勢(shì)解鎖項(xiàng)目案例
項(xiàng)目最終實(shí)現(xiàn)效果。
由于缺少紅色的error背景圖。我自己從安卓項(xiàng)目找到一個(gè)手勢(shì)解鎖,然后通過ps添加粉紅色的紅圈,才得以解決。為了分享給大家源碼,github和本地都進(jìn)行了備份。項(xiàng)目才100K左右。最后用到了block回調(diào)思想或者設(shè)置代理的方式。
github源碼地址:https://github.com/sunjunjunsun/Gesture-unlocking.git
CSDN資源地址:
https://download.csdn.net/download/A1521315qwss/15398441
具體的實(shí)現(xiàn)代碼:
#import "ViewController.h"
#import "LJView.h"
@interface ViewController
()
@property (weak
, nonatomic
) IBOutlet LJView
*passwordView
;
@end
@implementation ViewController
- (void)viewDidLoad
{[super viewDidLoad
];self.view
.backgroundColor
= [UIColor colorWithPatternImage
:[UIImage imageNamed
:@"Home_refresh_bg"]];self.passwordView
.passwordBlock
= ^(NSString
* pwd
) {if([pwd isEqualToString
:@"123"]){NSLog(@"輸入密碼正確");return YES
;}else{NSLog(@"輸入密碼錯(cuò)誤");return NO
;}};
}
@end
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface LJView
: UIView
@property(nonatomic
,copy
) BOOL
(^passwordBlock
)(NSString
*);
@end
NS_ASSUME_NONNULL_END
#import "LJView.h"
#define kButtonCount 9
@interface LJView
()
@property(nonatomic
,strong
)NSMutableArray
*btns
;
@property(nonatomic
,strong
)NSMutableArray
*lineBtns
;
@property(nonatomic
,assign
)CGPoint currentPoint
;
@end
@implementation LJView
- (void)drawRect
:(CGRect
)rect
{if(!self.lineBtns
.count
){return;}UIBezierPath
*path
= [UIBezierPath bezierPath
];for(int i
=0;i
<self.lineBtns
.count
;++i
){UIButton
*btn
=self.lineBtns
[i
];if(i
==0){[path moveToPoint
:btn
.center
];}else{[path addLineToPoint
:btn
.center
];}}[path addLineToPoint
:self.currentPoint
];[[UIColor whiteColor
] set
];[path setLineWidth
:10];[path setLineJoinStyle
:kCGLineJoinRound
];[path setLineCapStyle
:kCGLineCapRound
];[path stroke
];
}- (NSMutableArray
*)lineBtns
{if(!_lineBtns
){_lineBtns
= [NSMutableArray array
];}return _lineBtns
;
}
- (void)touchesMoved
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{UITouch
*t
=touches
.anyObject
;CGPoint p
=[t locationInView
:t
.view
];self.currentPoint
= p
;for(int i
=0;i
<self.btns
.count
;++i
){UIButton
*btn
= self.btns
[i
];if(CGRectContainsPoint(btn
.frame
, p
)){btn
.selected
= YES
;if(![self.lineBtns containsObject
:btn
]){[self.lineBtns addObject
:btn
];}}}[self setNeedsDisplay
];
}- (void)touchesEnded
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{self.currentPoint
= [[self.lineBtns lastObject
] center
];[self setNeedsDisplay
];NSString
*password
= @"";for(int i
=0;i
<self.lineBtns
.count
;++i
){UIButton
*btn
=self.lineBtns
[i
];btn
.selected
= NO
;btn
.enabled
= NO
;password
= [password stringByAppendingFormat
:[NSString stringWithFormat
:@"%ld",btn
.tag
]];}NSLog(@"%@",password
);if(self.passwordBlock
){if(self.passwordBlock(password
)){NSLog(@"正確");}else{NSLog(@"錯(cuò)誤");}}[self setUserInteractionEnabled
:NO
];dispatch_after(dispatch_time(DISPATCH_TIME_NOW
, (int64_t
)(2*NSEC_PER_SEC
)), dispatch_get_main_queue(), ^{[self setUserInteractionEnabled
:YES
];[self clear
];});}
-(void)clear
{for(int i
=0;i
<self.btns
.count
;++i
){UIButton
*btn
= self.btns
[i
];btn
.selected
= NO
;btn
.enabled
= YES
;}[self.lineBtns removeAllObjects
];[self setNeedsDisplay
];
}
- (void)touchesBegan
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{UITouch
*t
=touches
.anyObject
;CGPoint p
=[t locationInView
:t
.view
];for(int i
=0;i
<self.btns
.count
;++i
){UIButton
*btn
= self.btns
[i
];if(CGRectContainsPoint(btn
.frame
, p
)){btn
.selected
= YES
;[self.lineBtns addObject
:btn
];}}
}- (NSMutableArray
*)btns
{if(!_btns
){_btns
=[NSMutableArray array
];}return _btns
;
}
- (void)awakeFromNib
{for(int i
=0;i
<kButtonCount
;++i
){UIButton
*btn
= [[UIButton alloc
] init
];btn
.tag
= i
;[btn setUserInteractionEnabled
:NO
];[btn setBackgroundImage
:[UIImage imageNamed
:@"gesture_node_normal"] forState
:UIControlStateNormal
];[btn setBackgroundImage
:[UIImage imageNamed
:@"gesture_node_highlighted"] forState
:UIControlStateSelected
];[btn setBackgroundImage
:[UIImage imageNamed
:@"gesture_node_error-2"] forState
:UIControlStateDisabled
];[self addSubview
:btn
];[self.btns addObject
:btn
];}
}- (void)layoutSubviews
{[super layoutSubviews
];CGFloat w
= 74;CGFloat h
= w
;int colCount
= 3;CGFloat margin
=(self.frame
.size
.width
- 3 *w
)/4;for(int i
=0;i
<kButtonCount
;++i
){CGFloat x
= (i
%colCount
)*(margin
+w
)+margin
;CGFloat y
= (i
/colCount
)*(margin
+w
)+margin
;[self.btns
[i
] setFrame
:CGRectMake(x
, y
, w
, h
)];}
}
@end
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的IOS开发基础之手势解锁项目案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。