码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中的触摸事件(TouchView) - (代理响应) - (实现touch的按钮化)

时间:2015-08-27 22:33:57      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:

#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.m

#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.h

#import <UIKit/UIKit.h>

@interface ActionView : UIView
//给外界提供接口,获取触摸响应的目标(target),以及响应的方法(action)

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

ActionView.m

#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

 

iOS中的触摸事件(TouchView) - (代理响应) - (实现touch的按钮化)

标签:

原文地址:http://www.cnblogs.com/wohaoxue/p/4764749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!