IOS Development's motion events and remote control _ios

Source: Internet
Author: User
Tags rewind

Before we've learned about touch processing and gesture recognition, in fact these two touch events that are part of the iOS event, today we're going to learn two other things about the iOS event:

I. Sports EVENTS
Motion events, which are triggered by accelerators, like touch events, inherit objects of the Uiresponder class to handle motion events

Uiresponder How to handle motion events:

Copy Code code as follows:

#pragma mark, when the movement started.
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event;
#pragma mark, after the campaign is done.
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event;
#pragma Mark's movement was accidentally canceled.
-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event;

You didn't read it wrong, here said the event, just shaking the phone, so only the start of the movement, the end of the movement, the movement is canceled, can not get the movement of the speed of movement, direction of movement and other data, these need another framework to achieve, we can understand that the movement time here is "swinging event."

Monitor the motion of the event premises:
Listener object must be first responder, control needs-(BOOL) Canbecomefirstresponder method returns Yes
Invokes the Becomefirstresponder method of the motion control in the View controller-(void) Viewwillappear: (BOOL) Animated method, making the control appear as the first responder
Call the Resignfirstresponder method of the motion control in the View controller-(void) Viewdiddisappear: (BOOL) animated method to unregister the control's first responder identity when the control is not displayed
Instance:

Copy Code code as follows:

kcimageview.m
#import "KCImageView.h"
#define KIMAGECOUNT 3
@implementation Kcimageview
-(Instancetype) initWithFrame: (CGRect) Frame {
self = [super Initwithframe:frame];
if (self) {
Self.image = [self getImage];
}
return self;
}
#pragma mark settings control can be the first responder
-(BOOL) canbecomefirstresponder{
return YES;
}
#pragma mark Movement started
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event{
This only deals with shaking events.
if (motion = = Uieventsubtypemotionshake) {
Self.image = [self getImage];
}
}
#pragma mark campaign is over.
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event{
}
#pragma mark randomly gets pictures
-(UIImage *) getimage{
int index = arc4random ()% Kimagecount;
NSString *imagename = [NSString stringwithformat:@ "Avatar%i.png", index];
UIImage *image = [UIImage imagenamed:imagename];
return image;
}
@end
Kcshakeviewcontroller.m
#import "KCShakeViewController.h"
#import "KCImageView.h"
@interface Kcshakeviewcontroller () {
Kcimageview *_imageview;
}
@end
@implementation Kcshakeviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
}
Make a control the first responder #pragma mark view is displayed
-(void) Viewdidappear: (BOOL) animated{
_imageview = [[Kcimageview alloc] Initwithframe:[uiscreen mainscreen].applicationframe];
_imageview.userinteractionenabled = true;
[Self.view Addsubview:_imageview];
[_imageview Becomefirstresponder];
}
#pragma the identity of a control's first responder when the Mark view is not displayed
-(void) Viewdiddisappear: (BOOL) animated{
[_imageview Resignfirstresponder];
}
@end

Motion Event Instance Effect

Second, remote control events
The iOS remote control event is triggered by other remote devices (such as the headset control button), and only the iOS remote control event is associated with-(void) Remotecontrolreceivedwithevent: (Uievent *) event

The premise of listening for remote control events:
Start remote event receive, call
[[UIApplication sharedapplication] beginreceivingremotecontrolevents];
UI controls also require that you must be the first responder "using reference motion events"
But if it is a view controller or uiapplication, there is no requirement to be the first responder
The application must be the current audio amount controller
The current iOS7 gives us remote CONTROL permission limited to audio control

Copy Code code as follows:

typedef ns_enum (Nsinteger, Uieventsubtype) {
Does not contain any child event types
Uieventsubtypenone = 0,
Shaking events (support for this event starting from iOS3.0)
Uieventsubtypemotionshake = 1,
Remote Control child event type (remote control events are supported from iOS4.0)
Play event "Action: Stop State, press the middle button of Earphone line to click"
Uieventsubtyperemotecontrolplay = 100,
Pause Event
Uieventsubtyperemotecontrolpause = 101,
Stop Event
Uieventsubtyperemotecontrolstop = 102,
Play or pause toggle operation: In the state of playing or pausing, press the middle button of the earphone line.
Uieventsubtyperemotecontroltoggleplaypause = 103,
Next "Operation: Press the earphone line to control the middle button two"
Uieventsubtyperemotecontrolnexttrack = 104,
Previous "Operation: according to the Headphone Line Control center button three"
Uieventsubtyperemotecontrolprevioustrack = 105,
Rewind start "Operation: Press the middle button of the earphone to control the three don't loosen up"
Uieventsubtyperemotecontrolbeginseekingbackward = 106,
Fast Rewind Stop "operation: according to the Headphone Line Control Center button three down to the fast back position release
Uieventsubtyperemotecontrolendseekingbackward = 107,
Fast forward start operation: Press the middle button in the earphone line and don't loosen it.
Uieventsubtyperemotecontrolbeginseekingforward = 108,
Fast forward Stop "operation: according to the Headphone Line Control Center button two down to the fast forward position release"
Uieventsubtyperemotecontrolendseekingforward = 109,
};

Instance:

Copy Code code as follows:

#import "ViewController.h"
@interface Viewcontroller () {
UIButton *_playbutton;
BOOL _isplaying;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[[UIApplication sharedapplication] beginreceivingremotecontrolevents];
[Self initlayout];
}
-(BOOL) canbecomefirstresponder{
return NO;
}
-(void) Viewdidappear: (BOOL) animated{
[Super viewdidappear:animated];
Nsurl *url = [Nsurl urlwithstring:@ "http://stream.jewishmusicstream.com:8000"];
_player = [[Avplayer alloc] initwithurl:url];
}
#pragma mark Remote Control event
-(void) Remotecontrolreceivedwithevent: (uievent *) event{
if (Event.type = = Uieventtyperemotecontrol) {
Switch (event.subtype) {
Case Uieventsubtyperemotecontrolplay:
[_player play];
_isplaying = true;
Break
Case Uieventsubtyperemotecontroltoggleplaypause:
[Self Btnclick:_playbutton];
Break
Case Uieventsubtyperemotecontrolnexttrack:
NSLog (@ "Next ...");
Break
Case Uieventsubtyperemotecontrolprevioustrack:
NSLog (@ "Previous ...");
Break
Case Uieventsubtyperemotecontrolbeginseekingforward:
NSLog (@ "Begin seek forward ...");
Break
Case Uieventsubtyperemotecontrolendseekingforward:
NSLog (@ "End seek forward ...");
Break
Case Uieventsubtyperemotecontrolbeginseekingbackward:
NSLog (@ "Begin seek backward ...");
Break
Case Uieventsubtyperemotecontrolendseekingbackward:
NSLog (@ "End seek backward ...");
Break
Default
Break
}
[Self changeuistate];
}
}
#pragma mark Interface layout
-(void) initlayout{
Album cover
UIImage *image = [uiimage imagenamed:@ "wxl.jpg"];
CGRect *frame = [UIScreen mainscreen].applicationframe;
Uiimageview *imageview = [[Uiimageview alloc] initwithframe:frame];
Imageview.image = image;
Imageview.contentmode = Uiviewcontentmodescaleaspectfill;
[Self.view Addsubview:imageview];
Play Control Panel
UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, 480, 320, 88)];
View.backgroundcolor = [Uicolor Lightgraycolor];
View.alpha = 0.9;
[Self.view Addsubview:view];
Add Play button
_playbutton = [UIButton buttonwithtype:uibuttontypecustom];
_playbutton.bounds = CGRectMake (0, 0, 50, 50);
CGFloat playbtnx = VIEW.FRAME.SIZE.WIDTH/2;
CGFloat Playbtny = VIEW.FRAME.SIZE.HEIGHT/2;
_playbutton.center = Cgpointmake (playbtnx, Playbtny);
[Self changeuistate];
[_playbutton addtarget:self
Action: @selector (Btnclick:)
Forcontrolevents:uicontroleventtouchupinside];
[View Addsubview:_playbutton];
}
#pragma Mark Interface State
-(void) changeuistate{
if (_isplaying) {
UIImage *pauseimage = [uiimage imagenamed:@ "Playing_btn_pause_n.png"];
UIImage *pauseimageh = [uiimage imagenamed:@ "Playing_btn_pause_h.png"];
[_playbutton setimage:pauseimage Forstate:uicontrolstatenormal];
[_playbutton Setimage:pauseimageh forstate:uicontrolstatehighlighted];
}else{
UIImage *playimage = [uiimage imagenamed:@ "Playing_btn_play_n.png"];
UIImage *playimageh = [uiimage imagenamed:@ "Playing_btn_play_h.png"];
[_playbutton setimage:playimage Forstate:uicontrolstatenormal];
[_playbutton Setimage:playimageh forstate:uicontrolstatehighlighted];
}
}
-(void) Btnclick: (UIButton *) btn{
if (_isplaying) {
[_player pause];
}else{
[_player play];
}
_isplaying =! _isplaying;
[Self changeuistate];
}
@end

Remote control Instance Effect

This note has a lot of code, because these two events use simple, theoretical knowledge is not much, light theory, also not good understanding, paste code very intuitive.

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.