服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS开发实现手机震动的提示实例代码

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

2021-03-13 16:26iOS开发网 IOS

这篇文章主要介绍了IOS开发实现手机震动的提示实例代码的相关资料,需要的朋友可以参考下

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

我们都知道手机有震动功能,其实呢,这个功能实现起来特别的简单,我们只需要用到几个函数就可以了: 

?
1
2
3
- (void)motionbegan:(uieventsubtype)motion withevent:(uievent *)event
- (void)motionended:(uieventsubtype)motion withevent:(uievent *)event
- (void)motioncancelled:(uieventsubtype)motion withevent:(uievent *)event

还有就是通过canbecomefirstresponder:设置一个第一响应者为label,然后摇动手机两下,看看效果如下:

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

代码如下:

hhlappdelegate.h

?
1
2
3
4
5
6
7
8
9
10
11
#import <uikit/uikit.h>
 
@class hhlviewcontroller;
 
@interface hhlappdelegate : uiresponder <uiapplicationdelegate>
 
@property (strong, nonatomic) uiwindow *window;
 
@property (strong, nonatomic) hhlviewcontroller *viewcontroller;
 
@end

hhlappdelegate.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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

?
1
2
3
4
5
6
7
8
9
10
#import <uikit/uikit.h>
 
@interface hhlviewcontroller : uiviewcontroller
 
@end
 
 
@interface labelformotion : uilabel
 
@end

hhlviewcontroller.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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的对象就行了。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/hanhailong18/article/details/17615795

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03