设计模式,手势识别器

target/action设计模式

类为委托方
控制器为代理方
方法为协议

//.h文件
#import <UIKit/UIKit.h>

@interface TapView : UIView

@property (nonatomic,assign) id target;
@property (nonatomic,assign) SEL action;

@end
//视图的.m文件
#import "TapView.h"
@implementation TapView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //这里负责调用
    //外界传什么方法,我就执行什么方法,不需要知道方法到底是怎么实现的.
    //如果外界的方法有参数,那么这个参数一定是TapView*类型的
    [_target performSelector:self.action withObject:self];
}
//根视图控制器控制
//控制器的.m文件
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    TapView *tap1 = [[TapView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    tap1.backgroundColor = [UIColor redColor];
    [self.view addSubview:tap1];
    tap1.tag = 1001;
    tap1.target = self;     //target=控制器
    tap1.action = @selector(randomColor:);//选择要触发的事件
    [tap1 release];
    }

代理设计模式

控制有一些是时间点,控制器可以实现这个代理方法,以便在适应的时候做适应的事
对于一些时间点,想让我的代理对象做什么事

//代理设计模式要先写一个协议
//协议名称以类名开头加上Delegate .h文件
@protocol TapViewDelegate <NSObject>
@optional
//当视图刚开始被触摸的时候,代理方要去实现的方法.
- (void)tapViewBegan:(TapView *)tapView;
//当视图在拖动过程中,想要代理方要去实现方式
- (void)tapViewMove:(TapView *)tapView;
//当视图结束触摸的时候,代理方要去实现的方法
- (void)tapViewEnd:(TapView *)tapView;
@end
@interface TapView : UIView
@property (nonatomic,assign) id<TapViewDelegate> detegate; //设计接收协议的代理对象
@end
//.m文件 如果代理不为空,切响应了某个事件 就触发 代理一般用在时间点上
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_detegate != nil && [_detegate respondsToSelector:@selector(tapViewBegan:)])
    {
         [_detegate tapViewBegan:self];
    }
   
}

UIImageView

//写法1:创建一个图片视图
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2753441426829430"]];
    imageView.frame = self.view.bounds ;
    [self.view addSubview:imageView];
//写法2:创建一个gif视图    
//如何让UIImageView播放多张图片,GIF动态图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 150, 350, 350)];
    NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:4];
    for (int i = 1; i <= 4; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"w12%d.tiff",i];
        UIImage *image = [UIImage imageNamed:imageName];
        [imageArray addObject:image];
    }
    
    imageView.animationImages = imageArray;//指定需要做动画的图片
    imageView.animationDuration = 0.5;//设置播放的时间 速率
    imageView.animationRepeatCount = 0; //设置循环次数
    [self.view addSubview:imageView];
    [imageView startAnimating];//让其开始播放
    [imageView release];   
    //写法3:通过图片路径加载图片
        //通过文件路径来加载一个图片
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"/Users/anhao/Desktop/2753441426829430.png"]];
    imageView1.frame = self.view.bounds ;
    [self.view addSubview:imageView1];
 

手势识别器

    //创建一个点按轻拍的手势识别对象
    //轻拍手势识别器,一旦识别轻拍手势,就会去执行self中的tap:方法,完全可以点一下执行多个方法!!但是我们一般不加
    UITapGestureRecognizer *tapG =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    tapG.numberOfTapsRequired = 2; //点几下
    tapG.numberOfTouchesRequired = 1;//几个手指  
     //创建一个旋转手势
    UIRotationGestureRecognizer *rogR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotage:)];  
//注意 注意啦  这个地方 UILabel 和 UIImageView必须加这句
    imageView.userInteractionEnabled = YES;
//    [imageView addGestureRecognizer:tapG];
    [imageView addGestureRecognizer:rogR];
    [imageView release];  
 posted on 2015-04-26 21:47  ianhao_cn  阅读(187)  评论(0编辑  收藏  举报