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

发布时间 - 2026-01-11 00:38:41    点击率:

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 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  无锡营销型网站制作公司,无锡网选车牌流程?  魔方云NAT建站如何实现端口转发?  jQuery validate插件功能与用法详解  如何在宝塔面板中修改默认建站目录?  Laravel定时任务怎么设置_Laravel Crontab调度器配置  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  googleplay官方入口在哪里_Google Play官方商店快速入口指南  linux写shell需要注意的问题(必看)  如何确保西部建站助手FTP传输的安全性?  Python图片处理进阶教程_Pillow滤镜与图像增强  uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址  Laravel队列由Redis驱动怎么配置_Laravel Redis队列使用教程  Laravel如何实现API版本控制_Laravel版本化API设计方案  Laravel的辅助函数有哪些_Laravel常用Helpers函数提高开发效率  Laravel Eloquent关联是什么_Laravel模型一对一与一对多关系精讲  Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】  Laravel如何生成API文档?(Swagger/OpenAPI教程)  南京网站制作费用,南京远驱官方网站?  Swift中循环语句中的转移语句 break 和 continue  Laravel如何实现API速率限制?(Rate Limiting教程)  Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制  详解Android中Activity的四大启动模式实验简述  如何在企业微信快速生成手机电脑官网?  Laravel怎么生成URL_Laravel路由命名与URL生成函数详解  javascript读取文本节点方法小结  Laravel如何与Pusher实现实时通信?(WebSocket示例)  专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?  Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件  公司门户网站制作流程,华为官网怎么做?  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  如何用ChatGPT准备面试 模拟面试问答与职场话术练习教程  如何快速生成橙子建站落地页链接?  如何在Windows虚拟主机上快速搭建网站?  Laravel怎么自定义错误页面_Laravel修改404和500页面模板  如何快速生成高效建站系统源代码?  UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】  详解jQuery停止动画——stop()方法的使用  Python文件操作最佳实践_稳定性说明【指导】  Laravel如何实现一对一模型关联?(Eloquent示例)  C++时间戳转换成日期时间的步骤和示例代码  Swift中swift中的switch 语句  实例解析Array和String方法  微信小程序 HTTPS报错整理常见问题及解决方案  Laravel如何配置任务调度?(Cron Job示例)  如何在橙子建站上传落地页?操作指南详解  Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程  Laravel如何从数据库删除数据_Laravel destroy和delete方法区别  昵图网官方站入口 昵图网素材图库官网入口  如何在建站主机中优化服务器配置?