IOS開發學習筆記Day4-IOC基礎一
控件的常用屬性和方法
NSLog(@"view的父控件:%@----控制器的view:%@", self.greenView.superview, self.view);
NSLog(@"view的子控件%@", self.greenView.subviews);
NSLog(@"控制器的view的子控件%@", self.view.subviews);
NSLog(@"控制器的view的父控件-----%@", self.view.superview);//在viewDidAppear方法中纔可以拿到
根據tag拿到對應的view
UIView *redView = [self.view viewWithTag:1];
加到控制器的view中
[self.view addSubview:sw];
移除kongjian
[self.view removeFromSuperview];
基本創建方式
// 創建UILabel對象
UILabel *label = [[UILabel alloc] init];
// 設置frame (位置和尺寸)
label.frame = CGRectMake(100, 100, 100, 60);
// 設置背景顏色
label.backgroundColor = [UIColor yellowColor];
// 改變尺寸 iOS9以後, 中心點不變,向四周延伸
self.label.bounds = CGRectMake(0, 0, 200, 120);
改變控件的frame
// 方式1
// self.label.frame = CGRectMake(200, 100, 100, 60);
// 方式2
self.label.frame = (CGRect){{100, 100}, {100, 100}};
// 方式3
// 結構體是值傳遞,不是地址傳遞
// self.label.frame.size.width += 100;
CGRect frame = self.label.frame;
// frame.origin.x -= 100; // 改變x值
// frame.origin.y += 100; // 改變y值
// frame.size.width += 50; // 改變寬度
frame.size.height += 100; // 改變高度
self.label.frame = frame;
讓控件居中
// self.label.center = CGPointMake(100, 100);
// 顯示在最中間
self.label.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
監聽器中tag的使用
不需要設置屬性方式即可監聽。
switch (button.tag) {
case 3:
NSLog(@"點擊了按鈕1");
break;
case 4:
NSLog(@"點擊了按鈕2");
break;
case 5:
NSLog(@"點擊了按鈕3");
break;
default:
break;
}
UILabel
// 1.1 創建UILabel對象
UILabel *label = [[UILabel alloc] init];
// 1.2 設置frame
label.frame = CGRectMake(100, 100, 202, 175);
// 1.3 設置背景顏色
label.backgroundColor = [UIColor redColor];
// 1.4 設置文字
label.text = @"da shen 11期最牛逼!!!!da shen da shen da shen da shen da shen ";
// 1.5 居中
label.textAlignment = NSTextAlignmentCenter;
// 1.6 設置字體大小
label.font = [UIFont systemFontOfSize:20.f];
label.font = [UIFont boldSystemFontOfSize:25.f];
label.font = [UIFont italicSystemFontOfSize:20.f];
// 1.7 設置文字的顏色
label.textColor = [UIColor whiteColor];
// 1.8 設置陰影(默認是有值)
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(-2, 1);
// 1.9 設置行數(0:自動換行)
label.numberOfLines = 1;
// 1.10 顯示模式
label.lineBreakMode = NSLineBreakByTruncatingHead;
/*
NSLineBreakByWordWrapping = 0, // 單詞包裹,換行的時候會以一個單詞換行
NSLineBreakByCharWrapping, // 字符包裹換行,換行的時候會以一個字符換行
NSLineBreakByClipping, // 裁剪超出的內容
NSLineBreakByTruncatingHead, // 一行中頭部省略(注意:numberOfLines要爲1): "...wxyz"
NSLineBreakByTruncatingTail, // 一行中尾部省略: "abcd..."
NSLineBreakByTruncatingMiddle // 一行中中間部省略: "ab...yz"
*/
UIImageView
// 1.1 創建UIImageView對象
UIImageView *imageView = [[UIImageView alloc] init];
// 1.2 設置frame
imageView.frame = CGRectMake(100, 100, 250, 200);
// 1.3 設置背景
// imageView.backgroundColor = [UIColor greenColor];
// 1.4 設置圖片 (png不需要後綴)
imageView.image = [UIImage imageNamed:@"1"];
/**
UIViewContentModeRedraw, // 重新繪製 (核心繪圖) drawRact
//帶有Scale,標明圖片有可能被拉伸或壓縮
UIViewContentModeScaleToFill, // 完全的壓縮或拉伸
// Aspect 比例,縮放是帶有比例的
UIViewContentModeScaleAspectFit, // 寬高比不變 Fit 適應
UIViewContentModeScaleAspectFill, // 寬高比不變 Fill 填充
//不帶有Scale,標明圖片不可能被拉伸或壓縮
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
*/
// 1.5 設置圖片的內容模式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 2.0 加到控制器的view中
[self.view addSubview:imageView];
// 裁剪多餘的部分
imageView.clipsToBounds = YES;
UIImageView毛玻璃練習
// 1.創建UIImageView對象
UIImageView *imageView = [[UIImageView alloc] init];
// 2. 設置尺寸
// imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
imageView.frame = self.view.bounds;
// 3. 設置背景顏色
imageView.backgroundColor = [UIColor redColor];
// 4. 設置背景圖片
imageView.image = [UIImage imageNamed:@"1"];
// 5.設置圖片的內容模式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 6.加毛玻璃
// 6.1 創建UIToolBar對象
UIToolbar *toolBar = [[UIToolbar alloc] init];
// 6.2 設置toolBar的frame
toolBar.frame = imageView.bounds;
// 6.3 設置毛玻璃的樣式
toolBar.barStyle = UIBarStyleBlack;
toolBar.alpha = 0.98;
// 6.4 加到imageView中
[imageView addSubview:toolBar];
// 加到控制器的view中
[self.view addSubview:imageView];
UIImageView的frame設置
// 設置frame的方式
// 方式一
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"1"];
// imageView.frame = CGRectMake(100, 100, 267, 400);
// imageView.frame = (CGRect){{100, 100},{267, 400}};
*/
// 方式二
/*
UIImageView *imageView = [[UIImageView alloc] init];
// 創建一個UIImage對象
UIImage *image = [UIImage imageNamed:@"1"];
// 設置frame
imageView.frame = CGRectMake(100, 10, image.size.width, image.size.height);
// 設置圖片
imageView.image = image;
*/
// 方式三
/*
// 創建一個UIImage對象
UIImage *image = [UIImage imageNamed:@"1"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, image.size.width, image.size.height)];
imageView.image = image;
// 方式四
// 創建一個UIimageview對象
// 注意: initWithImage 默認就有尺寸--->圖片的尺寸
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
// 改變位置
// imageView.center = CGPointMake(200, 150);
imageView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
[self.view addSubview:imageView];
UIImageView練習-幀動畫
#pragma mark - 開始動畫
- (IBAction)startAnimation {
// 1.1 加載所有的圖片
NSMutableArray<UIImage *> *imageArr = [NSMutableArray array];
for (int i=0; i<20; i++) {
// 獲取圖片的名稱
NSString *imageName = [NSString stringWithFormat:@"%d", i+1];
// 創建UIImage對象
UIImage *image = [UIImage imageNamed:imageName];
// 加入數組
[imageArr addObject:image];
}
// 設置動畫圖片
self.imageView.animationImages = imageArr;
// 設置動畫的播放次數
self.imageView.animationRepeatCount = 0;
// 設置播放時長
// 1秒30幀, 一張圖片的時間 = 1/30 = 0.03333 20 * 0.0333
self.imageView.animationDuration = 1.0;
// 開始動畫
[self.imageView startAnimating];
}
#pragma mark - 結束動畫
- (IBAction)overAnimation {
[self.imageView stopAnimating];
}
第二種方式
// 1.加載所有的圖片
NSMutableArray<UIImage *> *standImages = [NSMutableArray array];
for (int i=0; i<10; i++) {
// 獲取所有圖片的名稱
NSString *imageName = [NSString stringWithFormat:@"stand_%d", i+1];
// 創建UIImage
UIImage *image = [UIImage imageNamed:imageName];
// 裝入數組
[standImages addObject:image];
}
*/
// 2.設置動畫圖片
self.imageView.animationImages = self.standImages;
/**
* 加載所有的圖片
*
* @param imagePrefix 名稱前綴
* @param count 圖片的總個數
*/
- (NSArray *)loadAllImagesWithimagePrefix:(NSString *)imagePrefix count:(int)count{
NSMutableArray<UIImage *> *images = [NSMutableArray array];
for (int i=0; i<count; i++) {
// 獲取所有圖片的名稱
NSString *imageName = [NSString stringWithFormat:@"%@_%d",imagePrefix, i+1];
// 創建UIImage
UIImage *image = [UIImage imageNamed:imageName];
// 裝入數組
[images addObject:image];
}
return images;
}
// 3.設置播放次數
self.imageView.animationRepeatCount = 0;
// 4.設置播放的時長
self.imageView.animationDuration = 0.6;
// 5.播放
[self.imageView startAnimating];
第三種方式
self.standImages = [self loadAllImagesWithimagePrefix:@"stand" count:10];
/**
* 加載所有的圖片
* @param imagePrefix 名稱前綴
* @param count 圖片的總個數
*/
- (NSArray *)loadAllImagesWithimagePrefix:(NSString *)imagePrefix count:(int)count{
NSMutableArray<UIImage *> *images = [NSMutableArray array];
for (int i=0; i<count; i++) {
// 獲取所有圖片的名稱
NSString *imageName = [NSString stringWithFormat:@"%@_%d",imagePrefix, i+1];
// 創建UIImage
UIImage *image = [UIImage imageNamed:imageName];
// 裝入數組
[images addObject:image];
}
return images;
}
UIImageView加載方式
加載圖片的方式:
- imageNamed
- 就算指向它的指針被銷燬,該資源也不會被從內存中幹掉
- 放到Assets.xcassets的圖片,默認就有緩存
- 圖片經常被使用
- imageWithContentsOfFile:
- 指向它的指針被銷燬,該資源會被從內存中幹掉
- 放到項目中的圖片就不由緩存
- 不經常用,大批量的圖片
-
加載Assets.xcassets這裏面的圖片:
1> 打包後變成Assets.car
2> 拿不到路徑
3> 只能通過imageNamed:來加載圖片
4> 不能通過imageWithContentsOfFile:來加載圖片 -
放到項目中的圖片:
1> 可以拿到路徑
2> 能通過imageNamed:來加載圖片
3> 也能通過imageWithContentsOfFile:來加載圖片
// 方式一
// self.imageView.image = [UIImage imageNamed:@"1"];
// 方式二
// 路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
self.imageView.image = [UIImage imageWithContentsOfFile:path];
播放音頻實戰
- (void)viewDidLoad {
[super viewDidLoad];
// 1.加毛玻璃
UIToolbar *toolbar = [[UIToolbar alloc] init];
// 2. 設置frame
toolbar.frame = self.bgImageView.bounds;
// 3. 設置樣式和透明度
toolbar.barStyle = UIBarStyleBlack;
toolbar.alpha = 0.98;
// 4.加到背景圖片上
[self.bgImageView addSubview:toolbar];
// 5.創建播放器
/*
NSString *path = [[NSBundle mainBundle] pathForResource:@"mySong1.mp3" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
*/
// 資源的URL地址
NSURL *url = [[NSBundle mainBundle] URLForResource:@"mySong1.mp3" withExtension:nil];
// 創建播放器曲目
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
// 創建播放器
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
//self.player.rate = 2.0;//調節速率
}
播放邏輯控制
- (IBAction)playOrPause:(UIButton *)button {
switch (button.tag) {
case 3:
[self.player play]; // 播放
break;
case 4:
[self.player pause]; // 暫停
break;
default:
break;
}
}
切歌
- (IBAction)changeMusic:(UIButton *)button {
// 歌曲的名稱
NSString *musicName = nil;
switch (button.tag) {
case 1:// 上一首
musicName = @"mySong2.mp3";
break;
case 2:// 下一首
musicName = @"mySong3.mp3";
break;
default:
break;
}
NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
[self.player replaceCurrentItemWithPlayerItem:playerItem];
// 播放
[self.player play];
}
延遲執行某方法;
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
UIAlertView
// 創建對象
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"輸入有誤" message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
//顯示
[alertView show];