1. 程式人生 > >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];
    [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簡單實現手機震動提示

我們都知道手機有震動功能,其實呢,這個功能實現起來特別的簡單,我們只需要用到幾個函式就可以了: - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event - (void)motionEnde

iOS簡單實現toastView

自定義提示 /** 獲取指定寬度width的字串在UITextView上的高度 @param textView 待計算的UITextView @param width 限制字串顯示區域的寬度 @return 返回的高度 */ - (float)heightForStrin

H5移動端實現手機震動效果

判斷相容 瀏覽器對振動API的支援情況,一個好的習慣就是在使用之前要檢查一下當前你的應用環境、瀏覽器是否支援振動API。下面就是檢測的方法: setTimeout(()=>{ navigator.vibrate = navigator.vibrate || navigator.

iOS簡單實現圖片模糊漸變效果

文章說明:本文可以其實只是個人對知識的記錄,為了自己以後找起來方便 例子參考連結 http://code4app.com/ios/EssentialImager/527cb3596803fa4e50000000 實現原理:其實就是用一張黑白的漸變圖片作為底圖 效果圖片: 具

怎麼用 Cocos2d- 3.x 實現 手機震動

在很多時候,我們都看到很多遊戲裡面有手機震動的這種效果,今天給大家分享一下怎麼用Cocos2d-3.x實現手機震動。 廢話不多說,直接進入主題,請看 1、首先你需要建一個Coco2dx的專案,確保當前專案能打包到Android手機上,並沒有錯誤 2、建立一個Vibrator

iOS簡單實現虛線的小方法

做程式猿也有段時間了。 寫程式的時候,經常會用到各種線。 現在我就簡單說一下 用UIGraphicsBeginImageContext來實現一道虛線的繪製。 我這裡是將方法寫到UIImage的一個擴充套件類中, 首先 .h 檔案,宣告方法: #i

iOS實現手機的連續震動和停止震動的程式碼(拷如工程就能用)

最近在做一個類似手機來電一樣的讓手機無線震動的功能。蘋果官方給出的介面很簡單總結起來就兩步: 1.往專案中匯入AudiToolbox.framework框架 2.就一句程式碼: AudioServicesPlaySystemSound (kSystemSoundID_V

iOS如何完美簡單實現UITableView索引的放大懸浮提示View顯示

一,問題描述:在好多app上面,類似通訊錄的名字都是按字母分組的,索引的時候在UITableView中間會顯示一個浮動的VIew顯示當前索引的字母,如何實現這一功能? 二,實現思路,第一種,簡單粗暴的方式是自定義UITableView的索引,但是比較麻煩。第二種,在UITa

IOS-小項目(餓了麽 網絡部分 簡單實現

分享 tab 程序啟動 下拉刷新控件 ram pen address register 單例 在介紹小項目之前,在此說明一下此代碼並非本人所寫,我只是隨筆的整理者。 在介紹之前先展現一下效果圖。 看過效果圖大家應該很熟悉了,就是餓了麽的一個界面而已,值得註意的是,實現時並

Android簡單實現手機圖片上傳到server中

sdk etc mov 創建 ast bmi 以及 lena ews 在本例中。將會簡單的實現安卓手機將圖片上傳到server中。本例使用到了 server端:PHP+APACHE 客戶端:JAVA 先簡單實現一下server端的上傳並測試上傳

自定義訊息通知、手機震動提示

Android中自定義訊息通知、獲取手機震動、提示音,在此做下記錄 package com.wjy.project.railway.activity; import android.annotation.TargetApi; import android.app.Notification;

業務重點-實現一個簡單手機號碼驗證

前言     本文純乾貨,直接拿走使用,不用付費。在業務開發中,手機號碼驗證是我們常常需要面對的問題,目前市場上各種各樣的手機號碼驗證方式,比如正則表示式等等,本文結合實際業務場景,在業務級別對手機號碼進行嚴格驗證;同時增加可配置方式,方便業務擴充套件,程式碼非常簡單,擴充套件非常靈活。 1. 目前手機號

android 呼叫手機打電話 簡單實現

首先看下佈局xml ,我的事一個button按鈕 點選 打電話 ,手機號是自己定義,這個你可以根據自己邏輯寫 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas

(二十一)蜂鳴提示音 和手機震動

本節知識點 蜂鳴提示音 plus.device.beep(); 手機震動 plus.device.vibrate(); 蜂鳴提示音plus.device.beep() switch ( plus.os.name ) { //判斷裝置型別 case

iOS系統鍵盤上的輸入框簡單實現

#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface WYPSeparateSetCodeView : UIView @property (weak, nonat

ios swift 左側抽屜簡單實現

var leftView: UIView? var minX: CGFloat? var midX: CGFloat? var maxX:CGFloat? /* 抽屜 */ func drawerView(){

webapp在ios簡單實現滑動,回彈,加速等效果

-webkit-overflow-scrolling來自safari原生控制元件的實現,工作原理是:在有這個屬性的容器上,系統會建立了一個uiscrollview,應用於該元素並將之作為渲染物件,從而為我們實現體驗流暢的觸屏滑動 在Ios上的表現結果令人十分滿意,並且網

iOS簡單直播實現(二:推流)

 推流用的是一個第三方的IFLiveKit框架。這個框架基於rtmp協議的,c語言和oc語言分開的非常清楚,對oc語言非常友好,使用起來相對簡單。   IFLiveKit內部集成了GPUIImage。內部實現了圖片渲染等美豔效果。減少了開發時候美豔效果的除錯。   需

IOS動畫的使用方法總結-下拉框的簡單實現

第一種 CATransition *transition = [CATransition animation]; transition.duration = 0.4f;//時間

iOS Swift 簡單實現Loading動畫

最近突然對動畫特別感興趣,尤其是最常見的載入動畫,百度了一圈發現全是OC程式碼,這讓我這個習慣寫swift的假iOS開發很是鬱悶。 在網上扒的過程中看到一個挺簡單的一個載入動畫,看了一下原作者的程式碼,學習了一下原作者的思想,也感謝那位坐著哈。。。 接下來看