Iphone 用加速度计移动两个物体的核心运动
我正试图使用xcode中的加速计移动两个按钮。当我调用startDrifting时,第一个按钮工作,但是当我调用startDrifting 2作为第二个按钮时,第二个按钮没有发生任何事情。我尝试调用startDrifting2的startAccelerometerUpdatesToQueue,但第一个停止工作。有什么想法吗Iphone 用加速度计移动两个物体的核心运动,iphone,xcode,accelerometer,core-motion,Iphone,Xcode,Accelerometer,Core Motion,我正试图使用xcode中的加速计移动两个按钮。当我调用startDrifting时,第一个按钮工作,但是当我调用startDrifting 2作为第二个按钮时,第二个按钮没有发生任何事情。我尝试调用startDrifting2的startAccelerometerUpdatesToQueue,但第一个停止工作。有什么想法吗 -(void) startDrifting:(UIButton *) button { [self.motionManager startAccelerometer
-(void) startDrifting:(UIButton *) button
{
[self.motionManager startAccelerometerUpdatesToQueue:[[[NSOperationQueue alloc] init] autorelease] withHandler:^(CMAccelerometerData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
CGRect labelFrame = button.frame;
labelFrame.origin.x += data.acceleration.x * MOTION_SCALE;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.x = button.frame.origin.x;
labelFrame.origin.y -= data.acceleration.y * MOTION_SCALE;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.y = button.frame.origin.y;
button.frame = labelFrame;
} );
}];
}
-(void) startDrifting2:(UIButton *) button
{
CMAccelerometerData *data = [self.motionManager accelerometerData];
dispatch_async(dispatch_get_main_queue(), ^{
CGRect labelFrame = button.frame;
labelFrame.origin.x += data.acceleration.x * MOTION_SCALE * 1;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.x = button.frame.origin.x;
labelFrame.origin.y += data.acceleration.y * MOTION_SCALE * 1;
if (!CGRectContainsRect(self.view.bounds, labelFrame))
labelFrame.origin.y = button.frame.origin.y;
button.frame = labelFrame;
});
}
尝试将代理设置为自身,然后将其返回给应用程序的主代理。单击“下一步”按钮时,请尝试用另一种方法重新分配代理