Shake in IOS

Source: Internet
Author: User

Simple implementation.

There are several monitoring responses in the uiresponder class.

Responding to touch events
-Touchesbegan: withevent:
-Touchesmoved: withevent:
-Touchesended: withevent:
-Touchescancelled: withevent:
Responding to Motion Events
-Motionbegan: withevent:
-Motionended: withevent:
-Motioncancelled: withevent:

-Motionended: withevent:

The parameter event is available

Typedef Enum {
Uieventsubtypenone = 0,

Uieventsubtypemotionshake = 1,

Uieventsubtyperemotecontrolplay = 100,
Uieventsubtyperemotecontrolpause = 101,
Uieventsubtyperemotecontrolstop = 102,
Uieventsubtyperemotecontroltoggleplaypause = 103,
Uieventsubtyperemotecontrolnexttrack = 104,
Uieventsubtyperemotecontrolprevioustrack= 105,
Uieventsubtyperemotecontrolbeginseekingbackward = 106,
Uieventsubtyperemotecontrolendseekingbackward = 107,
Uieventsubtyperemotecontrolbeginseekingforward = 108,
Uieventsubtyperemotecontrolendseekingforward = 109,
} Uieventsubtype;

What we use to determine shake is uieventsubtypemotionshake.

See the code below.

// Shakewindow. h

# Import <uikit/uikit. h>

@ Interface shakewindow: uiview

@ End

// Shakewindow. m

# Import "shakewindow. H"

@ Implementation shakewindow

-(ID) initwithframe :( cgrect) Frame
{
Self = [Super initwithframe: frame];
If (Self ){
// Initialization code
}
Return self;
}

/*
// Only override drawrect: If you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(Void) drawrect :( cgrect) rect
{
// Drawing code
}
*/

-(Void) motionbegan :( uieventsubtype) motion withevent :( uievent *) event
{

}

-(Void) motionended :( uieventsubtype) motion withevent :( uievent *) event
{
If (motion = uieventsubtypemotionshake)
{
// User was shaking the device. Post a notification named "Shake ".
[[Nsicationcenter center defacenter center] postnotificationname: @ "Shake" Object: Self];
}

}

-(Void) motioncancelled :( uieventsubtype) motion withevent :( uievent *) event
{

}

@ End

Add and use this wiindow elsewhere, and then monitor the implementation:

........

{

// Add the shake noticefication.
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (shakeme) Name: @ "Shake" Object: Nil];
......
}

-(Void) shakeme
{
Uialertview * alertview = [[uialertview alloc] initwithtitle: @ "bing" message: @ "Shake me" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: nil, nil]
[Alertview show];
}

That's over.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.