Touch Events (TouchView) in iOS-(proxy response)-(push button for touch)

Source: Internet
Author: User
Tags uikit

#import"RootViewController.h"#import"ActionView.h"#import"Uicolor+myuicolor.h"@interface Rootviewcontroller () @end @implementation Rootviewcontroller- (void) viewdidload {[Super viewdidload]; //Self refers to the current object Rootviewcontroller//NSLog (@ "%@", self);Actionview*actionviewyellow = [[Actionview alloc] Initwithframe:cgrectmake ( -, -,260,104)]; Actionviewyellow.tag= -; //Add a response event to the current view, implement the interface in view to the parameters of the Actionview incoming response, self is the view controller[Actionviewyellow addtarget:self Action: @selector (Changeactionviewcolor:)];    [Self.view Addsubview:actionviewyellow]; Actionviewyellow.backgroundcolor=[Uicolor Yellowcolor];                                   [Actionviewyellow release]; Actionview*actionviewgree = [[Actionview alloc] Initwithframe:cgrectmake ( -, Max,260,104)]; Actionviewgree.tag= $;    [Actionviewgree addtarget:self Action: @selector (Changesuperviewcolor:)];    [Self.view Addsubview:actionviewgree]; Actionviewgree.backgroundcolor=[Uicolor Bluecolor];        [Actionviewgree release]; Actionview*actionviewgray = [[Actionview alloc] Initwithframe:cgrectmake ( -,260,260,104)]; Actionviewgray.tag= -;    [Actionviewgray addtarget:self Action: @selector (changeselfviewframe:)];    [Self.view Addsubview:actionviewgray]; Actionviewgray.backgroundcolor=[Uicolor Graycolor];        [Actionviewgray release]; Actionview*actionviewred= [[Actionview alloc] Initwithframe:cgrectmake ( -, -,260,104)]; Actionviewred.tag= -;    [Self.view addsubview:actionviewred]; Actionviewred.backgroundcolor=[Uicolor Redcolor]; [actionviewred release];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark---target action//Modify your own color- (void) Changeactionviewcolor: (Actionview *) aview{Aview.backgroundcolor=[Uicolor Randomcolor];}- (void) Changesuperviewcolor: (Actionview *) aview{//AView.superview.backgroundColor = [Uicolor randomcolor];Self.view.backgroundColor =[Uicolor Randomcolor];}- (void) Changeselfviewframe: (Actionview *) aview{Aview.center= Cgpointmake (arc4random ()%101+ -, Arc4random ()%301+ -);} @end

DelegateViewController.h

#import <UIKit/UIKit.h>"TouchView.h"//4. Compliance Agreement @ Interface delegateviewcontroller:uiviewcontroller<touchviewdelegat>@end

Delegateviewcontroller.m

#import"DelegateViewController.h"#import"TouchView.h"#import"Uicolor+myuicolor.h"@interface Delegateviewcontroller () @end @implementation Delegateviewcontroller//5. Implementing the methods in the Protocol#pragmaMark--touchviewdelegate-(void) Handletouchbegin: (TouchView *) aview{Aview.backgroundcolor=[Uicolor Randomcolor];}//-(void) handletouchended: (TouchView *) Aview;////-(void) handletouchmoved: (TouchView *) Aview;////-(void) Handletouchcancle: (TouchView *) Aview;- (void) viewdidload {[Super viewdidload]; TouchView*redview = [[TouchView alloc] Initwithframe:cgrectmake ( -, -,280, -)];    [Self.view Addsubview:redview]; //3. Set proxy objects for RedviewRedview.Delegate=Self ; Redview.backgroundcolor=[Uicolor Redcolor]; [Redview release];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}

ActionView.h

#import <UIKit/UIKit.h>@interface actionview:uiview// provides interfaces to the outside world to get the target of touch response, and the Response Method (action)-(void) AddTarget: (ID) Target action: (SEL) action; @end

actionview.m

#import"ActionView.h"#import"Uicolor+myuicolor.h"@interface Actionview () {//Actionview Accept the response target view object and response method from the controllerID _target;//destination of the storage responseSEL _action;//ways to store responses} @end @implementation Actionview- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{//    //Self.backgroundcolor = [Uicolor randomcolor];//   //switch (self.tag) {//Case ://Self.backgroundcolor = [Uicolor randomcolor];//Break ;//Case ://Self.superview.backgroundColor = [Uicolor randomcolor];//Break ;//Case ://self.center = Cgpointmake (arc4random ()% 101 +, Arc4random ()% 301 +);//Break ;//            //Case ://self.bounds = CGRectMake (0, 0, arc4random ()% 101 +, Arc4random ()% ();//Break ;//Default:////Break ;//    }    //notifies target to execute the action method when Actionview receives touch time//Withobject is the parameter of _action//invokes the specified method of the stored object when the View object is touched//the _action parameter specified by self is also a click View Actionview;[_target performselector:_action withobject:self]; //NSLog (@ "%@,%@", _target,self);    }- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{    }- (void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event{    }- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{    }//before the operation of the Actionview process is not flexible enough, each time a new view, and the corresponding time, you have to modify the source code of the Touchbegin method, add processing events to the corresponding view, too cumbersome//at this time through the target/action design mode, let Actionview like UIButton as flexible//provide interfaces to the outside world, get the target of the touch response (target), and the Response Method (action)- (void) AddTarget: (ID) Target action: (SEL) action{//save externally specified response targets (target) and behavior (action)_target =Target; _action=Action;} @end

TouchVew.h

#import <UIKit/UIKit.h>@class TouchView;/** * Agreement and use of agents; Steps to use when customizing the Protocol: 1. Define the Protocol (the task the storage agent should complete in the Protocol) 2. Define Proxy properties (store external settings proxy objects) 3. Specify proxy objects in other files (to the Protocol settings Agent) 4. The class to which the proxy object belongs, subject to the corresponding protocol ( promise to work) 5. Implement the method in the Protocol (agent knows how to work) 6. The client notifies the proxy object to execute the corresponding method in the protocol (let the agent work) * * @return < #return value description#>*///1. Define the protocol, touch the event to the agent to complete@protocol Touchviewdelegat <NSObject>@optional//an optional- (void) Handletouchbegin: (TouchView *) Aview;//corresponding Touchbegan timing- (void) handletouchended: (TouchView *) Aview;//corresponding touchended timing- (void) handletouchmoved: (TouchView *) Aview;//corresponding touchmoved timing- (void) Handletouchcancle: (TouchView *) Aview;//the corresponding touchcancle time@end @interface Touchview:uiview//defining proxy properties, storing proxy objects for external settings@property (nonatomic, assign) id<touchviewdelegat>Delegate; @end

touchview.m

#import"TouchView.h"@implementation TouchView- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{    //6. Let the agent perform the task//judge the agent when the implementation of the optional method, the implementation of the representative back to achieve, want to do the corresponding operation, if not implemented-the representative does not want to implement the optional method    if([Self.Delegaterespondstoselector: @selector (handletouchbegin:)]) {        //How to have the agent perform the response[Self.DelegateHandletouchbegin:self]; }}- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{    }- (void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event{    }- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{} @end

Touch Events (TouchView) in iOS-(proxy response)-(push button for touch)

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.