IOS 自定义简陋的数字键盘
2016-10-23 20:39
274 查看
IOS 自定义简陋的数字键盘(纯代码实现)
1.AppDelegate.m改写- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;方法,删除info.list中Main.storyboard作为主加载启动视窗的选项。删除Main.storyboard(注意在主项目中去掉DeploymentInfo中 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
该自定义数字软剑篇,可用来做数字安全键盘,原理应该与此类似,实现可能更加复杂。
相关文章推荐
- iOS开发之UIKeyboardTypeNumberPad数字键盘自定义按键
- IOS自定义的数字键盘 FPNumberPadView
- iOS自定义数字键盘
- ios中如何自定义数字键盘
- iOS开发:自定义数字键盘(两种方式)
- iOS开发之自定义键盘(数字,字母类型等随意切换)
- ios自定义数字键盘
- iOS数字键盘自定义按键
- 为数字键盘添加自定义按钮的方法(iOS所有版本适用
- iOS开发系列之常用自定义控件开发集—自定义数字键盘控件开发
- ios键盘通知和自定义键盘
- IOS开发之自定义系统弹出键盘上方的view
- 为数字键盘添加自定义按钮的方法
- ios自定义键盘高度
- ios之键盘的自定义
- Android-自定义数字小键盘
- IOS自定义键盘
- ios键盘通知和自定义键盘
- 自定义悬浮在界面上的数字键盘
- IOS开发之自定义系统弹出键盘上方的view(转载)