越努力.越幸福.----willingseal.
x y z
x围绕x轴摇动。y围绕y轴摇动。(加速计)
z围绕z轴摇动 (陀螺仪)
为了有自己更难好的体会,可以写一个程序放在ipad上面,按照上面的做法摇动ipad,看看都有些什么变化。这样子便于理解一些。
//摇动装置
-(void) shakeDevice {
//摇动手机的一个动画
CABasicAnimation *shake = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];
shake.fromValue = [NSNumbernumberWithFloat:-M_PI/10];
shake.toValue = [NSNumbernumberWithFloat:+M_PI/10];
shake.autoreverses =YES;//自动重播
shake.duration =0.1;//持续时间
shake.repeatCount =4;//重复次数
[self.leftImage.layeraddAnimation:shakeforKey:@"shakeAnimation"];//把shake这个基本动画加入到leftImage的图层里面
[self.rightImage.layeraddAnimation:shakeforKey:@"shakeAnimation"];
NSMutableArray *allArray = [[DatabaseOperatorsharedDatabaseOperator]allPlace];
NSLog(@"%d",[allArray
count]);
int x = arc4random_uniform([allArray
count]);//随机数
NSLog(@"--x-%d",x);
self.leftTravel = [allArray
objectAtIndex:x];
leftName.text =self.leftTravel.placeName;
leftImage.image = [UIImageimageWithData:self.leftTravel.imgData];
//int y=random()%[allArray count];//随机数
int y = arc4random_uniform([allArray
count]-1);//随机数
NSLog(@"--y-%d",y);
self.rightTravel = [allArray
objectAtIndex:y];
rightName.text =self.rightTravel.placeName;
rightImage.image = [UIImageimageWithData:self.rightTravel.imgData];
}
//成为第一响应者
-(BOOL) canBecomeFirstResponder {
returnYES;
}
//
-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type
==UIEventSubtypeMotionShake) {
//[self viewWillAppear:YES];
[selfshakeDevice];
}
}