每次摇动我的iPhone时,如何制作图像

时间:2011-01-14 05:35:03

标签: iphone objective-c

我在iphone中创建了一个摇动应用程序,但我遇到了问题。我希望每当我摇动我的iphone时,每次震动时都会出现一个图像。任何人都可以帮助它。 这是我写的代码:

CGRect myImageRect = CGRectMake(110.0f, 70.0f, 220.0f, 380.0f);
//This line is for setting my tick.png image on my image view
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
            [myImage setImage:[UIImage imageNamed:@"tick_mark.png"]];
        [self.view addSubview:myImage];

       //animation
        [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1];
                [myImage setAlpha:0.0];
            [UIView commitAnimations];

        [myImage release];

这是为了开始动画,我将我的图像alpha设置为0.0,这样我的图像就会消失。此代码正常运行。但是我想要它,这样当我再次摇动我的iPhone时,我的图像应该重新出现。这怎么可能。

1 个答案:

答案 0 :(得分:0)

我建议您使用UIAccelerometer来实现此目的,请使用以下代码

@interface PageLoopViewController : UIViewController<UIAccelerometerDelegate> {
UIAccelerationValue myAccelerometer[3];
}
******************* .m code*********************

#define kAccelerometerFrequency  10 //Hz
#define kFilteringFactor     0.5
#define kEraseAccelerationThreshold 0.2 

-(void)viewDidLoad{
[[UIAccelerometer sharedAccelerometer] setDelegate:self];


[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.30 / kAccelerometerFrequency)];

}

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
UIAccelerationValue  length, x, y,z;


//Use a basic high-pass filter to remove the influence of the gravity
myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
// Compute values for the three axes of the acceleromater
x = acceleration.x - myAccelerometer[0];
y = acceleration.y - myAccelerometer[1];
z = acceleration.z - myAccelerometer[2];

//Compute the intensity of the current acceleration 
length = sqrt(x * x + y * y + z * z);
if(length > kEraseAccelerationThreshold)
{
    //Image code            

}

}

干杯