IOS简单的实现手机震动的提示
2013-12-27 18:20
381 查看
我们都知道手机有震动功能,其实呢,这个功能实现起来特别的简单,我们只需要用到几个函数就可以了:
- (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];
[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实现手机的连续震动和停止震动的代码(拷如工程就能用)
- 实现ios提示音播放与震动
- ios简单实现如果没有开启定位,提示开启系统软件定位功能
- iOS如何完美简单实现UITableView索引的放大悬浮提示View显示
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- C#简单实现发送手机短信
- iOS实现短信发送代码---简单demo
- ios中提示信息的实现及自动消失
- iOS开发在js中调用oc可以实现在HTML5中调用手机系统的相册,拍照等。
- IOS简单实现瀑布流UICollectionView
- iOS之简单瀑布流的实现
- ios开发之实现提示框动画效果
- iOS Widget的简单实现
- IOS实现一个简单的浏览器
- Android 手机震动功能实现
- iOS瀑布流的简单实现(Swift)
- iOS-网络-NSURLSessionDataTask实现大文件下载,进度提示
- iOS-UI-UIScrollView自动布局的简单说明和底层实现(改bounds)