寒假至今才出去一天和最好的朋友吃吃吃 然后就得继续回来学习 嗯…我可能过了一个假的寒假
昨天深入的学习了UITextField..UILabel..UIImage..利用UIImageView和UIToolBar 生成一个毛玻璃的背景..利用UIImageView和NSMutableArray创建动画 以及AVFoundation/AVFoundation.h中的AVplayer
利用UIButton UILabel UITextField 做一个简易的加法计算器demo 核心代码:
- (IBAction)getResult { NSString *num1 = self.numberOne.text; NSString *num2 = self.numberTwo.text; //用两个字符串 num1和num2监听两个TextField用户所输入的 //数值 if (num1.length ==0 ||num2.length ==0) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"缺少输入数字" message:@"请补充完全部数字" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { nil; }]]; /*UIAlertController 是一个弹窗的方法.alertControllerWithTitle是弹窗的标题 message是弹窗的内容 addAction:则是添加一个按钮.该方法是通过阅读其他博客所学的 */ [self presentViewController:alertController animated:YES completion:nil]; //当前页面添加创建的弹窗对象 } int res = [num1 intValue] + [num2 intValue]; self.result.text = [NSString stringWithFormat:@"%d",res]; } demo截图 虚拟键盘 在UITextField控件的显示属性检查器中有一个是Keyboard Type 可以选择虚拟键盘的类型 然后在模拟器中通过command+K调出虚拟键盘demo截图
原图
效果图
想用AVPlayer需要调用头文件AVFoundation/AVFoundation.h 在上面的demo的基础上通过修改变量和添加控件制作一个简易播放器的demo,核心代码如下:
@interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageview; @property (nonatomic, strong) AVPlayer *player; //创建一个AVPlayer的全局变量对象 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self groundGlass]; [self playwithitem]; } -(void)playwithitem{ NSURL *url = [[NSBundle mainBundle]URLForResource:@"mySong1.mp3" withExtension:nil]; AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url]; self.player = [[AVPlayer alloc]initWithPlayerItem:item]; /* 创建一个AVPlayerItem对象并读取音乐文文件 初始化AVPlayer 传入AVPlayerItem对象获取的文件文件 并赋值给全局变量对象 */ } (IBAction)nextOrLast:(UIButton *)sender { // 通过4个按键 关联2个代码块 通过各自的Tag来区别各自的功能 NSString *musicName = nil; switch (sender.tag) { case 1: musicName = @"mySong2.mp3"; break; //上一首 case 4: musicName = @"mySong3.mp3"; break; //下一首 default: break; } NSURL *url = [[NSBundle mainBundle]URLForResource:musicName withExtension:nil]; AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url]; //获取上一首或下一首的歌名并传入AVPlayerItem对象 [self.player replaceCurrentItemWithPlayerItem:item]; //将获取到歌名的AVPlayerItem对象传给更换歌曲的方法 [self.player play]; //播放音乐 } - (IBAction)playOrPause:(UIButton *)sender { switch (sender.tag) { case 2: [self.player pause]; break; //暂停 case 3: [self.player play]; break; //播放 default: break; } } @end demo截图通过AVPlayer的学习,我学到一种写OC代码的方法:就是缺啥补啥法 例如
AVPlayer *palyer1 = [AVPlayer alloc]initWithPlayerItem:<#(nullable AVPlayerItem *)#>] /* 上面代码需要你传入一个AVPlayerItem的对象,这个时候我们就创建一个AVPlayerItem的对象 */ AVPlayerItem *item =[AVPlayerItem playerItemWithURL:<#(nonnull NSURL *)#>] /* 上面代码需要你传入一个XVPlayerItem的对象,这个时候我们就创建一个NSURL的对象 */ ... /* 反正 需要我们传入什么 我们除了可以commond 点击类名去查找方法 还可以通过上述方法编写代码 */通过UIImage..NSMutableArray类来制作一个简易拳皇的帧动画demo 核心代码如下:
- (void)loadingWithCount:(int)count andName:(NSString *)name andDuration:(NSTimeInterval)duration andRepeatCount:(int)Rcount{ NSMutableArray <UIImage *> *imageArr = [[NSMutableArray alloc]init]; //创建一个具有<UIImage *>协议的可变数组来接收所有图片 for (int i = 0; i < count; ++i) { NSString *path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@_%d",name,i+1] ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; [imageArr addObject:image]; //将遍历出来的图片传入图片可变数组 } self.imageView.animationImages = imageArr; //将图片数组赋值给动画播放图片 self.imageView.contentMode = UIViewContentModeScaleAspectFit; self.imageView.animationRepeatCount =Rcount; //播放次数 self.imageView.animationDuration = duration; //播放时长 [self.imageView startAnimating]; //开始播放 - (IBAction)Mid{ [self loadingWithCount:39 andName:@"xiaozhao3" andDuration:2.4f andRepeatCount:1]; } - (IBAction)Max{ [self loadingWithCount:87 andName:@"dazhao" andDuration:5.8f andRepeatCount:1]; } - (void)initalization { [self loadingWithCount:9 andName:@"stand" andDuration:0.6 andRepeatCount:0]; } - demo截图  内存管理 //这里与之前的传入图片方法有所不同之前是用了 UIImage *image = [UIImage imageNamed:@"xxx"] //而这里用的是 NSString *path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@_%d",name,i+1] ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; /* 图片也不是传入到Assets.xcassets,而是加载到Supporting Files 这样做的好处是当把强指针赋值为nil时,会释放掉当前对象的运行内存 而用 UIImage *image = [UIImage imageNamed:@"xxx"]并把图片放置Assets.xcassets会生成一个Assets.car,即使你把强指针赋值为nil,占用内存依旧没有变化 */以上是第二天的学习,如果有什么疑问或者错误以及宝贵的意见欢迎提出,吃个午饭继续第三天的学习,嘿嘿嘿.
