溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

IOS如何實(shí)現(xiàn)手機(jī)震動的提示功能

發(fā)布時(shí)間:2021-07-08 18:23:43 來源:億速云 閱讀:438 作者:小新 欄目:移動開發(fā)

小編給大家分享一下IOS如何實(shí)現(xiàn)手機(jī)震動的提示功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

IOS開發(fā)實(shí)現(xiàn)手機(jī)震動的提示實(shí)例代碼

我們都知道手機(jī)有震動功能,其實(shí)呢,這個(gè)功能實(shí)現(xiàn)起來特別的簡單,我們只需要用到幾個(gè)函數(shù)就可以了: 

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

還有就是通過canBecomeFirstResponder:設(shè)置一個(gè)第一響應(yīng)者為label,然后搖動手機(jī)兩下,看看效果如下:

IOS如何實(shí)現(xiàn)手機(jī)震動的提示功能

代碼如下:

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]; 
  //將標(biāo)簽設(shè)置為第一響應(yīng)者 
  [label becomeFirstResponder]; 
  [label release]; 
} 
 
 
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{ 
  NSLog(@"motionBegan"); 
} 
 
//震動結(jié)束時(shí)調(diào)用的方法 
- (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

其實(shí)更簡單的沒有必要搞一個(gè)類繼承自UIlabel,可以直接定義一個(gè)UIlabel的對象就行了。

以上是“IOS如何實(shí)現(xiàn)手機(jī)震動的提示功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI