iOS中的觸摸事件(TouchView) - (代理響應) - (實現touch的按鈕化)(target/action設計模式,代理設計模式)重點

//兩種設計模式詳解
//target/action設計模式
1.給當前的視圖添加觸摸響應事件,
2.當觸摸時響應

//代理設計模式
 協議和代理的使用;
     當自定義協議時的使用步驟: 126是在委託方實現的,345是在代理方實現的
      1.定義協議(協議中存儲代理應該完成的任務)
1.在touchView定義代理協議

      2.定義代理屬性(存儲外界設置代理對象)
2 .在touchView中定義代理屬性
      3.在其餘文件中指定代理對象(給協議設置代理)
      4.代理對象所屬的類,服從對應的協議 (答應幹活)
      5.實現協議中的方法(代理知道怎麼幹活)

      6.委託方通知代理對象執行協議中對應的方法(讓代理什麼時候幹活)
 *



#import "RootViewController.h" #import "ActionView.h" #import "UIColor+MyUIColor.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; //self指的是當前對象 RootViewController //NSLog(@"%@",self); ActionView *actionViewYellow = [[ActionView alloc] initWithFrame:CGRectMake(30, 30, 260, 104)]; actionViewYellow.tag = 100; //給當前View添加響應事件,實現View中的接口給ActionView傳入響應的參數,self是視圖控制器 [actionViewYellow addTarget:self action:@selector(changeActionViewColor:)]; [self.view addSubview:actionViewYellow]; actionViewYellow.backgroundColor = [UIColor yellowColor]; [actionViewYellow release]; ActionView *actionViewGree = [[ActionView alloc] initWithFrame:CGRectMake(30, 150, 260, 104)]; actionViewGree.tag = 200; [actionViewGree addTarget:self action:@selector(changeSuperViewColor:)]; [self.view addSubview:actionViewGree]; actionViewGree.backgroundColor = [UIColor blueColor]; [actionViewGree release]; ActionView *actionViewGray = [[ActionView alloc] initWithFrame:CGRectMake(30, 260, 260, 104)]; actionViewGray.tag = 300; [actionViewGray addTarget:self action:@selector(changeselfViewFrame:)]; [self.view addSubview:actionViewGray]; actionViewGray.backgroundColor = [UIColor grayColor]; [actionViewGray release]; ActionView *actionViewRed= [[ActionView alloc] initWithFrame:CGRectMake(30, 400, 260, 104)]; actionViewRed.tag = 400; [self.view addSubview:actionViewRed]; actionViewRed.backgroundColor = [UIColor redColor]; [actionViewRed release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark --- target action //修改自身顏色 - (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 + 100, arc4random() % 301 + 100); } @end

DelegateViewController.h設計模式

#import <UIKit/UIKit.h>
#import "TouchView.h"
//4.服從協議
@interface DelegateViewController : UIViewController<TouchViewDelegat>

@end

DelegateViewController.mdom

#import "DelegateViewController.h"
#import "TouchView.h"
#import "UIColor+MyUIColor.h"
@interface DelegateViewController ()

@end

@implementation DelegateViewController
//5.實現協議中的方法
#pragma mark --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(20, 50, 280, 100)];
    [self.view addSubview:redView];
    //3.爲redView設置代理對象
    redView.delegate = self;
    redView.backgroundColor = [UIColor redColor];
    [redView release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ActionView.hatom

#import <UIKit/UIKit.h>

@interface ActionView : UIView
//給外界提供接口,獲取觸摸響應的目標(target),以及響應的方法(action)

- (void)addTarget:(id)target action:(SEL)action;
@end

ActionView.mspa

#import "ActionView.h"
#import "UIColor+MyUIColor.h"
@interface ActionView ()
{
    //ActionView 接受從控制器重傳來的響應目標View對象和響應方法
    id _target; // 存儲響應的目標
    SEL _action; // 存儲響應的方法

}
@end

@implementation ActionView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    //self.backgroundColor = [UIColor randomColor];
//   
//    switch (self.tag) {
//        case 100:
//            self.backgroundColor = [UIColor randomColor];
//            break;
//        case 200:
//            self.superview.backgroundColor = [UIColor randomColor];
//            break;
//        case 300:
//            self.center = CGPointMake(arc4random() % 101 + 100, arc4random() % 301 + 100);
//            break;
//            
//        case 400:
//            self.bounds = CGRectMake(0, 0, arc4random() % 101 + 160 , arc4random() % 51 + 54);
//            break;
//        default:
//
//            break;
//    }
    //當ActionView接收到觸摸時間以後,通知target執行action方法
    //WithObject是_action的參數
    //當觸摸到這個View對象的時候 調用存儲對象的指定方法
    //self指定的_action參數也是點擊的視圖 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{
    
}
//以前操做ActionView的處理方式不夠靈活,每次建立一個新的視圖,以及對應的不一樣時間時,都要修改touchBegin方法的源代碼,給相應的視圖添加處理事件,太麻煩
//此時經過target /action設計模式,讓ActionView象UIButton同樣靈活




//給外界提供接口,獲取觸摸響應的目標(target),以及響應的方法(action)
- (void)addTarget:(id)target action:(SEL)action
{
    //保存外界指定的響應目標(target)以及行爲(action)
    _target = target;
    _action = action;
}


@end

TouchVew.h設計

#import <UIKit/UIKit.h>
@class TouchView;
/**
 *  協議和代理的使用;
     當自定義協議時的使用步驟:
      1.定義協議(協議中存儲代理應該完成的任務)
      2.定義代理屬性(存儲外界設置代理對象)
      3.在其餘文件中指定代理對象(給協議設置代理)
      4.代理對象所屬的類,服從對應的協議 (答應幹活)
      5.實現協議中的方法(代理知道怎麼幹活)
      6.委託方通知代理對象執行協議中對應的方法(讓代理什麼時候幹活)
 *
 *  @return <#return value description#>
 */
//1.定義協議,觸摸事件交由代理完成
@protocol TouchViewDelegat <NSObject>

@optional // 可選的
- (void)handleTouchBegin:(TouchView *)aView; // 對應touchBegan時機

- (void)handleTouchEnded:(TouchView *)aView; // 對應touchEnded時機

- (void)handleTouchMoved:(TouchView *)aView; // 對應touchMoved時機

- (void)handleTouchCancle:(TouchView *)aView; //對應的touchCancle時機
@end
@interface TouchView : UIView
//定義代理屬性,存儲外界設置的代理對象
@property (nonatomic, assign) id<TouchViewDelegat> delegate;
@end

TouchView.m代理

#import "TouchView.h"

@implementation TouchView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //6.讓代理執行任務
    //判斷代理時候實現了可選的方法,實現了表明回去實現,想要去作對應的操做,若是沒有實現--表明不想實現該可選方法
    if ([self.delegate respondsToSelector:@selector(handleTouchBegin:)]) {
        //讓代理執行響應的方法
        [self.delegate handleTouchBegin:self];
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
@end
相關文章
相關標籤/搜索