您的位置:首页 > 移动开发 > IOS开发

IOS 自定义简陋的数字键盘

2016-10-23 20:39 274 查看

IOS 自定义简陋的数字键盘(纯代码实现)

1.AppDelegate.m改写- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;方法,删除info.list中Main.storyboard作为主加载启动视窗的选项。删除Main.storyboard(注意在主项目中去掉Deployment
Info中 Main interface项为空)

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [[ViewController alloc] init];

    [self.window setBackgroundColor:[UIColor whiteColor]];

    [self.window makeKeyAndVisible];

    return YES;

}

2.ViewController.h定义

#import <UIKit/UIKit.h&g
4000
t;

@interface ViewController : UIViewController

{

    NSString * _logStr;

}

@end

3.ViewController.m实现文件

//

//  ViewController.m

//  securityKeyboard

//

//  Created by LKMacBookPro on 16/10/22.

//  Copyright © 2016年 apple. All rights reserved.

//

#import "ViewController.h"

#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.view setBackgroundColor:[UIColor whiteColor]];

    

    UITextField * uname = [[UITextField alloc] init];

    uname.frame = CGRectMake(100, 100, 200, 30);

    uname.tag = 301;

    uname.borderStyle = UITextBorderStyleLine;

//    [uname respondsToSelector:@selector(autoFocus)];

    uname.delegate = self;

    [self.view addSubview:uname];

    

   

    [self myKeyBoard];

    

    _logStr = @"";

    

}

-(void) myKeyBoard{

    UIView * subview = [[UIView alloc] init];

    subview.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 200);

    subview.backgroundColor = [UIColor blueColor];

    

    for (int i = 0; i < 3; i++) {

        for (int j = 0; j < 3; j++) {

            UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

            btn.frame = CGRectMake(30+j*128, 2.5+i*50, 98, 40);

            btn.backgroundColor = [UIColor blackColor];

            [btn setTitle:[NSString stringWithFormat:@"%d",i*3+j+1] forState:UIControlStateNormal];

            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            btn.tag = 101+i*3+j;

            [btn addTarget:self action:@selector(pressDown:) forControlEvents:UIControlEventTouchUpInside];

            [subview addSubview:btn];

        }

    }

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(30+128, 152.5, 98, 40);

    btn.backgroundColor = [UIColor blackColor];

    [btn setTitle:[NSString stringWithFormat:@"%d",0] forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    btn.tag = 100;

    [btn addTarget:self action:@selector(pressDown:) forControlEvents:UIControlEventTouchUpInside];

    [subview addSubview:btn];

    subview.tag = 1001;

    [self.view addSubview:subview];

    

}

-(void) pressDown:(UIButton *)btn{

    NSLog(@"Button%ld is pressed!",btn.tag - 100);

    UITextField * uname = (UITextField *)[self.view viewWithTag:301];

    _logStr = [_logStr stringByAppendingString:[NSString stringWithFormat:@"%ld",btn.tag - 100]];

    uname.text = _logStr;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void) textFieldDidBeginEditing:(UITextField *)textField{

    NSLog(@"sdfe");

    UIView * view = [self.view viewWithTag:1001];

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.5];

    view.hidden = NO;

    view.frame = CGRectMake(0, ScreenHeight/2, ScreenWidth, 200);

    [UIView commitAnimations];

}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    UITouch * tch = [touches anyObject];

    

    UIView * view = [self.view viewWithTag:1001];

    CGPoint pt =  [tch locationInView:view]; //获取手指触摸坐标

//    NSLog(@"%f,%f",pt.x,pt.y);

    if(pt.y < 0 || pt.y > 200){

        [UIView beginAnimations:nil context:nil];

        [UIView setAnimationDuration:0.5];

        view.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 200);

        view.hidden = YES;

        [UIView commitAnimations];

//强制转换

        UITextField * uname = (UITextField *)[self.view viewWithTag:301];

        [uname resignFirstResponder];

        uname.text = @"";

        _logStr = @"";

    }

}

@end

该自定义数字软剑篇,可用来做数字安全键盘,原理应该与此类似,实现可能更加复杂。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: