服务器之家

服务器之家 > 正文

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

时间:2021-03-13 16:26     来源/作者: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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部