⼀、target/action设计模式
⼆、代理设计模式
三、UIImageView
四、⼿势识别器

target/action设计模式

耦合是衡量⼀个程序写的好坏的标准之⼀,
耦合是衡量模块与模块之间关联程度的指标
“⾼内聚,低耦合”是⾯向对象编程的核⼼思想。高内聚:功能上强大,低耦合:就是与其他类的关联性程度低

(注意:在定义代理的属性的时候属性的属性要用 assign)

手势识别的基类:UIGestureRecognizer 提供了手势识别的基本功能,他有7个手势 (轻拍手势、长按手势、轻扫手势、平移手势、捏合手势、旋转、边缘旋转)

1.轻怕手势 UITapGestureRecognizer

UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackgroundColor:)];

tapGesture.numberOfTapsRequired = 2;//点两次

tapGesture.numberOfTouchesRequired = 2;//需要两个手指

[aView addGestureRecognizer:tapGesture];

[tapGesture release];

2.长按手势   UILongPressGestureRecognizer

UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackgroundColor:)];

longPress.numberOfTapsRequired = 2;//拍两次

longPress.numberOfTouchesRequired = 2;//需要两个手指

[aView addGestureRecognizer:longPress];

[aView addSubview:longPress];错误写法?

[longPress release];

3.轻扫手势   UISwipeGestureRecognizer

UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

//设置轻扫的方向

//轻扫

//一次最多只能识别两种手势,默认的方向是向右轻扫

//上下不能在一起 左右不能在一起

swipe.direction = UISwipeGestureRecognizerDirectionRight;//向右

UISwipeGestureRecognizer * leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleLeftSwip:)];

leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;//向左

UISwipeGestureRecognizer * uPSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleUpSwip:)];

uPSwipe.direction = UISwipeGestureRecognizerDirectionUp;

UISwipeGestureRecognizer * DownSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleDownSwip:)];

DownSwipe.direction = UISwipeGestureRecognizerDirectionDown;

[greenView addGestureRecognizer:leftSwipe];

[greenView addGestureRecognizer:uPSwipe];

[greenView addGestureRecognizer:DownSwipe];

[greenView addGestureRecognizer:swipe];

[swipe release];

[leftSwipe release];

[uPSwipe release];

[DownSwipe release];

4.平移手势  UIPanGestureRecognizer

UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

[greenView addGestureRecognizer:pan];

[pan release];

5.捏合手势  UIPinchGestureRecognizer

UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];

[aView addGestureRecognizer:pinch];

[pinch release];

6.旋转      UIRotationGestureRecognizer

UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotate:)];

[aView addGestureRecognizer:rotate];

[rotate release];

7.边缘旋转   UIScreenEdgePanGestureRecognizer

UIScreenEdgePanGestureRecognizer * screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenPan:)];

[aView addGestureRecognizer:screenPan];

screenPan.edges = UIRectEdgeLeft;//如果改为右边的话,本例不能实现,因为视图不在当前窗口的边缘 (不要改为 上 和 下 他们分别是:通知)

[screenPan release];

//上面的对应的方法如下代码

//边缘平移
-(void)handleScreenPan:(UIScreenEdgePanGestureRecognizer *)screenPan{screenPan.view.backgroundColor = RandomColor;//改变位置 (是 平移的子类 所以也可以平移位置)CGPoint point = [screenPan translationInView:screenPan.view];screenPan.view.transform = CGAffineTransformTranslate(screenPan.view.transform, point.x, point.y);[screenPan setTranslation:CGPointZero inView:screenPan.view];
}
-(void)handleScreenPan2:(UIScreenEdgePanGestureRecognizer *)screenPan{screenPan.view.backgroundColor = RandomColor;CGPoint point = [screenPan translationInView:screenPan.view];screenPan.view.transform = CGAffineTransformTranslate(screenPan.view.transform, point.x, point.y);[screenPan setTranslation:CGPointZero inView:screenPan.view];
}
//旋转
-(void)handleRotate:(UIRotationGestureRecognizer *)rotate{rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);//将角度置换为0rotate.rotation = 0;
}//捏合
-(void)handlePinch:(UIPinchGestureRecognizer *)pinch{pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);//将之前的缩放比置为1(还原)[pinch setScale:1.0];
}//平移
-(void)handlePan:(UIPanGestureRecognizer *)pan{//获取平移的增量CGPoint point = [pan translationInView:pan.view];//视图移动之后的位置变化pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);//将之前的清除掉
    [pan setTranslation:CGPointZero inView:pan.view];
}//翻页 (注意不能)
-(void)handleLeftSwip:(UISwipeGestureRecognizer*)swipeTag{swipeTag.view.superview.backgroundColor = RandomColor;
}
-(void)handleUpSwip:(UISwipeGestureRecognizer*)swipeTag{swipeTag.view.superview.backgroundColor = RandomColor;
}
-(void)handleDownSwip:(UISwipeGestureRecognizer*)swipeTag{swipeTag.view.superview.backgroundColor = RandomColor;
}
-(void)handleSwipe:(UISwipeGestureRecognizer*)swipeTag{NSLog(@"456");swipeTag.view.superview.backgroundColor = RandomColor;
}-(void)handleTap:(UITapGestureRecognizer *)tap{UIView * aView = tap.view;aView.bounds = CGRectMake(0, 0, (arc4random()%256/255.0)*self.view.frame.size.width, (arc4random()%256/255.0)*self.view.frame.size.height);
}-(void)handleLongPress:(UILongPressGestureRecognizer *)LongTap{UIView * longView = LongTap.view;longView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
-(void)changeViewBackgroundColor:(UITapGestureRecognizer *)tap{UIView * aView = tap.view;aView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];NSLog(@"fff");
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}

View Code  上面对应的实现方法

代码:(手势)

#import "AppDelegate.h"
#import "RootViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// Override point for customization after application launch.self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];RootViewController * RecognizerVC = [[RootViewController alloc]init];self.window.rootViewController = RecognizerVC;[RecognizerVC release];//手势就是一组有规律的触摸return YES;
}

View Code AppDelegate.m文件

//
//  RootViewController.m

#import "RootViewController.h"#define RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0]@interface RootViewController ()@end@implementation RootViewController-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {//初始化方法一般不重写,需要初始化数据的时候才重写(如果要添加其他数据的时候)
    }return self;
}
- (void)viewDidLoad {[super viewDidLoad];/*UIGestureRecognizer 手势识别基类,提供了手势识别的基本功能,他有7个手势1.轻怕手势 UITapGestureRecognizer//设置轻拍的次数(默认为1)tap.numberOfTapsRequired = 1;//设置轻拍的手指个数(默认为1)tap.numberOfTouchesRequired = 2;*///    UIView * aView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 280, 280)];
//    aView.backgroundColor = [UIColor greenColor];
//    [self.view addSubview:aView];
//    [aView release];//轻拍事件
//    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackgroundColor:)];
//    tapGesture.numberOfTapsRequired = 2;//点两次
//    tapGesture.numberOfTouchesRequired = 2;//需要两个手指
//    [aView addGestureRecognizer:tapGesture];
//    [tapGesture release];//长按手势
//    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(changeViewBackgroundColor:)];
//    longPress.numberOfTapsRequired = 2;//拍两次
//    longPress.numberOfTouchesRequired = 2;//需要两个手指
//    [aView addGestureRecognizer:longPress];
//    [longPress release];//    UIView * redView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 120, 120)];
//    redView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:redView];
//    [redView release];
//    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
//    [redView addGestureRecognizer:tap];
//    [tap release];
//    //设置轻拍的次数(默认为1)
//    tap.numberOfTapsRequired = 1;
//    //设置轻拍的手指个数(默认为1)
//    tap.numberOfTouchesRequired = 2;
//    UIView * greenView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//    greenView.backgroundColor = [UIColor greenColor];
//        [self.view addSubview:greenView];
//    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
////    [greenView addSubview:longPress];错误写法?
//    longPress.minimumPressDuration = 0.5;//默认是0.5秒
//    [greenView addGestureRecognizer:longPress];
//    [longPress release];
//    [greenView release];/*//轻扫手势UIView * greenView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 120, 120)];greenView.backgroundColor = [UIColor greenColor];[self.view addSubview:greenView];[greenView release];UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];//设置轻扫的方向//轻扫//一次最多只能识别两种手势,默认的方向是向右轻扫//上下不能在一起 左右不能在一起swipe.direction = UISwipeGestureRecognizerDirectionRight;//向右UISwipeGestureRecognizer * leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleLeftSwip:)];leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;//向左UISwipeGestureRecognizer * uPSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleUpSwip:)];uPSwipe.direction = UISwipeGestureRecognizerDirectionUp;UISwipeGestureRecognizer * DownSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleDownSwip:)];DownSwipe.direction = UISwipeGestureRecognizerDirectionDown;[greenView addGestureRecognizer:leftSwipe];[greenView addGestureRecognizer:uPSwipe];[greenView addGestureRecognizer:DownSwipe];[greenView addGestureRecognizer:swipe];[swipe release];[leftSwipe release];[uPSwipe release];[DownSwipe release];*///平移手势
//    UIView * greenView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 120, 120)];
//    greenView.backgroundColor = [UIColor grayColor];
//    [self.view addSubview:greenView];
//    [greenView release];
//    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
//    [greenView addGestureRecognizer:pan];
//    [pan release];//捏合UIView * aView = [[UIView alloc]initWithFrame:CGRectMake(4, 600, 280, 280)];aView.backgroundColor = [UIColor greenColor];[self.view addSubview:aView];[aView release];
//    UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
//    [aView addGestureRecognizer:pinch];
//    [pinch release];//旋转
//    UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotate:)];
//    [aView addGestureRecognizer:rotate];
//    [rotate release];//边缘平移 IOS 7.0之后,视图的位置必须在你的屏幕的边缘UIScreenEdgePanGestureRecognizer * screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenPan:)];[aView addGestureRecognizer:screenPan];screenPan.edges = UIRectEdgeLeft;//如果改为右边的话,本例不能实现,因为视图不在当前窗口的边缘 (不要改为 上 和 下 他们分别是:通知)
    [screenPan release];UIScreenEdgePanGestureRecognizer * screenPan2 = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenPan:)];[aView addGestureRecognizer:screenPan2];screenPan2.edges = UIRectEdgeRight;[screenPan2 release];// Do any additional setup after loading the view.
}
#pragma mark-----------手势响应事件------------------
//边缘平移
-(void)handleScreenPan:(UIScreenEdgePanGestureRecognizer *)screenPan{screenPan.view.backgroundColor = RandomColor;//改变位置 (是 平移的子类 所以也可以平移位置)CGPoint point = [screenPan translationInView:screenPan.view];screenPan.view.transform = CGAffineTransformTranslate(screenPan.view.transform, point.x, point.y);[screenPan setTranslation:CGPointZero inView:screenPan.view];
}
-(void)handleScreenPan2:(UIScreenEdgePanGestureRecognizer *)screenPan{screenPan.view.backgroundColor = RandomColor;CGPoint point = [screenPan translationInView:screenPan.view];screenPan.view.transform = CGAffineTransformTranslate(screenPan.view.transform, point.x, point.y);[screenPan setTranslation:CGPointZero inView:screenPan.view];
}
//旋转
-(void)handleRotate:(UIRotationGestureRecognizer *)rotate{rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);//将角度置换为0rotate.rotation = 0;
}//捏合
-(void)handlePinch:(UIPinchGestureRecognizer *)pinch{pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);//将之前的缩放比置为1(还原)[pinch setScale:1.0];
}//平移
-(void)handlePan:(UIPanGestureRecognizer *)pan{//获取平移的增量CGPoint point = [pan translationInView:pan.view];//视图移动之后的位置变化pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);//将之前的清除掉
    [pan setTranslation:CGPointZero inView:pan.view];
}//翻页 (注意不能)
-(void)handleLeftSwip:(UISwipeGestureRecognizer*)swipeTag{swipeTag.view.superview.backgroundColor = RandomColor;
}
-(void)handleUpSwip:(UISwipeGestureRecognizer*)swipeTag{swipeTag.view.superview.backgroundColor = RandomColor;
}
-(void)handleDownSwip:(UISwipeGestureRecognizer*)swipeTag{swipeTag.view.superview.backgroundColor = RandomColor;
}
-(void)handleSwipe:(UISwipeGestureRecognizer*)swipeTag{NSLog(@"456");swipeTag.view.superview.backgroundColor = RandomColor;
}-(void)handleTap:(UITapGestureRecognizer *)tap{UIView * aView = tap.view;aView.bounds = CGRectMake(0, 0, (arc4random()%256/255.0)*self.view.frame.size.width, (arc4random()%256/255.0)*self.view.frame.size.height);
}-(void)handleLongPress:(UILongPressGestureRecognizer *)LongTap{UIView * longView = LongTap.view;longView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
-(void)changeViewBackgroundColor:(UITapGestureRecognizer *)tap{UIView * aView = tap.view;aView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];NSLog(@"fff");
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

View Code RootViewController.m文件

代码:( 设计模式 代理)

#import <UIKit/UIKit.h>@interface ActionView : UIView-(void)addTarget:(id)target action:(SEL)action;
@end//
//  ActionView.m

#import "ActionView.h"
#import "UIColor+Addtion.h"@interface ActionView ()
//延展功能:添加私有功能变量
{id  _target; //存储传入的对象SEL _action; //存储传入的响应事件
}
//此时的 ActionView 就相当于一个封装好的类,无论外界的需求如何的修改,我们都不需要修改内部的代码(这就是一个封装的比较好的)
@end
@implementation ActionView-(instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self) {self.multipleTouchEnabled = YES;
//        [self setLabel];//布局页面
//        [self setRTextField];
//        [self setButton];
    }return self;
}
//布局页面
//-(void)setLabel{
//
//}
//-(void)setRTextField{
//
//}
//-(void)setButton{
//
//}
//刚才的写法不好的地方很不灵活,现在我们要让外部选择实现内部的方法,因为ActionView 接触到外界的触摸事件后,是自己去处理触摸事件,所以每次提一个需求,他的内部都要去修改代码,(此时的耦合很大,关联很大),此时的ActionView 和触摸事件绑定在一起(即两者的耦合性高)
//我们可以模仿 Button的 通过 addTarget .... Action 设计模式  我们将事件的处理,不在内部去实现,我们吧处理交给其他对象去处理,而 Action 只负责通知外部由谁来实现。这样就降低了耦合性,提高了程序的内聚性
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//当接收到触摸事件的时候,自己不处理触摸事件,而是交给 _target 去处理//此时就是传进来的 _target 执行 action
    [_target performSelector:_action withObject:self];
//    NSLog(@"%@",self);
//    NSLog(@"%@",_target);
}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}-(void)addTarget:(id)target action:(SEL)action{_target = target; //存储局部的实例变量 使其在其他的方法里使用_action = action; //
}
@end

View Code  ActionView .h .m文件

#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end#import "AppDelegate.h"
#import "ActionViewController.h"
#import "DelegateViewController.h"
#import "MyselfViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// Override point for customization after application launch.self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];/*ActionViewController * VC = [[ActionViewController alloc]init];
//    NSLog(@"%@",VC);
//    self.window.rootViewController = VC;DelegateViewController * RootVC = [[DelegateViewController alloc]init];self.window.rootViewController = RootVC;[RootVC release];[VC release];*/MyselfViewController * mySelfVC = [[MyselfViewController alloc]init];self.window.rootViewController = mySelfVC;[mySelfVC release];return YES;
}

View Code AppDelegate.h .m文件

//
//  ActionViewController.m

#import "ActionViewController.h"
#import "ActionView.h"
#import "UIColor+Addtion.h"@interface ActionViewController ()@end@implementation ActionViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.ActionView * redView = [[ActionView alloc]initWithFrame:CGRectMake(40, 50, 300, 60)];redView.backgroundColor = [UIColor redColor];redView.tag = 101;//self 是当前视图控制器对象 (谁调用 self 所在的方法 self 就是指的就是谁)
    [redView addTarget:self action:@selector(changSelfColor:)];[self.view addSubview:redView];[redView release];ActionView * greenView = [[ActionView alloc]initWithFrame:CGRectMake(40, 110, 300, 60)];greenView.backgroundColor = [UIColor greenColor];greenView.tag = 102;[self.view addSubview:greenView];[greenView release];//blueView 改变自身的位置ActionView * blueView = [[ActionView alloc]initWithFrame:CGRectMake(40, 170, 300, 60)];[self.view addSubview:blueView];blueView.backgroundColor = [UIColor blueColor];
//    blueView addTarget:<#(id)#> action:<#(SEL)#>
    [blueView addTarget:self action:@selector(changePositation:)];[blueView release];//yellowView 改变自身的大小ActionView * yellowView = [[ActionView alloc]initWithFrame:CGRectMake(40, 230, 300, 60)];yellowView.backgroundColor = [UIColor yellowColor];[yellowView addTarget:self action:@selector(changeSelfSize:)];[self.view addSubview:yellowView];[yellowView release];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.

}/**/-(void)changSelfColor:(ActionView *)view{view.backgroundColor = [UIColor randUIColor];
//    view.frame.size = CGSizeMake((double)arc4random()%((self.view.frame.size.width -10 + 1)+10),     (double)arc4random()%((self.view.frame.size.height -10 + 1)+10));

}
-(void)changePositation:(ActionView *)view{view.center = CGPointMake(arc4random()%(51+300), arc4random()%(101+400));
//    NSLog(@"%@",self);
}
-(void)changeSelfSize:(ActionView *)view{
//点击改变大小int i = 0;i++;if (1 == i) {view.bounds = CGRectMake(0, 0,view.frame.size.width - arc4random()%101,view.frame.size.height - arc4random()% 101);}view.bounds = CGRectMake(0, 0,view.frame.size.width + arc4random()%101,view.frame.size.height + arc4random()% 101);
}
@end

View Code ActionViewController.m

#import "UIColor+Addtion.h"@implementation UIColor (Addtion)
+(UIColor *)randUIColor;{return [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
@end

View Code UIClolor+Addition.m

#import <UIKit/UIKit.h>
@class DelegateView;
//使用代理的设计模式 来进行解耦,代理去完成视图的触摸事件操作
/*当使用代理设计模式时候,具体的操作1.制定相关的协议(协议里存放的是代理人需要完成的任务)2.定义代理的属性 (存储代理对象)3.在其他类里指定代理对象4.让代理所在的类服从相应的协议5.代理对象实现协议中的方法6.通知代理去执行*///第一步:制定协议
@protocol TouchDelegate <NSObject>
@optional
-(void)touchBeginWithView:(DelegateView *)touchView;//对应触摸开始时触发
-(void)touchEndedWithView:(DelegateView *)touchView;//对应触摸结束时触发
-(void)touchMovedWithView:(DelegateView *)touchView;//对应触摸移动时触发
-(void)touchCancelledWithView:(DelegateView *)touchView;//对应触摸中断时触发
@required@end
@interface DelegateView : UIView
//第二步:定义代理的属性 语义属性 assign  服从上面设置好的协议
@property(nonatomic,assign) id<TouchDelegate>delegate;
@end

View Code DelegateView。h

//
//  DelegateView.m

#import "DelegateView.h"@implementation DelegateView
//第六步 通知代理的对象去执行协议的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//代理存在,并且已经选择这个方法if (_delegate && [self.delegate respondsToSelector:@selector(touchBeginWithView:)]) {[self.delegate touchBeginWithView:self];}
}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{if (_delegate && [self.delegate respondsToSelector:@selector(touchMovedWithView:)]) {[self.delegate touchMovedWithView:self];}
}-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{if (_delegate && [self.delegate respondsToSelector:@selector(touchCancelledWithView:)]) {[self.delegate touchCancelledWithView:self];}
}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{if (_delegate && [self.delegate respondsToSelector:@selector(touchEndedWithView:)]) {[self.delegate touchEndedWithView:self];}
}@end

View Code DelegateView.m

//
//  DelegateViewController.m
//
#import "DelegateViewController.h"
#import "UIColor+Addtion.h"//第四步,让当前类服从协议
@interface DelegateViewController ()<TouchDelegate>@end@implementation DelegateViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//布局页面DelegateView * redView = [[DelegateView alloc]initWithFrame:CGRectMake(40, 50, 300, 60)];redView.backgroundColor = [UIColor redColor];//第三步 指定代理的对象redView.delegate = self;redView.tag = 201;[self.view addSubview:redView];[redView release];DelegateView * greenView = [[DelegateView alloc]initWithFrame:CGRectMake(40, 110, 300, 60)];greenView.backgroundColor = [UIColor greenColor];greenView.tag = 202;[self.view addSubview:greenView];[greenView release];//blueView 改变自身的位置DelegateView * blueView = [[DelegateView alloc]initWithFrame:CGRectMake(40, 170, 300, 60)];blueView.tag = 203;[self.view addSubview:blueView];blueView.delegate = self;blueView.backgroundColor = [UIColor blueColor];
//    [blueView addTarget:self action:@selector(changePositation:)];
    [blueView release];//yellowView 改变自身的大小DelegateView * yellowView = [[DelegateView alloc]initWithFrame:CGRectMake(40, 230, 300, 60)];yellowView.backgroundColor = [UIColor yellowColor];yellowView.tag = 204;yellowView.delegate = self;
//    [yellowView addTarget:self action:@selector(changeSelfSize:)];
    [self.view addSubview:yellowView];[yellowView release];}
//第五步 实现代理的方法
#pragma mark--------- 实现代理的方法--------
-(void)touchBeginWithView:(DelegateView *)touchView{switch (touchView.tag) {case 201:{touchView.backgroundColor = [UIColor randUIColor];self.view.superview.backgroundColor = [UIColor randUIColor];}break;default:break;}}//触摸结束的时候改变 blueView 的位置
-(void)touchEndedWithView:(DelegateView *)touchView{switch (touchView.tag) {case 203:{NSLog(@"%ld",touchView.tag);touchView.center = CGPointMake(arc4random()%(51+300), arc4random()%(101+400));}break;default:break;}
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end

View Code DelegateViewController.m

#import <UIKit/UIKit.h>
#import "DelegateView.h"@interface DelegateViewController : UIViewController@end

View Code DelegateViewController.h

转载于:https://www.cnblogs.com/benpaobadaniu/p/4778329.html

UI:target-action设计模式、手势识别器相关推荐

  1. UI一揽子计划 5 (Target: Action:  、Protocol   Delegate、 UIImageView 、手势识别器)

    一.target/ action 设计模式      耦合是衡量⼀一个程序写的好坏的标准之一,      耦合是衡量模块与模块之间关联程度的指标      "高内聚,低耦合"是⾯面 ...

  2. target-action设计模式--主要为Button的方法重写

    新建两个类MainViewController/ButtonView ButtonView.h #import <UIKit/UIKit.h> @interface ButtonView ...

  3. iOS开发——手势识别器(用手势实现图片旋转和缩小放大)

    iOS开发中,除了有关触摸的这组方法来控制用户的手指触控外,还可以用UIGestureRecognize的衍生类来进行判断,方便了开发. UIGestureRecognize的子类类别有以下几种: U ...

  4. 154在屏幕中绘图时设置透明度(扩展知识:为图片视图添加点击手势识别器,来实现点击事件操作)...

    一张图片,通过混合模式绘制后,能得到不同效果的图片. 这里的示例仅是测试效果:实际上可以通过不同程度的混合模式绘制,来得到符合需求的效果. 效果如下: ViewController.h 1 #impo ...

  5. iOS常用手势识别器

    手势识别状态: typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureReco ...

  6. python手势识别控制幻灯片翻页系统_实战1.2-利用手势识别器,实现视图的手势控制...

    title: 实战1.2-利用手势识别器,实现视图的手势控制 知识预备 什么是仿射变换? 从视觉效果上来理解,仿射变换是通过一系列原子变换复合而来的变换.包括:平移(Translation).缩放(S ...

  7. QT 008 UI Add action 的方法

    1 Action 的简介和例子: ref:https://www.devbean.net/2012/08/qt-study-road-2-action/ Qt 使用QAction类作为动作.顾名思义, ...

  8. 手势识别器GestureDetector

    手势检测用于辅助检测用户的单击.滑动.长按.双击等行为.当然我们完全可以在View的onTouchEvent方法中实现所有的监听,但有的时候用手势识别器更方便,比如说:监听用户双击行为. 它的使用步骤 ...

  9. iOS疯狂讲解之手势识别器

    #import "RootViewController.h"@interface RootViewController ()@end@implementation RootView ...

最新文章

  1. 缩进对于python程序至关重要吗_缩进对于Python程序至关重要。
  2. quality center 支持的平台
  3. php百度人脸识别做登陆,php调用百度人脸识别接口查询数据库人脸信息实现验证登录功能...
  4. 实战:网店活动付邮试用全攻略
  5. 【Elasticsearch】es Timelion是Kibana中时间序列的可视化工具
  6. java.io.IOException: Server returned HTTP response code: 411 for URL
  7. 打造自己的Android源码学习环境之二:在虚拟机中安装Ubuntu(上)
  8. bode图处理----当相频特性曲线纵坐标超过正负180度
  9. 企业研发人员配备比例_中小IT企业项目团队人员配置管理
  10. Hash散列算法详细解析(五)
  11. 启动docker 报ERROR: failed to register layer: symlink
  12. Linux 运维一些知识点
  13. windows11 git 安装SSH密钥
  14. DataGrip离线安装数据库驱动
  15. Linux下开MC服务器
  16. 【Python数据分析学习笔记Day3】(三)数据分析工具pandas,数据清洗,聚类K-Means
  17. 手撕十大排序算法①——思路讲解
  18. Windows找不到文件‘xxxxx‘。请确定文件名称是否正确,再试一次。 win+r命令打不开xxxx
  19. matlab 图像痕迹识别,鉴别P图,人脸识别和数字图像取证方法
  20. 在宝塔面板中创建免费的ddns(这里用了dynu.com)

热门文章

  1. MySQL CONCAT函数:字符串拼接
  2. git如何查看和切换账号
  3. Linux使用awk命令获取某一行或某一列
  4. Android6.0之后的权限机制对App开发的影响
  5. php面试专题---MySQL常用SQL语句优化
  6. 敏捷宣言和背后的原则 (Agile Manifesto and the principles behind)
  7. 案例篇-HBase 实战之 MOB 使用指南
  8. 关于 stl的内存分配的深浅拷贝
  9. SQL Server不存在或拒绝访问故障的排除
  10. 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单等相关问题...