移动物体
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. |
访问别的类的属性或者方法
myAppDelegate *appDelegate
= (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[[[appDelegate rootViewController] flipsideViewController] myMethod];
遍历子视图
for (UIImageView *anImage in [self.view subviews]) {
if (anImage.tag == 1) {
// do something
}
}
播放音乐
// 设置音乐文件路径
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()
例:
[_cloud02 setFrame:CGRectMake(-1024+rand()%600, -768+rand()%300, 2048, 1536)];
[_cloud01 setFrame:CGRectMake(-1024+rand()%600, -768+rand()%300, 2048, 1536)];
Timer
每秒调用
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(myMethod)
userInfo:nil
repeats:YES];
如果需要传递参数的话:
[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(myMethod)
userInfo:myObject
repeats:YES];
-(void)myMethod:(NSTimer*)timer {
// Now I can access all the properties and methods of myObject
[[timer userInfo] myObjectMethod];
}
停止Timer
[myTimer invalidate];
myTimer = nil;
时间
CFAbsoluteTime myCurrentTime = CFAbsoluteTimeGetCurrent();
在模拟器里测试 App 的加速度感应功能
在模拟器里可以用鼠标点击模拟 iPhone 的触摸操作,但重力感应(比如用户倾斜机身来操控飞机/赛车)怎么模拟?按住 command 再按左右方向键,就能在电脑模拟器上测试加速度感应了。