UI:target-action设计模式、手势识别器

⼀、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);
    //将角度置换为0
    rotate.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);
    //将角度置换为0
    rotate.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { namespace 'com.example.qrtopicture' compileSdk 33 defaultConfig { applicationId "com.example.qrtopicture" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.3.2' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'com.google.zxing:core:3.4.1' implementation 'com.google.zxing:android-core:3.3.0' implementation 'com.google.zxing:android-integration:3.3.0' implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.5.1' implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' }帮我看看
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值