how to make an image appear everytime when i shake my iphone
i have created a shake application in ip开发者_如何学编程hone but i am having a problem. I want that when i shake my iphone everytime an image should appear for every shake.Can anybody help how is it possible. This is the code which i have written:
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];
This is for beginning animations and I have set my image alpha to 0.0 so that my image will disappear. This code is working properly. But I want it so that when I shake my iPhone again my image should reappear for every shake. How is it possible.
I suggest you to use UIAccelerometer for this purpose,Use the following code
@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
}
}
Cheers
精彩评论