UI day 5 target...action模式 代理模式 手势识别器

                                   第一种模式 target …… .action
第一步
//创建ActionViewController对象
   
ActionViewController  *actionVC = [[ActionViewController alloc]init];
   
//actionVC指定为self.window的根视图控制器
   
self.window.rootViewController = actionVC;
   
//释放
    [actionVC 
release];

第二步
 //改变自身颜色
   
ActionView  *redView = [[ActionView alloc]initWithFrame:CGRectMake(20, 30, 280, 100)];
    redView.
backgroundColor = [UIColor redColor];
    redView.
tag =101;
    [redView
addTarget:self action:@selector(changeSelfBackgroundColor:)];
    [
self.view  addSubview:redView];
    redView.
layer.cornerRadius = 30;
    [redView
release];
   
   
   
//改变父视图的颜色
   
ActionView  *greenView = [[ActionView alloc]initWithFrame:CGRectMake(20,150, 280, 100)];
    greenView.
backgroundColor = [UIColor greenColor];
    greenView.
tag = 102;
   
   
    [greenView
addTarget:self action:@selector(changeSuperBackgroundColor:)];
    [
self.view  addSubview:greenView];
    greenView.
layer.cornerRadius = 30;
    [greenView
release];
   
   
//改变自身位置
       
ActionView  *grayView = [[ActionView alloc]initWithFrame:CGRectMake(20, 270, 280, 100)];
    grayView.
backgroundColor = [UIColor grayColor];
    grayView.
tag = 103;
    [grayView 
addTarget:self action:@selector(changeSelfLoction:)];
    grayView.
layer.cornerRadius = 30;
    [
self.view  addSubview:grayView];
    [grayView
release];



添加一个UIColor分类,来获取随机颜色
(1).h中方法的声明
+ (UIColor *)randomColor;
(2).m方法的实现
定义一个宏
#define kColorValue  arc4random_uniform(256)/255.0
+ (UIColor *)randomColor{
    return [UIColor colorWithRed:kColorValue green:kColorValue blue:kColorValue alpha:1.0];
 
   
第三步
#pragma mark  ---Target...action设计模式的方法实现

//改变自身的背景
- (
void)changeSelfBackgroundColor:(ActionView *)view
{
    view.
backgroundColor  = [UIColor randomColor];

}

//改变父视图的背景
- (
void)changeSuperBackgroundColor:(ActionView *)view
{
    view.
superview.backgroundColor = [UIColor randomColor];
}

//改变自身位置
- (
void)changeSelfLoction:(ActionView *)view
{
    view.
frame = CGRectMake(20, arc4random_uniform(468-100+1)+100, 280, 100) ;
    view.
backgroundColor = [UIColor randomColor];
}

第四步
创建一个ActionView子类的来继承UIView类
(1) 在.h中写方法的声明
//给外界提供一个接口(方法)用来给actionView指定事件的响应对象(目标,target),以及target目标响应的方法;
- (void)addTarget:(id)target action:(SEL)action;

(2)在.m中写方法的实现

@interface ActionView ()
{
   
id _target;//存储传人的响应对象
   
SEL _action;//存储响应对象执行的方法
}

@end

#import "ActionView.h"
#import
"UIColor+randomColor.h"


@interface ActionView ()
{
   
id _target;//存储传人的响应对象
   
SEL _action;//存储响应对象执行的方法
}

@end


@implementation ActionView
/*
 ActionView
是自定义的视图,我们想把它封装为一个完整的类,一个完整的类就是无论你在有甚么样的需要,都无需去修改它的源文件
 
以前的处理方式不够灵活,因为actionView创建的对象没接收到触摸消息都要自己去处理事件,所有没创建一个对象,提出一个新的需求都要去修改它的源文件,此时,actionView对象就捆绑到一起了,此时耦合性太强了
 
通过target...action设计模式,将事件交给其他对象处理,让我们的actionViewUIButton一样灵活,此时它只需要负责通知目标对象干活就可以了,此时的ActionView对象和事件的就不在捆绑到一起,耦合性降低了,内聚就升高了
 
 */


- (
void)addTarget:(id)target action:(SEL)action{
   
//此时在这个方法中要把外界传进来的目标对象,和目标对象要执行的方法存储起来
   
_target = target;
   
_action = action;
}
//开始触摸

-(
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
   
//actionView对象接收到触摸的事件的时候,自己不处理事件,需要有_target来处理;
   
//目标选择方法去为self执行事件,而且方法中如果有参数,参数就指的是addTarget...action  方法的调用者;
    [_target performSelector:_action withObject:self];
  
}

//移动触摸
-(
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
                             第二种模式  代理模式delegate
1. 使用代理和协议的设计模式,完成 touchView 的触摸事件的响应操作,其实就是通过这种方式解除事件和 touchView 的耦合
 使用代理和协议的步骤
 1.
制定协议,(代理要完成的任务)
 2.
定义代理属性(存储代理对象)
 3.
在其他的文件中指定touchView的代理对象(帮touchView干活)
 4.
让代理对象遵循协议(代理对象答应帮touchView)干活
 5.
让代理对象实现协议中的方法(代理对象知道怎么去干活)
 6.委托人通知代理对象什么时候执行协议中的方法(通知代理去干活)

2. 创建 DelegateViewController 对象
    DelegateViewController  *delegateVC = [[DelegateViewController alloc]init];
   
//delegateVC指定为self.window的根视图控制器
   
self.window.rootViewController = delegateVC;
    [delegateVC
release];

3.  首先创建一个TouchView子类来继承UIView
(1)第一步   制定协议      在TouchView.h中写协议的制定

@class TouthView;

第一步制定协议
@protocol TouchViewDelegate <NSObject>
@optional
//刚开始触摸的时候让代理对象完成这个方法
- (
void)touchBeganWithTouchView:(TouthView *)touchView;

//结束触摸的时候让代理完成这个方法
- (
void)touchEndWithTouchView:(TouthView *)touchView;

//触摸移动的时候让代理完成这个方法
- (
void)touchMoveWithTouchView:(TouthView *)touchView;

//触摸终端的时候让代理完成这个方法
- (
void)touchCancelledWithTouchView:(TouthView *)touchView;

@end

(2)第二步   制定代理属性
//第二步,代理属性
@property (nonatomic,assign)id<TouchViewDelegate>delegate;

(3)第三步   指定代理对象
#import "DelegateViewController.h"
#import "TouthView.h"
TouthView  *redView = [[TouthView alloc]initWithFrame:CGRectMake(20, 30, 280, 100)];
    redView.
backgroundColor = [UIColor redColor];
    redView.
layer.cornerRadius = 30;
   
//第三步 指定代理对象
    redView.
delegate  = self;
   
    [
self.view addSubview:redView];
    [redView
release];

(4)第四步    让代理对象遵循协议
//第四步 让代理对象遵从协议
@interface DelegateViewController ()< TouchViewDelegate >
@end

(5)第五步   实现协议中的方法

//5 实现协议中的方法
- (
void)touchBeganWithTouchView:(TouthView *)touchView
{
   
//修改自身的颜色
    touchView.
backgroundColor = [UIColor randomColor];
}
- (void)touchEndWithTouchView:(TouthView *)touchView
{
   
//修改自身父视图的颜色
    touchView.
superview.backgroundColor = [UIColor randomColor];
}
- (
void)touchMoveWithTouchView:(TouthView *)touchView
{
}

(6)第六步    让代理对象干活 在TouthView.m中实现


-(
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    self.backgroundColor = [UIColor greenColor];
   
//6 让代理对象干活
   
if ([_delegate respondsToSelector:@selector(touchBeganWithTouchView:)]) {
        [
_delegate touchBeganWithTouchView:self];
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   
//判断代理对象有没有响应对应的方法
   
if ([_delegate respondsToSelector:@selector(touchMoveWithTouchView:)]) {
       
//通知代理执行协议中的方法
        [
_delegate touchMoveWithTouchView:self];
    }
}

-(
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   
if ([_delegate respondsToSelector:@selector(touchEndWithTouchView:)]) {
        [
_delegate  touchEndWithTouchView:self];
    }
}

-(
void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   
if ([_delegate respondsToSelector:@selector(touchCancelledWithTouchView:)]) {
        [
_delegate touchCancelledWithTouchView:self];
    }
}




                                    手势识别器

第一步
 //创建GestureViewController对象
   
GestureViewController  *gestureVC = [[GestureViewController alloc]init];
   
//gestureVC指定为self.window的根视图控制器
   
self.window.rootViewController = gestureVC;
   
//释放
    [gestureVC
release];

第二步
  UIGestureRecognizer 手势识别器的基类,他为我们提供了一些手势识别器的一些功能,我们在屏幕上的操作全部有手势识别器完成识别;此时我们只需要关心手势识别之后应该做出什么处理,他有 6 大子类,还有一个孙子类(屏幕边缘手势,它是平移手势的子类)
   
   
UIView  *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
    redView.
backgroundColor = [UIColor randomColor];
    redView.
layer.cornerRadius = 30;
    [
self.view addSubview:redView];
    [redView
release];
   
(1)轻拍手势
 UITapGestureRecognizer轻拍的手势类
UITapGestureRecognizer   *tapGesture = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (handleTap:)];
    //配置属性
   
//设置轻拍手势触发事件所需的轻拍次数
    tapGesture.numberOfTapsRequired = 1;
    //设置轻拍需要的手指个数
    tapGesture.
numberOfTouchesRequired = 2;
   
//redView添加轻拍的手势对象
    [redView   addGestureRecognizer:tapGesture];
    [tapGesture release];
#pragma mark  轻拍手势的方法实现
- (void)handleTap:(UITapGestureRecognizer *)tapGesture
{
    //获取手势对象所在的视图
    tapGesture.view.backgroundColor = [UIColor randomColor];
}

(2)长按手势
UILongPressGestureRecognizer
UILongPressGestureRecognizer *longGestuer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLangPress:)];
    //设置长按手势最小多长时间触发
    longGestuer.
minimumPressDuration = 1;
   
//在视图上添加长按手势对象
    [redView
addGestureRecognizer:longGestuer];
    [longGestuer release];
#pragma mark 长按手势的方法实现
- (void)handleLangPress:(UILongPressGestureRecognizer *)longGesture
{
   
//改变手势所在的视图的父视图颜色
   
//根据手势的状态,选择执行相应的操作
   
if (longGesture.state == UIGestureRecognizerStateBegan) {
         longGesture.
view.backgroundColor = [UIColor randomColor];
    }
}

(3)清扫手势
 UISwipeGestureRecognizer
UISwipeGestureRecognizer  *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleLeftSwipe:)];
   
    // 设置清扫的方向   direction清扫方向
    swipeGesture.direction UISwipeGestureRecognizerDirectionLeft;
    [redView 
addGestureRecognizer:swipeGesture];
    [swipeGesture
release];
   
   
   
UISwipeGestureRecognizer  *swipeGesture2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleRightSwipe:)];
   
   
//设置清扫的方向
    swipeGesture2.
direction UISwipeGestureRecognizerDirectionRight;
    [redView 
addGestureRecognizer:swipeGesture2];
    [swipeGesture2
release];

#pragma mark  清扫手势的实现
- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)swipeGertrue
{
   
//向左清扫时改变自身的颜色
    swipeGertrue.
view.backgroundColor = [UIColor randomColor];
}

(4)平移手势
UIPanGestureRecognizer  *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[redView  addGestureRecognizer:panGesture];
[panGesture release];

#pragma mark  实现平移手势 方法一
- (void)handlePan:(UIPanGestureRecognizer *)panGesture
{
    //1.获取平移增量
   
CGPoint point = [panGesture translationInView:panGesture.view];
   
//2.仿射变换  transform就是变身
    //1.变形之前的位置  2.x轴上的形变量  3.y轴上的形变量
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
    //3.将之前的增量清零 CGPointZero代表(00)  CGPointMake{00}
    [panGesture 
setTranslation:CGPointZero inView:panGesture.view];
}

#pragma mark  实现平移手势 方法二
- (void)handlePan:(UIPanGestureRecognizer *)panGesture
{
    CGPoint point = [pan  translationInView: panGesture .view];
    NSLog(@"pan = %@",NSStringFromCGPoint(point));//打印偏移量
    panGesture .view.transform CGAffineTransformMakeTranslation(point.x, point.y);
}







(5)捏合手势
UIPinchGestureRecognizer
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[redView addGestureRecognizer :pinchGesture];
 pinchGesture release];

#pragma mark 实现捏合手势 方法一
- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGesture
{
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
    // 将之前的比例置为 1  scale   缩放 比例
    pinchGesture. scale = 1 ; 如果不把scale置为1,当两个点重合时,程序会发生崩溃
}

#pragma mark 实现捏合手势 方法二

- (void)handlePinch:(UIPinchGestureRecognizer *)pinchGesture
{
    NSLog(@"scale = %f", pinchGesture .scale);//打印缩放比例
    pinchGesture . view . transform = CGAffineTransformMakeScale ( pinchGesture . scale , pinchGesture . scale );
}





(6)旋转手势
UIRotationGestureRecognizer *rotantionGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotation:)];
[redView addGestureRecognizer:rotantionGesture];
 [rotantionGesture release];

#pragma mark 实现旋转的手势  方法一
- (void)handleRotation:(UIRotationGestureRecognizer *)rotantionGesture
{
    rotantionGesture.
view.transform = CGAffineTransformRotate(rotantionGesture.view.transform, rotantionGesture.rotation);
   
//将之前的角度增量之为0
    rotantionGesture.
rotation = 0;
}


#pragma mark 实现旋转的手势  方法二

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
   
NSLog(@"rotation = %f",rotation.rotation);//打印弧度
    rotation.
view.transform = CGAffineTransformMakeRotation(rotation.rotation);
}






(7)屏幕边缘手势
//屏幕边缘手势作用的视图必须和硬件设备的边缘重合,此时这个手势才有作用
UIScreenEdgePanGestureRecognizer *screenGesture = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreen:)];
[redView  addGestureRecognizer :screenGesture];
    //设置屏幕的边缘
 screenGesture.edges = UIRectEdgeLeft;
  [screenGesture release];

#pragma mark  实现屏幕边缘的手势
- (void)handleScreen:(UIScreenEdgePanGestureRecognizer *)screen
{
   
NSLog(@"小燕你能行吗?");
}

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值