DBGuestureLock.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //
  2. // DBGuestureLockView.m
  3. // DBGuestureLock
  4. //
  5. // Created by DeBao.Wu on 2/27/16.
  6. // Email: i36.lib@gmail.com QQ: 754753371
  7. // Copyright © 2016 http://i36.Me/. All rights reserved.
  8. // Github地址: https://github.com/i36lib/DBGuestureLock/
  9. #import "DBGuestureLock.h"
  10. #import "DBGuestureButton.h"
  11. #define DBGuestureLockPaswd @"Me_i36_DBGuestureLock_Password"
  12. @interface DBGuestureLock()
  13. @property (nonatomic, assign)CGPoint currentPoint;
  14. @property (nonatomic, strong)NSMutableArray *selectedButtons;
  15. @property (nonatomic, assign)BOOL isPasswordSetup;
  16. @property (nonatomic, assign)BOOL fillCenterPoint;
  17. @property (nonatomic, assign)BOOL showCenterPoint;
  18. @property (nonatomic, assign)CGFloat lineWidth;
  19. @property (nonatomic, assign)CGFloat circleRadius;
  20. @property (nonatomic, assign)CGFloat strokeWidth;
  21. @property (nonatomic, assign)CGFloat centerPointRadius;
  22. @property (nonatomic, strong)UIColor *lineColor;
  23. @property (nonatomic, strong)UIColor *fillColor;
  24. @property (nonatomic, strong)UIColor *strokeColor;
  25. @property (nonatomic, strong)UIColor *centerPointColor;
  26. //Work with block
  27. @property (nonatomic, assign)BOOL fillCenterPointOnStateNormal;
  28. @property (nonatomic, assign)BOOL showCenterPointOnStateNormal;
  29. @property (nonatomic, assign)CGFloat lineWidthOnStateNormal;
  30. @property (nonatomic, assign)CGFloat circleRadiusOnStateNormal;
  31. @property (nonatomic, assign)CGFloat strokeWidthOnStateNormal;
  32. @property (nonatomic, assign)CGFloat centerPointRadiusOnStateNormal;
  33. @property (nonatomic, strong)UIColor *lineColorOnStateNormal;
  34. @property (nonatomic, strong)UIColor *fillColorOnStateNormal;
  35. @property (nonatomic, strong)UIColor *strokeColorOnStateNormal;
  36. @property (nonatomic, strong)UIColor *centerPointColorOnStateNormal;
  37. @property (nonatomic, assign)BOOL fillCenterPointOnStateSelected;
  38. @property (nonatomic, assign)BOOL showCenterPointOnStateSelected;
  39. @property (nonatomic, assign)CGFloat lineWidthOnStateSelected;
  40. @property (nonatomic, assign)CGFloat circleRadiusOnStateSelected;
  41. @property (nonatomic, assign)CGFloat strokeWidthOnStateSelected;
  42. @property (nonatomic, assign)CGFloat centerPointRadiusOnStateSelected;
  43. @property (nonatomic, strong)UIColor *lineColorOnStateSelected;
  44. @property (nonatomic, strong)UIColor *fillColorOnStateSelected;
  45. @property (nonatomic, strong)UIColor *strokeColorOnStateSelected;
  46. @property (nonatomic, strong)UIColor *centerPointColorOnStateSelected;
  47. @property (nonatomic, assign)BOOL fillCenterPointOnStateIncorrect;
  48. @property (nonatomic, assign)BOOL showCenterPointOnStateIncorrect;
  49. @property (nonatomic, assign)CGFloat lineWidthOnStateIncorrect;
  50. @property (nonatomic, assign)CGFloat circleRadiusOnStateIncorrect;
  51. @property (nonatomic, assign)CGFloat strokeWidthOnStateIncorrect;
  52. @property (nonatomic, assign)CGFloat centerPointRadiusOnStateIncorrect;
  53. @property (nonatomic, strong)UIColor *lineColorOnStateIncorrect;
  54. @property (nonatomic, strong)UIColor *fillColorOnStateIncorrect;
  55. @property (nonatomic, strong)UIColor *strokeColorOnStateIncorrect;
  56. @property (nonatomic, strong)UIColor *centerPointColorOnStateIncorrect;
  57. @end
  58. @implementation DBGuestureLock
  59. +(BOOL)passwordSetupStatus {
  60. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  61. NSString *password = [defaults valueForKey:DBGuestureLockPaswd];
  62. if (password == nil || [password length] <= 0) {
  63. return NO;
  64. }
  65. return YES;
  66. }
  67. +(void)clearGuestureLockPassword {
  68. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  69. [defaults setValue: nil forKey:DBGuestureLockPaswd];
  70. }
  71. +(NSString *)getGuestureLockPassword {
  72. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  73. NSString *password = [defaults valueForKey:DBGuestureLockPaswd];
  74. return password;
  75. }
  76. -(BOOL)isPasswordSetup {
  77. return [[self class] passwordSetupStatus];
  78. }
  79. //@Override
  80. -(void)drawRect:(CGRect)rect {
  81. [super drawRect:rect];
  82. if ([self.selectedButtons count]== 0) {
  83. return;
  84. }
  85. UIBezierPath *path = [UIBezierPath bezierPath];
  86. [path setLineWidth: self.lineWidth];
  87. [self.lineColor set];
  88. [path setLineJoinStyle: kCGLineJoinRound];
  89. [path setLineCapStyle: kCGLineCapRound];
  90. for (NSInteger i=0; i<[self.selectedButtons count]; i++) {
  91. DBGuestureButton *button = self.selectedButtons[i];
  92. if (i == 0) {
  93. [path moveToPoint:[button center]];
  94. } else {
  95. [path addLineToPoint: [button center]];
  96. }
  97. [button setNeedsDisplay];
  98. }
  99. [path addLineToPoint:self.currentPoint];
  100. [path stroke];
  101. }
  102. +(instancetype)lockOnView:(UIView*)view delegate:(id<DBGuestureLockDelegate>)delegate {
  103. return [DBGuestureLock lockOnView:view offsetFromBottom:60.f delegate:delegate];
  104. }
  105. +(instancetype)lockOnView:(UIView*)view offsetFromBottom:(CGFloat)offset delegate:(id<DBGuestureLockDelegate>)delegate {
  106. CGFloat width = view.frame.size.height > view.frame.size.width ? view.frame.size.width : view.frame.size.height;
  107. CGFloat height = view.frame.size.height < view.frame.size.width ? view.frame.size.width : view.frame.size.height;
  108. CGRect frame = CGRectMake(0, height - width - offset, width, width);
  109. DBGuestureLock *lock = [[DBGuestureLock alloc] initWithFrame:frame];
  110. lock.delegate = delegate;
  111. lock.onPasswordSet = nil;
  112. lock.onGetCorrectPswd = nil;
  113. lock.onGetIncorrectPswd = nil;
  114. return lock;
  115. }
  116. +(instancetype)lockOnView:(UIView*)view onPasswordSet:(void (^)(DBGuestureLock *lock, NSString *password))onPasswordSet onGetCorrectPswd:(void (^)(DBGuestureLock *lock, NSString *password))onGetCorrectPswd onGetIncorrectPswd:(void (^)(DBGuestureLock *lock, NSString *password))onGetIncorrectPswd {
  117. return [DBGuestureLock lockOnView:view offsetFromBottom:60.f onPasswordSet:onPasswordSet onGetCorrectPswd:onGetCorrectPswd onGetIncorrectPswd:onGetIncorrectPswd];
  118. }
  119. +(instancetype)lockOnView:(UIView*)view offsetFromBottom:(CGFloat)offset onPasswordSet:(void (^)(DBGuestureLock *lock, NSString *password))onPasswordSet onGetCorrectPswd:(void (^)(DBGuestureLock *lock, NSString *password))onGetCorrectPswd onGetIncorrectPswd:(void (^)(DBGuestureLock *lock, NSString *password))onGetIncorrectPswd {
  120. CGFloat width = view.frame.size.height > view.frame.size.width ? view.frame.size.width : view.frame.size.height;
  121. CGFloat height = view.frame.size.height < view.frame.size.width ? view.frame.size.width : view.frame.size.height;
  122. CGRect frame = CGRectMake(0, height - width - offset, width, width);
  123. DBGuestureLock *lock = [[DBGuestureLock alloc] initWithFrame:frame];
  124. lock.delegate = nil;
  125. lock.onPasswordSet = onPasswordSet;
  126. lock.onGetCorrectPswd = onGetCorrectPswd;
  127. lock.onGetIncorrectPswd = onGetIncorrectPswd;
  128. return lock;
  129. }
  130. -(void)setupLockThemeWithLineColor:(UIColor*)lineColor lineWidth:(CGFloat)lineWidth strokeColor:(UIColor*)strokeColor strokeWidth:(CGFloat)strokeWidth circleRadius:(CGFloat)circleRadius fillColor:(UIColor*)fillColor showCenterPoint:(BOOL)showCenterPoint centerPointColor:(UIColor*)centerPointColor centerPointRadius:(CGFloat)centerPointRadius fillCenterPoint:(BOOL)fillCenterPoint onState:(DBButtonState)buttonState{
  131. if (self.delegate) { //Work with block only
  132. return;
  133. }
  134. switch (buttonState) {
  135. case DBButtonStateNormal:
  136. self.lineColorOnStateNormal = lineColor;
  137. self.lineWidthOnStateNormal = lineWidth;
  138. self.strokeColorOnStateNormal = strokeColor;
  139. self.strokeWidthOnStateNormal = strokeWidth;
  140. self.circleRadiusOnStateNormal = circleRadius;
  141. self.fillColorOnStateNormal = fillColor;
  142. self.showCenterPointOnStateNormal = showCenterPoint;
  143. self.centerPointColorOnStateNormal = centerPointColor;
  144. self.centerPointRadiusOnStateNormal = centerPointRadius;
  145. self.fillCenterPointOnStateNormal = fillCenterPoint;
  146. break;
  147. case DBButtonStateSelected:
  148. self.lineColorOnStateSelected = lineColor;
  149. self.lineWidthOnStateSelected = lineWidth;
  150. self.strokeColorOnStateSelected = strokeColor;
  151. self.strokeWidthOnStateSelected = strokeWidth;
  152. self.circleRadiusOnStateSelected = circleRadius;
  153. self.fillColorOnStateSelected = fillColor;
  154. self.showCenterPointOnStateSelected = showCenterPoint;
  155. self.centerPointColorOnStateSelected = centerPointColor;
  156. self.centerPointRadiusOnStateSelected = centerPointRadius;
  157. self.fillCenterPointOnStateSelected = fillCenterPoint;
  158. break;
  159. case DBButtonStateIncorrect:
  160. self.lineColorOnStateIncorrect = lineColor;
  161. self.lineWidthOnStateIncorrect = lineWidth;
  162. self.strokeColorOnStateIncorrect = strokeColor;
  163. self.strokeWidthOnStateIncorrect = strokeWidth;
  164. self.circleRadiusOnStateIncorrect = circleRadius;
  165. self.fillColorOnStateIncorrect = fillColor;
  166. self.showCenterPointOnStateIncorrect = showCenterPoint;
  167. self.centerPointColorOnStateIncorrect = centerPointColor;
  168. self.centerPointRadiusOnStateIncorrect = centerPointRadius;
  169. self.fillCenterPointOnStateIncorrect = fillCenterPoint;
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. //@Override
  176. -(instancetype)initWithFrame:(CGRect)frame {
  177. self = [super initWithFrame: frame];
  178. if (self) { // Draw 9 Lock Buttons
  179. [self setPropertiesByState:DBButtonStateNormal];
  180. _selectedButtons = [[NSMutableArray alloc] initWithCapacity:0];
  181. CGFloat width = frame.size.height > frame.size.width ? frame.size.width : frame.size.height;
  182. CGFloat spacing = width / 10; //Split into 10 Part
  183. CGFloat radius = spacing;
  184. [self setCircleRadius:radius];
  185. for (NSInteger i=0; i<9; i++) { //Total 9 buttons
  186. NSInteger row = i/3; //3 buttons for each row
  187. NSInteger col = i%3; //3 buttons for each column
  188. CGRect frame = CGRectMake((1+col*3)*spacing, (1+row*3)*spacing, 2*radius, 2*radius);
  189. DBGuestureButton *button = [[DBGuestureButton alloc] initWithFrame: frame];
  190. [button setTag: i+1]; // Present for password number
  191. [self addSubview: button];
  192. [self setBackgroundColor:[UIColor clearColor]];
  193. }
  194. }
  195. return self;
  196. }
  197. //@Override
  198. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  199. [super touchesBegan:touches withEvent:event];
  200. UITouch *touch = [touches anyObject];
  201. CGPoint point = [touch locationInView:self];
  202. self.currentPoint = point;
  203. for (DBGuestureButton *button in self.subviews) {
  204. if (CGRectContainsPoint(button.frame, point)) {
  205. [button setSelected:YES];
  206. if (![self.selectedButtons containsObject:button]) {
  207. [self.selectedButtons addObject:button];
  208. [self setPropertiesByState:DBButtonStateSelected];
  209. [button setNeedsDisplay];
  210. }
  211. }
  212. }
  213. [self setNeedsDisplay];
  214. }
  215. //@Override
  216. -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  217. [super touchesMoved:touches withEvent:event];
  218. UITouch *touch = [touches anyObject];
  219. CGPoint point = [touch locationInView:self];
  220. self.currentPoint = point;
  221. for (DBGuestureButton *button in self.subviews) {
  222. if (CGRectContainsPoint(button.frame, point)) {
  223. [button setSelected:YES];
  224. if (![self.selectedButtons containsObject:button]) {
  225. [self.selectedButtons addObject:button];
  226. [self setPropertiesByState:DBButtonStateSelected];
  227. }
  228. }
  229. }
  230. [self setNeedsDisplay];
  231. }
  232. //@Override
  233. -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  234. [super touchesEnded:touches withEvent:event];
  235. //Get password
  236. NSMutableString *password = [[NSMutableString alloc] initWithCapacity:0];
  237. for (NSInteger i=0; i<[self.selectedButtons count]; i++) {
  238. DBGuestureButton *button = self.selectedButtons[i];
  239. [password appendFormat:@"%li", (long)button.tag];
  240. }
  241. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  242. NSString *correctPswd = [defaults valueForKey:DBGuestureLockPaswd];
  243. if (correctPswd == nil || [correctPswd length] <= 0) {
  244. [defaults setValue: password forKey:DBGuestureLockPaswd];
  245. if (self.delegate) {
  246. [self.delegate guestureLock:self didSetPassword:password];
  247. } else {
  248. self.onPasswordSet(self, password);
  249. }
  250. [self setPropertiesByState:DBButtonStateNormal];
  251. } else if ([password isEqualToString:correctPswd]) {
  252. if (self.delegate) {
  253. [self.delegate guestureLock:self didGetCorrectPswd:password];
  254. } else {
  255. self.onGetCorrectPswd(self, password);
  256. }
  257. [self setPropertiesByState:DBButtonStateNormal];
  258. } else { //incorrect
  259. if (self.delegate) {
  260. [self.delegate guestureLock:self didGetIncorrectPswd:password];
  261. } else {
  262. self.onGetIncorrectPswd(self, password);
  263. }
  264. [self setPropertiesByState:DBButtonStateIncorrect];
  265. }
  266. DBGuestureButton *lastButton = [self.selectedButtons lastObject];
  267. [self setCurrentPoint:lastButton.center];
  268. [self setNeedsDisplay];
  269. }
  270. -(void)resetButtons {
  271. for (NSInteger i=0; i<[self.selectedButtons count]; i++) {
  272. DBGuestureButton *button = self.selectedButtons[i];
  273. [button setSelected:NO];
  274. }
  275. [self.selectedButtons removeAllObjects];
  276. [self setNeedsDisplay];
  277. }
  278. -(void)lockState:(NSArray *)states {
  279. NSNumber *stateNumber = [states objectAtIndex:0];
  280. [self setPropertiesByState:[stateNumber integerValue]];
  281. }
  282. -(void)setPropertiesByState:(DBButtonState)buttonState {
  283. switch (buttonState) {
  284. case DBButtonStateNormal:
  285. [self setUserInteractionEnabled:YES];
  286. [self resetButtons];
  287. self.fillCenterPoint = YES; //As default
  288. if (self.delegate) {
  289. if ([self.delegate respondsToSelector:@selector(fillButtonCircleCenterPointOnState:)]) {
  290. self.fillCenterPoint = [self.delegate fillButtonCircleCenterPointOnState:DBButtonStateNormal];
  291. }
  292. } else if (self.fillCenterPointOnStateNormal) {
  293. self.fillCenterPoint = self.fillCenterPointOnStateNormal;
  294. }
  295. self.showCenterPoint = YES; //As default
  296. if (self.delegate) {
  297. if ([self.delegate respondsToSelector:@selector(showButtonCircleCenterPointOnState:)]) {
  298. self.showCenterPoint = [self.delegate showButtonCircleCenterPointOnState:DBButtonStateNormal];
  299. }
  300. } else if (self.showCenterPointOnStateNormal) {
  301. self.showCenterPoint = self.showCenterPointOnStateNormal;
  302. }
  303. self.strokeWidth = 1.f; //As default
  304. if (self.delegate) {
  305. if ([self.delegate respondsToSelector:@selector(widthOfButtonCircleStrokeOnState:)]) {
  306. self.strokeWidth = [self.delegate widthOfButtonCircleStrokeOnState:DBButtonStateNormal];
  307. }
  308. } else if (self.strokeWidthOnStateNormal) {
  309. self.strokeWidth = self.strokeWidthOnStateNormal;
  310. }
  311. self.centerPointRadius = 10.f; //As default
  312. if (self.delegate){
  313. if ([self.delegate respondsToSelector:@selector(radiusOfButtonCircleCenterPointOnState:)]) {
  314. self.centerPointRadius = [self.delegate radiusOfButtonCircleCenterPointOnState:DBButtonStateNormal];
  315. }
  316. } else if (self.centerPointColorOnStateNormal){
  317. self.centerPointColor = self.centerPointColorOnStateNormal;
  318. }
  319. self.lineWidth = 0.f;
  320. if (self.delegate) {
  321. if ([self.delegate respondsToSelector:@selector(lineWidthOfGuestureOnState:)]) {
  322. self.centerPointRadius = [self.delegate lineWidthOfGuestureOnState:DBButtonStateNormal];
  323. }
  324. } else if (self.lineWidthOnStateNormal) {
  325. self.lineWidth = self.lineWidthOnStateNormal;
  326. }
  327. self.lineColor = [UIColor whiteColor];
  328. if (self.delegate) {
  329. if ([self.delegate respondsToSelector:@selector(lineColorOfGuestureOnState:)]) {
  330. self.lineColor = [self.delegate lineColorOfGuestureOnState:DBButtonStateNormal];
  331. }
  332. } else if (self.lineColorOnStateNormal) {
  333. self.lineColor = self.lineColorOnStateNormal;
  334. }
  335. //As default
  336. self.fillColor = [UIColor clearColor];
  337. if (self.delegate) {
  338. if ([self.delegate respondsToSelector:@selector(colorForFillingButtonCircleOnState:)]) {
  339. self.fillColor = [self.delegate colorForFillingButtonCircleOnState:DBButtonStateNormal];
  340. }
  341. } else if (self.fillColorOnStateNormal) {
  342. self.fillColor = self.fillColorOnStateNormal;
  343. }
  344. //As default
  345. self.strokeColor = [UIColor clearColor];
  346. if (self.delegate) {
  347. if ([self.delegate respondsToSelector:@selector(colorOfButtonCircleStrokeOnState:)]) {
  348. self.strokeColor = [self.delegate colorOfButtonCircleStrokeOnState:DBButtonStateNormal];
  349. }
  350. } else if (self.strokeColorOnStateNormal){
  351. self.strokeColor = self.strokeColorOnStateNormal;
  352. }
  353. //As default
  354. self.centerPointColor = [UIColor lightGrayColor];
  355. if (self.delegate) {
  356. if ([self.delegate respondsToSelector:@selector(colorOfButtonCircleCenterPointOnState:)]) {
  357. self.centerPointColor = [self.delegate colorOfButtonCircleCenterPointOnState:DBButtonStateNormal];
  358. }
  359. } else if (self.centerPointColorOnStateNormal){
  360. self.centerPointColor = self.centerPointColorOnStateNormal;
  361. }
  362. //self.circleRadius = self.circleRadius;
  363. break;
  364. case DBButtonStateSelected:
  365. self.fillCenterPoint = YES; //As default
  366. if (self.delegate) {
  367. if ([self.delegate respondsToSelector:@selector(fillButtonCircleCenterPointOnState:)]) {
  368. self.fillCenterPoint = [self.delegate fillButtonCircleCenterPointOnState:DBButtonStateSelected];
  369. }
  370. } else if (self.fillCenterPointOnStateSelected) {
  371. self.fillCenterPoint = self.fillCenterPointOnStateSelected;
  372. }
  373. self.showCenterPoint = YES; //As default
  374. if (self.delegate) {
  375. if ([self.delegate respondsToSelector:@selector(showButtonCircleCenterPointOnState:)]) {
  376. self.showCenterPoint = [self.delegate showButtonCircleCenterPointOnState:DBButtonStateSelected];
  377. }
  378. } else if (self.showCenterPointOnStateSelected) {
  379. self.showCenterPoint = self.showCenterPointOnStateSelected;
  380. }
  381. self.strokeWidth = 1.f; //As default
  382. if (self.delegate) {
  383. if ([self.delegate respondsToSelector:@selector(widthOfButtonCircleStrokeOnState:)]) {
  384. self.strokeWidth = [self.delegate widthOfButtonCircleStrokeOnState:DBButtonStateSelected];
  385. }
  386. } else if (self.strokeWidthOnStateSelected) {
  387. self.strokeWidth = self.strokeWidthOnStateSelected;
  388. }
  389. self.centerPointRadius = 10.f; //As default
  390. if (self.delegate){
  391. if ([self.delegate respondsToSelector:@selector(radiusOfButtonCircleCenterPointOnState:)]) {
  392. self.centerPointRadius = [self.delegate radiusOfButtonCircleCenterPointOnState:DBButtonStateSelected];
  393. }
  394. } else if (self.centerPointColorOnStateSelected){
  395. self.centerPointColor = self.centerPointColorOnStateSelected;
  396. }
  397. self.lineWidth = 2.f;
  398. if (self.delegate) {
  399. if ([self.delegate respondsToSelector:@selector(lineWidthOfGuestureOnState:)]) {
  400. self.centerPointRadius = [self.delegate lineWidthOfGuestureOnState:DBButtonStateSelected];
  401. }
  402. } else if (self.lineWidthOnStateSelected) {
  403. self.lineWidth = self.lineWidthOnStateSelected;
  404. }
  405. self.lineColor = [UIColor whiteColor];
  406. if (self.delegate) {
  407. if ([self.delegate respondsToSelector:@selector(lineColorOfGuestureOnState:)]) {
  408. self.lineColor = [self.delegate lineColorOfGuestureOnState:DBButtonStateSelected];
  409. }
  410. } else if (self.lineColorOnStateSelected) {
  411. self.lineColor = self.lineColorOnStateSelected;
  412. }
  413. //As default
  414. self.fillColor = [UIColor lightTextColor];
  415. if (self.delegate) {
  416. if ([self.delegate respondsToSelector:@selector(colorForFillingButtonCircleOnState:)]) {
  417. self.fillColor = [self.delegate colorForFillingButtonCircleOnState:DBButtonStateSelected];
  418. }
  419. } else if (self.fillColorOnStateSelected) {
  420. self.fillColor = self.fillColorOnStateSelected;
  421. }
  422. //As default
  423. self.strokeColor = [UIColor whiteColor];
  424. if (self.delegate) {
  425. if ([self.delegate respondsToSelector:@selector(colorOfButtonCircleStrokeOnState:)]) {
  426. self.strokeColor = [self.delegate colorOfButtonCircleStrokeOnState:DBButtonStateSelected];
  427. }
  428. } else if (self.strokeColorOnStateSelected){
  429. self.strokeColor = self.strokeColorOnStateSelected;
  430. }
  431. //As default
  432. self.centerPointColor = [UIColor whiteColor];
  433. if (self.delegate) {
  434. if ([self.delegate respondsToSelector:@selector(colorOfButtonCircleCenterPointOnState:)]) {
  435. self.centerPointColor = [self.delegate colorOfButtonCircleCenterPointOnState:DBButtonStateSelected];
  436. }
  437. } else if (self.centerPointColorOnStateSelected){
  438. self.centerPointColor = self.centerPointColorOnStateSelected;
  439. }
  440. //self.circleRadius = self.circleRadius;
  441. break;
  442. case DBButtonStateIncorrect:
  443. [self setUserInteractionEnabled:NO];
  444. self.fillCenterPoint = YES; //As default
  445. if (self.delegate) {
  446. if ([self.delegate respondsToSelector:@selector(fillButtonCircleCenterPointOnState:)]) {
  447. self.fillCenterPoint = [self.delegate fillButtonCircleCenterPointOnState:DBButtonStateIncorrect];
  448. }
  449. } else if (self.fillCenterPointOnStateIncorrect) {
  450. self.fillCenterPoint = self.fillCenterPointOnStateIncorrect;
  451. }
  452. self.showCenterPoint = YES; //As default
  453. if (self.delegate) {
  454. if ([self.delegate respondsToSelector:@selector(showButtonCircleCenterPointOnState:)]) {
  455. self.showCenterPoint = [self.delegate showButtonCircleCenterPointOnState:DBButtonStateIncorrect];
  456. }
  457. } else if (self.showCenterPointOnStateIncorrect) {
  458. self.showCenterPoint = self.showCenterPointOnStateIncorrect;
  459. }
  460. self.strokeWidth = 1.f; //As default
  461. if (self.delegate) {
  462. if ([self.delegate respondsToSelector:@selector(widthOfButtonCircleStrokeOnState:)]) {
  463. self.strokeWidth = [self.delegate widthOfButtonCircleStrokeOnState:DBButtonStateIncorrect];
  464. }
  465. } else if (self.strokeWidthOnStateIncorrect) {
  466. self.strokeWidth = self.strokeWidthOnStateIncorrect;
  467. }
  468. self.centerPointRadius = 10.f; //As default
  469. if (self.delegate){
  470. if ([self.delegate respondsToSelector:@selector(radiusOfButtonCircleCenterPointOnState:)]) {
  471. self.centerPointRadius = [self.delegate radiusOfButtonCircleCenterPointOnState:DBButtonStateIncorrect];
  472. }
  473. } else if (self.centerPointColorOnStateIncorrect){
  474. self.centerPointColor = self.centerPointColorOnStateIncorrect;
  475. }
  476. self.lineWidth = 5.f;
  477. if (self.delegate) {
  478. if ([self.delegate respondsToSelector:@selector(lineWidthOfGuestureOnState:)]) {
  479. self.centerPointRadius = [self.delegate lineWidthOfGuestureOnState:DBButtonStateIncorrect];
  480. }
  481. } else if (self.lineWidthOnStateIncorrect) {
  482. self.lineWidth = self.lineWidthOnStateIncorrect;
  483. }
  484. self.lineColor = [UIColor orangeColor];
  485. if (self.delegate) {
  486. if ([self.delegate respondsToSelector:@selector(lineColorOfGuestureOnState:)]) {
  487. self.lineColor = [self.delegate lineColorOfGuestureOnState:DBButtonStateIncorrect];
  488. }
  489. } else if (self.lineColorOnStateIncorrect) {
  490. self.lineColor = self.lineColorOnStateIncorrect;
  491. }
  492. //As default
  493. self.fillColor = [UIColor lightTextColor];
  494. if (self.delegate) {
  495. if ([self.delegate respondsToSelector:@selector(colorForFillingButtonCircleOnState:)]) {
  496. self.fillColor = [self.delegate colorForFillingButtonCircleOnState:DBButtonStateIncorrect];
  497. }
  498. } else if (self.fillColorOnStateIncorrect) {
  499. self.fillColor = self.fillColorOnStateIncorrect;
  500. }
  501. //As default
  502. self.strokeColor = [UIColor orangeColor];
  503. if (self.delegate) {
  504. if ([self.delegate respondsToSelector:@selector(colorOfButtonCircleStrokeOnState:)]) {
  505. self.strokeColor = [self.delegate colorOfButtonCircleStrokeOnState:DBButtonStateIncorrect];
  506. }
  507. } else if (self.strokeColorOnStateIncorrect){
  508. self.strokeColor = self.strokeColorOnStateIncorrect;
  509. }
  510. //As default
  511. self.centerPointColor = [UIColor orangeColor];
  512. if (self.delegate) {
  513. if ([self.delegate respondsToSelector:@selector(colorOfButtonCircleCenterPointOnState:)]) {
  514. self.centerPointColor = [self.delegate colorOfButtonCircleCenterPointOnState:DBButtonStateIncorrect];
  515. }
  516. } else if (self.centerPointColorOnStateIncorrect){
  517. self.centerPointColor = self.centerPointColorOnStateIncorrect;
  518. }
  519. //self.circleRadius = self.circleRadius;
  520. [self performSelector:@selector(lockState:) withObject:[NSArray arrayWithObject:[NSNumber numberWithInteger:DBButtonStateNormal]] afterDelay:1.f];
  521. break;
  522. default:
  523. break;
  524. }
  525. }
  526. @end