全网整合营销服务商

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

免费咨询热线: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 


相关文章: 台州网站建设制作公司,浙江手机无犯罪记录证明怎么开?  建站之星体验版:智能建站系统+响应式设计,多端适配快速建站  专业网站制作服务公司,有哪些网站可以免费发布招聘信息?  如何在云主机快速搭建网站站点?  香港服务器如何优化才能显著提升网站加载速度?  建站之星如何通过成品分离优化网站效率?  天河区网站制作公司,广州天河区如何办理身份证?需要什么资料有预约的网站吗?  图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?  如何通过PHP快速构建高效问答网站功能?  赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?  外汇网站制作流程,如何在工商银行网站上做外汇买卖?  大型企业网站制作流程,做网站需要注册公司吗?  高端建站三要素:定制模板、企业官网与响应式设计优化  如何用wdcp快速搭建高效网站?  建站之星客服服务时间及联系方式如何?  如何在沈阳梯子盘古建站优化SEO排名与功能模块?  *服务器网站为何频现安全漏洞?  建设网站制作价格,怎样建立自己的公司网站?  Bpmn 2.0的XML文件怎么画流程图  香港网站服务器数量如何影响SEO优化效果?  如何安全更换建站之星模板并保留数据?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?  微信小程序 五星评分(包括半颗星评分)实例代码  如何通过远程VPS快速搭建个人网站?  东莞专业网站制作公司有哪些,东莞招聘网站哪个好?  高性价比服务器租赁——企业级配置与24小时运维服务  名字制作网站免费,所有小说网站的名字?  焦点电影公司作品,电影焦点结局是什么?  C#怎么使用委托和事件 C# delegate与event编程方法  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  如何快速上传自定义模板至建站之星?  建站之星展会模板:智能建站与自助搭建高效解决方案  如何通过商城自助建站源码实现零基础高效建站?  建站之星手机一键生成:多端自适应+小程序开发快速建站指南  网站制作软件免费下载安装,有哪些免费下载的软件网站?  网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?  如何高效配置IIS服务器搭建网站?  如何通过西部数码建站助手快速创建专业网站?  简单实现Android验证码  如何用PHP工具快速搭建高效网站?  ,购物网站怎么盈利呢?  在线制作视频网站免费,都有哪些好的动漫网站?  教学论文网站制作软件有哪些,写论文用什么软件 ?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  建站主机无法访问?如何排查域名与服务器问题  存储型VPS适合搭建中小型网站吗?  logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?  如何选购建站域名与空间?自助平台全解析  如何在IIS中新建站点并配置端口与物理路径? 

您的项目需求

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