开发者

core motion moving two things with the accerlerometer

开发者 https://www.devze.com 2023-03-25 09:02 出处:网络
I\'m trying to move two buttons around using the accelerometer in xcode.I have the first button working, when I call startDrifting, but when I call startDrifting2 for the second button, nothing is hap

I'm trying to move two buttons around using the accelerometer in xcode. I have the first button working, when I call startDrifting, but when I call startDrifting2 for the second button, nothing is happening with the second button. I tried calling startAccelerometerUpdatesToQueue for the startDrifting2, but then the first one stopped working. any ideas?

-(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)) 
            l开发者_运维问答abelFrame.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;
});  
}


Try setting the delegate to itself and then give it back to the main delegate of the app. When you hit the next button try and reassign the delegate back in the other method.

0

精彩评论

暂无评论...
验证码 换一张
取 消