布局
行列关系 列与X轴 行与Y轴
列% 行/
1.
//获取当前屏幕的宽度
CGFloat swidth = [UIScreen mainScreen].bounds.size.width;
//边界到控件的距离
int space = (swidth - 260)/2;
for (int i = 0; i<9; i++){
//创建九个按钮
//按钮与按钮之间空隙为
//space+(i%3)*(WIDTH+10)表示每列间距为10
//180+(i/3)*(WIDTH+10)表示每行间距为10
//180表示与按钮控件与屏幕边界相距的间隔
//若180+(i/3)*(WIDTH)表示每行间距为0 按钮间向挨
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(space+(i%3)*(WIDTH+10), 180+(i/3)*(WIDTH+10), WIDTH, WIDTH);
NSString *titleStr = [NSString stringWithFormat:@"%d", i+1];
[btn setTitle:titleStr forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
2.
for (int i = 0; i < 80; i++) {
//创建label
//每行八个小球
//10+(i%8)*45表示每列 间距为45-40=5
//80+(i/8)*45表示每行
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(10+(i%8)*45, 80+(i/8)*45, 40, 40)];
NSString *title = [NSString stringWithFormat:@"%d",i+1];
lb.text = title;
//如想label成圆形其宽高相等 圆角lb.layer.cornerRadius = 宽度/2
lb.layer.cornerRadius = 20;
//设置边界
lb.layer.borderColor = [UIColor blackColor].CGColor;
lb.layer.borderWidth = 1;
lb.clipsToBounds = YES;
lb.backgroundColor = [UIColor cyanColor];
lb.textAlignment = NSTextAlignmentCenter;
lb.font = [UIFont systemFontOfSize:29];
lb.textColor = [UIColor blueColor];
[self.view addSubview:lb];
}
3.
#import "AppDelegate.h"
@interface AppDelegate ()
//类目
@property(nonatomic,strong)NSArray*app;
@end
@implementation AppDelegate
{
NSInteger lastNum;
UILabel*label;
UIButton*appbutton;
}
//懒加载
-(NSArray *)app{
if (_app == nil) {
//获取文件资源
NSString*path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
_app=[NSArray arrayWithContentsOfFile:path];
}
return _app;
}
-(void)dealloc{
self.window = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//创建uilablel
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 400, 50)];
label.backgroundColor=[UIColor brownColor];
label.text=@"0";
label.textAlignment = NSTextAlignmentCenter;
[self.window addSubview:label];
//创建uiview
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 100, self.window.frame.size.width, self.window.frame.size.height)];
view.backgroundColor = [UIColor whiteColor];
[self.window addSubview:view];
int totalloc=4;
CGFloat appvieww=90;
CGFloat appviewh=90;
CGFloat margin=(self.window.frame.size.width-totalloc*appvieww)/(totalloc+1);
for (int i=0; i<self.app.count; i++) {
//NSLog(@"====");
int row=i/totalloc;//行号
int loc=i%totalloc;//列号
CGFloat appviewx=margin+(margin+appvieww)*loc;
CGFloat appviewy=margin+(margin+appviewh)*row;
//创建按钮文本
appbutton=[UIButton buttonWithType:UIButtonTypeCustom];
//N*N格创建
appbutton.frame=CGRectMake(appviewx, appviewy, appvieww, appviewh);
if ((loc == 3 || row == 3) && loc !=0) {
appbutton.backgroundColor = [UIColor grayColor];
}
//1修改按钮边界
[appbutton.layer setMasksToBounds:YES];
//圆形按钮设置
appbutton.layer.cornerRadius = (appbutton.frame.size.width+10)/4;
//2修改按钮边界
[appbutton.layer setBorderWidth:1.0];
//将plist文本信息充当按钮标题
[appbutton setTitle:self.app[i] forState:UIControlStateNormal];
[appbutton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
//设置按钮文字大小即对其方式
appbutton.titleLabel.font = [UIFont systemFontOfSize:30];
appbutton.titleLabel.textAlignment = NSTextAlignmentCenter;
appbutton.tag = i + 100;
//添加事件
[appbutton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//最后将按钮加入到指定视图
[view addSubview:appbutton];
}
return YES;
}
4.
5.
定时器
//定时器 .5表示毫秒 5表示秒
[NSTimer scheduledTimerWithTimeInterval:9 target:self selector:@selector(onExpired) userInfo:nil repeats:YES];
6.
//创建颜色数组
NSArray *colorArray = @[[UIColor redColor], [UIColor purpleColor], [UIColor magentaColor], [UIColor brownColor], [UIColor orangeColor]];
//self.view.subviews表示一数组存放的都是UILabel对象
for (UILabel *lb in self.view.subviews) {
//随机
lb.backgroundColor = colorArray[arc4random()%colorArray.count];
[UIView animateWithDuration:1.5 animations:^{
//Label 位置随机
lb.frame = CGRectMake(arc4random()%370, arc4random()%680, 40, 40);
}];
}
7.
计算器-记录键盘输入的数值
- (void)onClickedBtn:(UIButton *)sender
{
//YES, ON
//按钮上得文本
NSLog(@"%@", sender.titleLabel.text);
if ([_display.text isEqualToString:@"0"]) {
_display.text = sender.titleLabel.text;
} else {
//创建一个可变字符
NSMutableString *displayStr = [NSMutableString stringWithString:_display.text];
//在字符串后面追加一个字符
[displayStr appendString:sender.titleLabel.text];
_display.text = displayStr;
}
}
8.
计算器单个字符删除
- (IBAction)shancu:(id)sender {
//创建一个可变字符来存放label.text数值
NSMutableString *ms = [NSMutableString stringWithString:_display.text];
//当label.text上只显示1个字符
if (ms.length == 1) {
_display.text = @"0";
} else {
//字符串截取
NSString *newStr = [ms substringToIndex:(ms.length-1)];
}
NSString *newStr = [ms substringToIndex:(ms.length-1)];
_display.text = newStr;
}
9.
10.
11.
12.
迷离小球案例
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (int i = 0; i < 80; i++) {
//创建label
//每行八个小球
//10+(i%8)*45表示每列
//80+(i/8)*45表示每行
UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(10+(i%8)*45, 80+(i/8)*45, 40, 40)];
NSString *title = [NSString stringWithFormat:@"%d",i+1];
lb.text = title;
//如想label成圆形其宽高相等 圆角lb.layer.cornerRadius = 宽度/2
lb.layer.cornerRadius = 20;
//设置边界
lb.layer.borderColor = [UIColor blackColor].CGColor;
lb.layer.borderWidth = 1;
lb.clipsToBounds = YES;
lb.backgroundColor = [UIColor cyanColor];
lb.textAlignment = NSTextAlignmentCenter;
lb.font = [UIFont systemFontOfSize:29];
lb.textColor = [UIColor blueColor];
[self.view addSubview:lb];
}
//定时器.5表示毫秒 5表示秒
[NSTimer scheduledTimerWithTimeInterval:9 target:self selector:@selector(onExpired) userInfo:nil repeats:YES];
}
- (void)onExpired
{
//创建颜色数组
NSArray *colorArray = @[[UIColor redColor], [UIColor purpleColor], [UIColor magentaColor], [UIColor brownColor], [UIColor orangeColor]];
//self.view.subviews表示一数组存放的都是UILabel对象
for (UILabel *lb in self.view.subviews) {
//随机
lb.backgroundColor = colorArray[arc4random()%colorArray.count];
[UIView animateWithDuration:1.5 animations:^{
lb.frame = CGRectMake(arc4random()%370, arc4random()%680, 40, 40);
}];
}
}
13
计算机
#import "ViewController.h"
#define WIDTH 80
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *display;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取当前屏幕的宽度
CGFloat swidth = [UIScreen mainScreen].bounds.size.width;
//边界到控件的距离
int space = (swidth - 260)/2;
for (int i = 0; i<9; i++){
//创建九个按钮
//按钮与按钮之间空隙为
//space+(i%3)*(WIDTH+10)表示每列
//180+(i/3)*(WIDTH+10)表示每行
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(space+(i%3)*(WIDTH+10), 180
+(i/3)*(WIDTH+10), WIDTH, WIDTH);
NSString *titleStr = [NSString stringWithFormat:@"%d", i+1];
[btn setTitle:titleStr forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//给btn添加背景图片。在点击 高亮状态下才显示
[btn setBackgroundImage:[UIImage imageNamed:@"11"] forState:UIControlStateHighlighted];
//超过当前view就剪切掉
btn.clipsToBounds = YES;
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//修改文字字体即大小
btn.titleLabel.font = [UIFont systemFontOfSize:24];
btn.backgroundColor = [UIColor clearColor];
btn.layer.cornerRadius = WIDTH/2;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1;
[btn addTarget:self action:@selector(onClickedBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)onClickedBtn:(UIButton *)sender
{
//YES, ON
//按钮上得文本
NSLog(@"%@", sender.titleLabel.text);
if ([_display.text isEqualToString:@"0"]) {
_display.text = sender.titleLabel.text;
} else {
//当label上的数值不为零
//创建一个可变字符来存放label.text
NSMutableString *displayStr = [NSMutableString stringWithString:_display.text];
//在字符串后面追加一个字符
[displayStr appendString:sender.titleLabel.text];
_display.text = displayStr;
}
}
//label.text--
- (IBAction)shancu:(id)sender {
//创建一个可变字符来存放label.text数值
NSMutableString *ms = [NSMutableString stringWithString:_display.text];
//当label.text上只显示不为0个字符
if (ms.length == 1) {
_display.text = @"0";
} else {
//字符串截取
NSString *newStr = [ms substringToIndex:(ms.length-1)];
}
NSString *newStr = [ms substringToIndex:(ms.length-1)];
_display.text = newStr;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
14
浮动球
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//NSLog(@"%@",NSHomeDirectory());
// CGRect rec = [UIScreen mainScreen].bounds;
//NSLog(@"rec:%@",NSStringFromCGRect(rec));
//UIView视图
UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 100, 100)];
v1.backgroundColor = [UIColor clearColor];
v1.clipsToBounds = YES;
v1.layer.borderColor = [UIColor blackColor].CGColor;
v1.layer.borderWidth = 1;
v1.layer.cornerRadius = 50;
[self.view addSubview:v1];
UIView *v2 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 100, 100)];
v2.backgroundColor = [UIColor redColor];
[v1 addSubview:v2];
[UIView animateWithDuration:2 animations:^{
//v1移动
v1.frame = CGRectMake(200, 300, 100, 100);
v2.alpha = 0.1;
v2.frame = CGRectMake(0, 100, 100, 1);
} completion:^(BOOL finished) {
v1.alpha = 1;
[UIView animateWithDuration:1 animations:^{
v2.frame = CGRectMake(0, 0, 100, 100);
}];
}];
// [self.view addSubview:v1];
// v1.frame = CGRectMake(100, 200, 100, 200);
// v1.alpha = 0.5;//透明度:从0~1;
//
// UIView *v2 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 60)];
// v2.backgroundColor = [UIColor redColor];
// [self.view addSubview:v2];
// v2.alpha = 1;//透明度会传递到子视图
// v2.clipsToBounds = YES;
// self.view.backgroundColor = [UIColor cyanColor];
//
// UIView *v3 = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 60, 60)];
// v3.backgroundColor = [UIColor greenColor];
// v3.layer.cornerRadius = 30;
// v3.layer.borderColor = [UIColor yellowColor].CGColor;
// v3.layer.borderWidth = 1;
// [v2 addSubview:v3];
// v3.alpha = .5;//透明度:从0~1;
// NSLog(@"subviews:%@", self.view.subviews);
//
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end