IOS开发实现手机震动的提示实例代码
2017-04-13 14:12
916 查看
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的对象就行了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章推荐
- ios开发——代码实现获得手机系统的名字和版本号&当前网络状态
- ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。
- iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
- IOS简单的实现手机震动的提示
- android开发之蜂鸣提示音和震动提示的实现原理与参考代码
- android 开发之蜂鸣提示音和震动提示的实现原理与参考代码
- iOS实现手机的连续震动和停止震动的代码(拷如工程就能用)
- iOS开发中使app获取本机通讯录的实现代码实例
- android开发之蜂鸣提示音和震动提示的实现原理与参考代码
- iOS开发有用代码(有判断邮箱格式是否正确的代码、图片压缩、亲测可用的图片上传代码和实现点击图片进行跳转的代码等。)
- android开发(21)蜂鸣提示音和震动提示的实现。
- 安卓与IOS通过hessian接口实现用友系统移动手机订单4安卓手机开发
- 【iOS开发必备指南合集】申请企业级IDP、真机调试、游戏接入GameCenter 指南(实现仿官方的成就提示)、游戏接入OpenFeint指南;
- iOS 实现简单的移动UIView代码实例
- 用js实现输入提示(自动完成)的实例代码
- 安卓与IOS通过hessian接口实现用友系统移动手机订单5苹果手机开发
- SP 短信开发-PROVISION接口实现-正向订购实例代码
- ((ios开发学习笔记 十))代码实现自定义TableView
- SP 短信开发-PROVISION接口实现-正向订购实例代码
- Android开发之手机铃声代码实现