非常感谢您的帮助,
凯文
解决方法 我曾经做过这样的应用程序.我会在这里发布,但它适用于iOS 4 …
http://localhostr.com/files/7ca39c/Tilter.zip
校准:
int tapCount = 0;- (voID)cancelDoubleTap { if (tapCount < 2) { tapCount = 0; }}- (voID)reset { [[NSUserDefaults standardUserDefaults] setfloat:0 forKey:@"X-Calibrate"]; [[NSUserDefaults standardUserDefaults] setfloat:0 forKey:@"Y-Calibrate"]; UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"reset!" message:@"Calibration reset." delegate:nil cancelbuttonTitle:@"Okay." otherbuttonTitles:nil]; [alert show]; [alert release];}- (voID)calibrate { if (tapCount == 3) { tapCount = 0; return; } [[NSUserDefaults standardUserDefaults] setfloat:accelX forKey:@"X-Calibrate"]; [[NSUserDefaults standardUserDefaults] setfloat:accelY forKey:@"Y-Calibrate"]; UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"Calibrated!" message:[Nsstring stringWithFormat:@"Calibrated to: %.2f %.2f.",accelX,accelY] delegate:nil cancelbuttonTitle:@"Okay." otherbuttonTitles:nil]; [alert show]; [alert release]; tapCount = 0;}- (BOol)canBecomeFirstResponder { return YES;}- (voID)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(cancelDoubleTap) userInfo:nil repeats:NO]; tapCount ++; if (tapCount == 3) { [timer invalIDate]; [self reset]; return; } if (tapCount == 2) { [timer invalIDate]; timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(calibrate) userInfo:nil repeats:NO]; } }
和…
- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions { but = [[UIbutton alloc] initWithFrame:window.frame]; [but addTarget:self action:@selector(touchesEnded:withEvent:) forControlEvents:UIControlEventtouchUpInsIDe]; [window addSubvIEw:but]; // OverrIDe point for customization after application launch. Box = [[UIVIEw alloc] initWithFrame:CGRectMake(self.window.center.x - 50,self.window.center.y - 50,100,100)]; [window makeKeyAndVisible]; Box.backgroundcolor = [UIcolor colorWithRed:1.0 green:0.0 blue:0.0 Alpha:0.75]; [window addSubvIEw:Box]; [[UIAccelerometer sharedAccelerometer] setDelegate:self]; [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1/100.0)]; slIDer = [[UiSlider alloc] initWithFrame:CGRectMake(20,438,280,22)]; [slIDer setMinimumValue:2.0]; [slIDer setMaximumValue:50.0]; [window addSubvIEw:slIDer]; return YES; }
.h文件:
#import <UIKit/UIKit.h>@interface TilterappDelegate : NSObject <UIApplicationDelegate,UIAccelerometerDelegate> { UIWindow *window; UIVIEw *Box; UiSlider *slIDer; UIbutton *but;}@property (nonatomic,retain) IBOutlet UIbutton *but;@property (nonatomic,retain) IBOutlet UiSlider *slIDer;@property (nonatomic,retain) IBOutlet UIVIEw *Box;@property (nonatomic,retain) IBOutlet UIWindow *window;@end
移动代码:
- (voID)moveBoxByX:(float)x byY:(float)y { float newX = Box.center.x + x; float newY = Box.center.y + y; if (newX < 50) newX = 50; if (newX > 270) newX = 270; if (newY < 50) newY = 50; if (newY > 430) newY = 430; Box.center = CGPointMake(newX,newY);}
应该在大多数时间工作.
总结以上是内存溢出为你收集整理的objective-c – 加速度计和校准 – iPhone SDK全部内容,希望文章能够帮你解决objective-c – 加速度计和校准 – iPhone SDK所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)