for ( int i = 0; i <text.length;i++){
pX = arc4random() % width + rect.size.width/text.length * i;
pY = arc4random() % height;
point = CGPointMake(pX, pY);
unichar c = [text characterAtIndex:i];
NSString *textC = [NSString stringWithFormat:@"%C", c];
[textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize}];
}
#import@interface AuthCodeViewController : UIViewController
@end
#import "AuthCodeViewController.h"#import "AuthcodeView.h"@interface AuthCodeViewController (){
AuthcodeView *authCodeView;
UITextField *_input;
}
@end
@implementation AuthCodeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//顯示驗(yàn)證碼界面
authCodeView = [[AuthcodeView alloc] initWithFrame:CGRectMake(30, 100, 70, 40)];
[self.view addSubview:authCodeView];
//添加輸入框
_input = [[UITextField alloc] initWithFrame:CGRectMake(30, 220, self.view.frame.size.width-60, 40)];
_input.layer.borderColor = [UIColor lightGrayColor].CGColor;
_input.layer.borderWidth = 2.0;
_input.layer.cornerRadius = 5.0;
_input.font = [UIFont systemFontOfSize:21];
_input.placeholder = @"請(qǐng)輸入驗(yàn)證碼!";
_input.clearButtonMode = UITextFieldViewModeWhileEditing;
_input.backgroundColor = [UIColor clearColor];
_input.textAlignment = NSTextAlignmentCenter;
_input.returnKeyType = UIReturnKeyDone;
_input.delegate = self;
[self.view addSubview:_input];
}
#pragma mark 輸入框代理,點(diǎn)擊return 按鈕
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//判斷輸入的是否為驗(yàn)證圖片中顯示的驗(yàn)證碼
if ([_input.text isEqualToString:authCodeView.authCodeStr])
{
//正確彈出警告款提示正確
UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"恭喜您 ^o^" message:@"驗(yàn)證成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alview show];
}
else
{
//驗(yàn)證不匹配,驗(yàn)證碼和輸入框抖動(dòng)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
anim.repeatCount = 1;
anim.values = @[@-20,@20,@-20];
//? ? ? ? [authCodeView.layer addAnimation:anim forKey:nil];
[_input.layer addAnimation:anim forKey:nil];
}
return YES;
}
#pragma mark 警告框中方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//清空輸入框內(nèi)容友驮,收回鍵盤(pán)
if (buttonIndex==0)
{
_input.text = @"";
[_input resignFirstResponder];
}
}
@end