IOS开发实现手机震动的提示实例代码
程序员文章站
2023-12-20 12:54:16
ios开发实现手机震动的提示实例代码
我们都知道手机有震动功能,其实呢,这个功能实现起来特别的简单,我们只需要用到几个函数就可以了:
- (v...
ios开发实现手机震动的提示实例代码
我们都知道手机有震动功能,其实呢,这个功能实现起来特别的简单,我们只需要用到几个函数就可以了:
- (void)motionbegan:(uieventsubtype)motion withevent:(uievent *)event - (void)motionended:(uieventsubtype)motion withevent:(uievent *)event - (void)motioncancelled:(uieventsubtype)motion withevent:(uievent *)event
还有就是通过canbecomefirstresponder:设置一个第一响应者为label,然后摇动手机两下,看看效果如下:
代码如下:
hhlappdelegate.h
#import <uikit/uikit.h> @class hhlviewcontroller; @interface hhlappdelegate : uiresponder <uiapplicationdelegate> @property (strong, nonatomic) uiwindow *window; @property (strong, nonatomic) hhlviewcontroller *viewcontroller; @end
hhlappdelegate.m
#import "hhlappdelegate.h" #import "hhlviewcontroller.h" @implementation hhlappdelegate - (void)dealloc { [_window release]; [_viewcontroller release]; [super dealloc]; } - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { self.window = [[[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease]; // override point for customization after application launch. self.viewcontroller = [[[hhlviewcontroller alloc] initwithnibname:@"hhlviewcontroller" bundle:nil] autorelease]; self.window.rootviewcontroller = self.viewcontroller; [self.window makekeyandvisible]; return yes; } - (void)applicationwillresignactive:(uiapplication *)application { // sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or sms message) or when the user quits the application and it begins the transition to the background state. // use this method to pause ongoing tasks, disable timers, and throttle down opengl es frame rates. games should use this method to pause the game. } - (void)applicationdidenterbackground:(uiapplication *)application { // use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // if your application supports background execution, this method is called instead of applicationwillterminate: when the user quits. } - (void)applicationwillenterforeground:(uiapplication *)application { // called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationdidbecomeactive:(uiapplication *)application { // restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previously in the background, optionally refresh the user interface. } - (void)applicationwillterminate:(uiapplication *)application { // called when the application is about to terminate. save data if appropriate. see also applicationdidenterbackground:. } @end
hhlviewcontroller.h
#import <uikit/uikit.h> @interface hhlviewcontroller : uiviewcontroller @end @interface labelformotion : uilabel @end
hhlviewcontroller.m
#import "hhlviewcontroller.h" @interface hhlviewcontroller () @end @implementation labelformotion - (bool)canbecomefirstresponder { return yes; } @end @implementation hhlviewcontroller - (void)viewdidload { [super viewdidload]; labelformotion *label = [[[labelformotion alloc]init]autorelease]; label.frame = self.view.bounds; label.autoresizingmask =uiviewautoresizingflexiblewidth|uiviewautoresizingflexibleheight; label.textalignment = nstextalignmentcenter; label.text = @"shake me"; [self.view addsubview:label]; //将标签设置为第一响应者 [label becomefirstresponder]; [label release]; } - (void)motionbegan:(uieventsubtype)motion withevent:(uievent *)event { nslog(@"motionbegan"); } //震动结束时调用的方法 - (void)motionended:(uieventsubtype)motion withevent:(uievent *)event { nslog(@"motionended"); uialertview *alert = [[uialertview alloc]initwithtitle:nil message:@"地震了" delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil nil]; [alert show]; [alert release]; } - (void)motioncancelled:(uieventsubtype)motion withevent:(uievent *)event { nslog(@"motioncancelled"); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } @end
其实更简单的没有必要搞一个类继承自uilabel,可以直接定义一个uilabel的对象就行了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!