IOS开发实现手机震动的提示实例代码

时间:2021-05-02

ios开发实现手机震动的提示实例代码

我们都知道手机有震动功能,其实呢,这个功能实现起来特别的简单,我们只需要用到几个函数就可以了:

? 1 2 3 - (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

? 1 2 3 4 5 6 7 8 9 10 11 #import <uikit/uikit.h> @class hhlviewcontroller; @interface hhlappdelegate : uiresponder <uiapplicationdelegate> @property (strong, nonatomic) uiwindow *window; @property (strong, nonatomic) hhlviewcontroller *viewcontroller; @end

hhlappdelegate.m

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #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

? 1 2 3 4 5 6 7 8 9 10 #import <uikit/uikit.h> @interface hhlviewcontroller : uiviewcontroller @end @interface labelformotion : uilabel @end

hhlviewcontroller.m

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 #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的对象就行了。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/hanhailong18/article/details/17615795

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章