標籤:
1、計時器的使用
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(flyAction) userInfo:nil repeats:YES];
2、隨機數的使用
arc4random()
3、UIWindow部分代碼(xib)
//UIScreen
CGRect rect = [UIScreen mainScreen].bounds;
//建立UIWindow
self.window = [[UIWindow alloc] initWithFrame:rect];
//設定背景顏色
self.window.backgroundColor = [UIColor blackColor];
//設定keyWindow,並使其可見
[self.window makeKeyAndVisible];
//添加主控制器
ViewController * vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
4、UIView部分代碼
//將v置為最前端
[self.view bringSubviewToFront:v];
//將v置為最後端
[self.view sendSubviewToBack:v];
//從父視圖刪除子視圖
[v removeFromSuperview];
5、計算Label的高度(核心代碼)
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
6、自適應(核心代碼)
(1)sizeToFit
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 17, 0)];
label.text = string;
label.numberOfLines = 0;
//使用sizeToFit一定要設定寬度
[label sizeToFit];
(2)sizeThatFits:
UILabel * label = [[UILabel alloc] init];
label.text = string;
label.numberOfLines = 0;
label.backgroundColor = [UIColor greenColor];
CGSize size = [label sizeThatFits:CGSizeMake(self.view.frame.size.width - 40, MAXFLOAT)];
label.frame = CGRectMake(20, 50, size.width, size.height);
[self.view addSubview:label];
7、UIImage部分代碼
(1)本目錄下的位置
//Resource:檔案名稱
//ofType:檔案名稱尾碼
//inDirectory:Bundle下的目錄
NSString * path =[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist" inDirectory:@"image"];
(2)簡易展開
UIImage* image = [UIImage imageNamed:@"backgroundImage”];//有緩衝
UIImageView* imageView =[[UIImageView alloc]initWithImage:image];
UIEdgeInsets inset = UIEdgeInsetsMake(6 , 6, 6, 6);
image = [image resizableImageWithCapInsets:inset resizingMode:UIImageResizingModeStretch];
imageView.image = image;
imageView.frame = CGRectMake(20, 20, self.view.frame.size.width-40, 30);
[self.view addSubview:imageView];
(3)圓切
imageView.layer.cornerRadius = 10
//隱藏圓外的東西
imageView.layer.masksToBounds = YES;、//隱藏
imageView.clipsToBounds = YES;//切掉
//設定邊線寬度
imageView.layer.borderWidth = 4 ;
//設定邊線顏色
imageView.layer.borderColor = [UIColor blackColor].CGColor;
//設定陰影顏色
imageView.layer.shadowColor = [UIColor greenColor].CGColor;
//設定陰影大小
imageView.layer.shadowOffset = CGSizeMake(2, 2);
//設定不透明度
imageView.layer.shadowOpacity = 0.3;
(4)動畫
NSMutableArray * array = [NSMutableArray array];
for (int i = 1; i < 6; i++) {
NSString * string = [NSString stringWithFormat:@"hehua0%d",i];
UIImage * image = [UIImage imageNamed:string];
[array addObject:image];
}
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
//設定動畫圖片
imageView.animationImages = array;
//動畫時間
imageView.animationDuration = 1;
//動畫播放重複次數,值為0時,無限迴圈
imageView.animationRepeatCount = 0;
//開始動畫
[imageView startAnimating];
[self.view addSubview:imageView];
8、UIButton部分代碼
(1)Button實現圖上字下(核心代碼)
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView.contentMode = UIViewContentModeBottom;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:20];
}
return self;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
CGFloat ponitX = 0;
CGFloat ponitY = 0;
CGFloat width = contentRect.size.width;
CGFloat height = contentRect.size.height * kScale;
return CGRectMake(ponitX, ponitY, width, height);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
CGFloat ponitX = 0;
CGFloat ponitY = contentRect.size.height * kScale;
CGFloat width = contentRect.size.width;
CGFloat height = contentRect.size.height * (1 - kScale);
return CGRectMake(ponitX, ponitY, width, height);
}
9、UITextField部分代碼
(1)第一響應(鍵盤彈入彈出)
//進行第一響應
[self.view addSubview:textField];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//取消第一響應
[self.textField resignFirstResponder];
//結束編輯
[self.textField endEditing:YES];
}
iOS學習day6