微信的搖一搖是怎么實現的~發現原來 ios本身就支持


在 UIResponder中存在這么一套方法

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

這就是執行搖一搖的方法。那么怎么用這些方法呢?

很簡單,你只需要讓這個Controller本身支持搖動

同時讓他成為第一相應者:

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    [[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];

    [selfbecomeFirstResponder];

}

 

然后去實現那幾個方法就可以了

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

{

    //檢測到搖動

}

 

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

{

    //搖動取消

}

 

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

{

    //搖動結束

    if (event.subtype == UIEventSubtypeMotionShake) {

        //something happens

    }

}

至於界面我就不寫了~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM