ios开发基础知识 - 2
移动物体
1 2 3 4 5 6 7 8 | CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@ "transform.translation.x" ]; theAnimation.duration=1; theAnimation.repeatCount=2; theAnimation.autoreverses= YES ; theAnimation.fromValue=[ NSNumber numberWithFloat:0]; theAnimation.toValue=[ NSNumber numberWithFloat:-60]; [view.layer addAnimation:theAnimation forKey:@ "animateLayer" ]; |
参考:
Field Key Path |
Description |
rotation.x |
The rotation, in radians, in the x axis. |
rotation.y |
The rotation, in radians, in the y axis. |
rotation.z |
The rotation, in radians, in the z axis. |
rotation |
The rotation, in radians, in the z axis. This is identical to setting the rotation.z field. |
scale.x |
Scale factor for the x axis. |
scale.y |
Scale factor for the y axis. |
scale.z |
Scale factor for the z axis. |
scale |
Average of all three scale factors. |
translation.x |
Translate in the x axis. |
translation.y |
Translate in the y axis. |
translation.z |
Translate in the z axis. |
translation |
Translate in the x and y axis. Value is an NSSize or CGSize. |
访问别的类的属性或者方法
1 2 3 | myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; [[[appDelegate rootViewController] flipsideViewController] myMethod]; |
遍历子视图
1 2 3 4 5 | for (UIImageView *anImage in [ self .view subviews]) { if (anImage.tag == 1) { // do something } } |
播放音乐
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // 设置音乐文件路径 path = [[ NSBundle mainBundle] pathForResource:@ "mani" ofType:@ "mp3" ]; // 判断是否可以访问这个文件 if ([[ NSFileManager defaultManager] fileExistsAtPath:path]) { // 设置 player player = [[AVAudioPlayer alloc] initWithContentsOfURL: [ NSURL fileURLWithPath:path] error: nil ]; // 调节音量 (范围从0到1) player.volume = 0.4f; // 准备buffer,减少播放延时的时间 [player prepareToPlay]; // 设置播放次数,0为播放一次,负数为循环播放 [player setNumberOfLoops:-1]; [player play]; } |
随机数
rand()
例:
1 2 | [_cloud02 setFrame:CGRectMake(-1024+rand()%600, -768+rand()%300, 2048, 1536)]; [_cloud01 setFrame:CGRectMake(-1024+rand()%600, -768+rand()%300, 2048, 1536)]; |
Timer
每秒调用
1 2 3 4 5 | [ NSTimer scheduledTimerWithTimeInterval:1 target: self selector: @selector (myMethod) userInfo: nil repeats: YES ]; |
如果需要传递参数的话:
1 2 3 4 | [ NSTimer scheduledTimerWithTimeInterval:1 target: self selector: @selector (myMethod) userInfo:myObject repeats: YES ]; |
1 2 3 4 | -( void )myMethod:( NSTimer *)timer { // Now I can access all the properties and methods of myObject [[timer userInfo] myObjectMethod]; } |
停止Timer
1 2 | [myTimer invalidate]; myTimer = nil ; |
时间
1 | CFAbsoluteTime myCurrentTime = CFAbsoluteTimeGetCurrent(); |
在模拟器里测试 App 的加速度感应功能
在模拟器里可以用鼠标点击模拟 iPhone 的触摸操作,但重力感应(比如用户倾斜机身来操控飞机/赛车)怎么模拟?按住 command 再按左右方向键,就能在电脑模拟器上测试加速度感应了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2009-01-13 Silverlight Cairngorm
2007-01-13 我和未来有约会