ios 搖一搖功能的實現

當我們用微信的時候是不是感覺搖一搖的功能很炫呢?是不是感覺實現起來比較麻煩呢?

其實,不然,這些都是蘋果已經給我們封裝好了,給我提供了非常簡單的入口了。

在UIResponder中有這些方法:

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"began");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"cancel");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"end");
}

在UIViewController的子類中都可以直接使用這些方法,因爲UIViewController也是繼承於UIResponder的。

實現中,需要讓viewController這個類本身支持搖一搖這個功能。

代碼:

    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
    [self becomeFirstResponder];

-(BOOL)canBecomeFirstResponder {

    return YES;

}


ok,這兩句代碼就能支持這個功能了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章