全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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中关于Taptic-Engine震动反馈的深入解析  # iOS仿微信摇一摇动画效果加震动音效实例  # 基于iOS实现音乐震动条效果  # iOS中震动反馈(UIFeedbackGenerator)与系统震动详解  # 一响  # 几个  # 希望能  # 可以直接  # 只需要  # 谢谢大家  # 设置为  # 两下  # 结束时  # 就可以  # 就行了  # 没有必要  # bounds  # initWithFrame  # UIScreen  # mainScreen  # Override  # point  # didFinishLaunchingWithOptions  # autorelease 


相关文章: 制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  制作销售网站教学视频,销售网站有哪些?  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  如何在搬瓦工VPS快速搭建网站?  简历在线制作网站免费,免费下载个人简历的网站是哪些?  如何选择高效可靠的多用户建站源码资源?  建站之星在线客服如何快速接入解答?  独立制作一个网站多少钱,建立网站需要花多少钱?  建站主机选购指南:核心配置与性价比推荐解析  如何选择最佳自助建站系统?快速指南解析优劣  如何通过VPS搭建网站快速盈利?  高端网站建设与定制开发一站式解决方案 中企动力  东莞专业网站制作公司有哪些,东莞招聘网站哪个好?  如何通过NAT技术实现内网高效建站?  子杰智能建站系统|零代码开发与AI生成SEO优化指南  定制建站策划方案_专业建站与网站建设方案一站式指南  红河网站制作公司,红河事业单位身份证如何上传?  如何选择高效便捷的WAP商城建站系统?  如何通过IIS搭建网站并配置访问权限?  制作企业网站建设方案,怎样建设一个公司网站?  如何快速使用云服务器搭建个人网站?  自助网站制作软件,个人如何自助建网站?  如何在云服务器上快速搭建个人网站?  简单实现Android文件上传  岳西云建站教程与模板下载_一站式快速建站系统操作指南  如何在Golang中引入测试模块_Golang测试包导入与使用实践  网站制作公司排行榜,四大门户网站排名?  建站之星代理如何获取技术支持?  如何通过远程VPS快速搭建个人网站?  定制建站平台哪家好?企业官网搭建与快速建站方案推荐  代购小票制作网站有哪些,购物小票的简要说明?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  网站网页制作专业公司,怎样制作自己的网页?  招贴海报怎么做,什么是海报招贴?  如何在IIS中新建站点并配置端口与IP地址?  制作充值网站的软件,做人力招聘为什么要自己交端口钱?  如何用美橙互联一键搭建多站合一网站?  如何设置并定期更换建站之星安全管理员密码?  如何在服务器上配置二级域名建站?  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  济南网站建设制作公司,室内设计网站一般都有哪些功能?  建站主机服务器选购指南:轻量应用与VPS配置解析  建站主机选择指南:服务器配置与SEO优化实战技巧  如何在沈阳梯子盘古建站优化SEO排名与功能模块?  香港网站服务器数量如何影响SEO优化效果?  内网网站制作软件,内网的网站如何发布到外网?  济南网站制作的价格,历城一职专官方网站?  css网站制作参考文献有哪些,易聊怎么注册?  如何通过建站之星自助学习解决操作问题?  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四) 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。