如何在每次摇动iphone时都显示图像

如何在每次摇动iphone时都显示图像,iphone,objective-c,Iphone,Objective C,我已经在iphone上创建了一个shake应用程序,但我遇到了一个问题。我希望每次摇动我的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

我已经在iphone上创建了一个shake应用程序,但我遇到了一个问题。我希望每次摇动我的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时,每次摇动我的图像都会重新出现。这怎么可能

我建议您为此使用UIAccelerator,请使用以下代码

@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            

}

}
@界面PageLoopViewController:UIViewController{
UIAccelerationValue My加速度计[3];
}
*******************.m代码*********************
#定义加速度计频率10//Hz
#定义kFilteringFactor 0.5
#定义kEraseAccelerationThreshold 0.2
-(无效)viewDidLoad{
[[UID加速度计]setDelegate:self];
[[UIAccelerator sharedAccelerometer]设置日期间隔:(1.30/K加速度计频率)];
}
-(无效)加速计:(UIAccelerator*)加速计DID加速度:(UIAcceleration*)加速度
{
UIAccelerationValue长度,x,y,z;
//使用基本高通滤波器消除重力的影响

MyAccelerator[0]=加速度.x*kFilteringFactor+MyAccelerator[0]*(1.0-kFilteringFactor); MyAccelerator[1]=加速度.y*kFilteringFactor+MyAccelerator[1]*(1.0-kFilteringFactor); MyAccelerator[2]=加速度.z*kFilteringFactor+MyAccelerator[2]*(1.0-kFilteringFactor); //计算加速度计三个轴的值 x=加速度。x-我的加速计[0]; y=加速度。y-我的加速计[1]; z=加速度。z-我的加速计[2]; //计算当前加速度的强度 长度=sqrt(x*x+y*y+z*z); 如果(长度>KeraseCacherationThreshold) { //图像编码 } }

干杯

请正确设置代码格式。很难读。。你试过代码了吗?嗨,我试过了,但它工作不正常。它给了我一个错误,我使用加减法计算值,即+和-。请帮助我解决这个问题MyAccelerator[0]=acceleration.x*kFilteringFactor+MyAccelerator[0]*(1.0-kFilteringFactor);MyAccelerator[1]=加速度.y*kFilteringFactor+MyAccelerator[1]*(1.0-kFilteringFactor);MyAccelerator[2]=加速度.z*kFilteringFactor+MyAccelerator[2]*(1.0-kFilteringFactor);首先,我得到了一个错误,我的加速计是未声明的,而我创建了声明的myacgrelometer,然后在上面三行中得到了一个无效的二进制运算符。我认为在objective-c中没有使用(+)和(-)运算符。请帮助我解决这个问题,如果(fabs(acceleration.x)>mAccelerationThresold | | | fabs(acceleration.y)>mAccelerationThresold | fabs(acceleration.z)>mAccelerationThresold){ShakingCount++;如果(ShakingCount>mShakecount){cRect myImageRect=CGRectMake(110.0f,70.0f,220.0f,380.0f);UIImageView*myImage=[UIImageView alloc]initWithFrame:myImageRect];[myImage setImage:[UIImage ImageName:@“tick_mark.png”];[self.view addSubview:myImage];[UIView beginAnimations:nil上下文:nil];[UIView setAnimationDuration:1];[myImage setAlpha:0.0];[UIView commitAnimations];[myImage release];在头文件中实现UIAccelerometerDelegate,并在头文件中将MyAccelerator声明为UIAccelerationValue MyAccelerator[3];此代码工作正常,但有一次不是在每次震动中,而是在每次震动中尝试