国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS開發實現手機震動的提示實例代碼

2019-10-21 18:47:05
字體:
來源:轉載
供稿:網友

IOS開發實現手機震動的提示實例代碼

我們都知道手機有震動功能,其實呢,這個功能實現起來特別的簡單,我們只需要用到幾個函數就可以了: 

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

還有就是通過canBecomeFirstResponder:設置一個第一響應者為label,然后搖動手機兩下,看看效果如下:

IOS簡單的實現手機震動的提示,IOS,手機震動提示開發

代碼如下:

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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金塔县| 盐源县| 育儿| 维西| 巴塘县| 尼木县| 宜都市| 左贡县| 太仆寺旗| 清丰县| 大悟县| 东乡族自治县| 巴楚县| 天等县| 陵川县| 余庆县| 镇赉县| 西乡县| 九江县| 紫阳县| 平谷区| 光泽县| 旺苍县| 芒康县| 永福县| 凤凰县| 大英县| 获嘉县| 周宁县| 武川县| 信丰县| 维西| 新蔡县| 宁阳县| 伊宁市| 鄂尔多斯市| 施甸县| 彭山县| 丹江口市| 库尔勒市| 抚州市|