您的位置:首页 > 其它

TargetAction&&Recognizer

2015-12-11 22:39 260 查看
//

//  RootViewController.m

 

#import "RootViewController.h"

#import "MyView.h"

 

@interfaceRootViewController ()

 

@property (nonatomic,retain)MyView *v;

 

@end

 

@implementationRootViewController

 

 

- (void)dealloc{

   [_v release];

   [superdealloc];

 

}

- (void)viewDidLoad {

   [superviewDidLoad];

 

//   MyView *view1 = [[MyViewalloc]initWithFrame:CGRectMake(100, 100, 100,100)];

//   view1.backgroundColor = [UIColorredColor];

//   view1.tag = 100;

//   [self.view addSubview:view1];

//   // 设置目标对象

//   view1.target = self;

//   // 设置执行的动作

//   view1.action = @selector(changeColor);

//   

//   

//   MyView *view2 = [[MyViewalloc]initWithFrame:CGRectMake(220, 229, 50,50)];

//   view2.backgroundColor = [UIColorgrayColor];

//   self.v = view2;

//   view2.target = self;

//   view2.action = @selector(changeLocation);

//   [self.view addSubview:view2];

//   

//   

//   // target,action 设计模式可以达到解耦的目的

//   [view1 release];

//   [view2 release];

//   

 

    // 创建图片对象

UIImage *image =[UIImageimageNamed:@"1.jpg"];

     // 重沙盒中获取一张图片

//   UIImage *image1 =[UIImageimageWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"drink_0.jpg"ofType:nil ] ];

//   NSLog(@"== %@",image1);

 

   // 产生一个图片视图

   // 视图大小根据图片大小而定

 

//   UIImageView *imageV = [[UIImageViewalloc]initWithImage:image];

//   

//   // 给图片视图加背景颜色,加不上去

//   imageV.backgroundColor = [UIColorredColor];

//   // 将视图添加到图片视图上

//   [self.viewaddSubview:imageV];

 

    // 产生一个图片视图对象

   // 图片视图的大小由我们自己来设定

UIImageView *imageV =[[UIImageViewalloc]initWithFrame:CGRectMake(0, 100, 414, 500)];

   // 设置展示图片

imageV.image = image;

   // 设置图片视图的展示模式,不同的值,图片的大小会不一样

imageV.contentMode =UIViewContentModeRedraw;

imageV.tag = 100;

   // 将图片视图添加到self.view上

   [self.viewaddSubview:imageV];

 

   // 打开用户交互

imageV.userInteractionEnabled = YES;

 

    //手势识别总共是七种

   //UIGestureRecognizer是一个抽象类,抽象类不直接使用,一般使用它的子类

 

   // 1、轻拍手势

UITapGestureRecognizer *tap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];

   // 设置点击次数

tap.numberOfTapsRequired = 2;

 

    // 设置点击需要的手指数量

tap.numberOfTouchesRequired = 2;

 

   // 1、给视图添加轻拍手势

   [imageVaddGestureRecognizer:tap];

 

   // 2、长按手势

UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longP)];

   [imageVaddGestureRecognizer:longPress];

 

   // 3、平移手势

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

   [imageVaddGestureRecognizer:pan];

 

    // 4、轻扫

UISwipeGestureRecognizer *swip =[[UISwipeGestureRecognizeralloc]initWithTarget:self action:@selector(swip:)];

   [imageVaddGestureRecognizer:swip];

   // 设置轻扫方向

swip.direction =UISwipeGestureRecognizerDirectionUp;

 

   // 5、捏合手势

UIPinchGestureRecognizer *pin =[[UIPinchGestureRecognizeralloc]initWithTarget:self action:@selector(pinch:)];

   //添加捏合手势

   [imageVaddGestureRecognizer:pin];

 

   // 6、旋转手势

UIRotationGestureRecognizer *rotate =[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotate:)];

   [imageVaddGestureRecognizer:rotate];

 

     // 7、屏幕边缘轻扫手势

UIScreenEdgePanGestureRecognizer *edge =[[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(edges:)];

   // 设置屏幕哪边触发

edge.edges = UIRectEdgeLeft;

   [imageVaddGestureRecognizer:edge];

 

   [tap release];

    [longPressrelease];

   [pan release];

   [swip release];

   [pin release];

   [rotate release];

   [edge release];

 

}

  //  1、轻拍手势

- (void)tap:(UITapGestureRecognizer *)taps{

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

imageV.image =[UIImageimageNamed:@"2.jpg"];

}

   // 2、长按手势

- (void)longP{

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

imageV.image =[UIImageimageNamed:@"3.jpg"];

}

   //  3、平移手势

- (void)pan:(UIPanGestureRecognizer *)pans{

    // 手势偏移量

CGPoint point = [panstranslationInView:self.view];

   //获取imagev

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

   //更改视图位置

imageV.center = CGPointMake(imageV.center.x+ point.x, imageV.center.y+ point.y);

   //清零偏移量,因为偏移量是相对于刚开始移动的偏移量,所以需要使用清零。

   [panssetTranslation:CGPointZeroinView:self.view];

}

 

   // 要注释掉平移手势才有用

   // 4、轻扫手势

- (void)swip:(UISwipeGestureRecognizer*)swipe{

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

imageV.image =[UIImageimageNamed:@"4.jpg"];

}

 

   // 5、捏合手势,按住aption(alt)键

- (void)pinch:(UIPinchGestureRecognizer*)pinch{

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

imageV.transform =CGAffineTransformScale(imageV.transform, pinch.scale, pinch.scale);

    // 若不设置这个语句,每次改变的大小是相对于初始的大小,所以要重新设置,让改变大小是相对于上一次的大小

pinch.scale = 1;

}

   // 6、旋转手势

- (void)rotate:(UIRotationGestureRecognizer*)rotate{

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

imageV.transform =CGAffineTransformRotate(imageV.transform, rotate.rotation);

   // 若不设置这个语句,每次旋转的角度是相对于初始的角度,所以要重新设置,让旋转的角度是相对于上一次的角度

rotate.rotation = 0;

}

 

   // 7、边缘轻扫手势,从左边边缘向右滑动改变图片

-(void)edges:(UIScreenEdgePanGestureRecognizer *)edge{

UIImageView *imageV = (UIImageView*)[self.view viewWithTag:100];

imageV.image =[UIImageimageNamed:@"5.jpg"];

}

 

- (void)changeColor{

MyView *view = (MyView *)[self.viewviewWithTag:100];

view.backgroundColor =[UIColorcolorWithRed:self.randomColorgreen:self.randomColorblue:self.randomColoralpha:self.randomColor];

}

 

 

- (void)changeLocation{

 

self.v.center = CGPointMake(arc4random() %(400 - 20 + 1) + 20, arc4random() % (400 - 20 + 1) + 20);

}

 

 

- (CGFloat)randomColor{

return arc4random() % 256 / 255.0;

}

 

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

 

@end

 

 

 

//

//  MyView.h

 

#import<UIKit/UIKit.h>

 

@interfaceMyView : UIView

 

  // 模拟UIButton的内部实现

@property (nonatomic,assign)id target;

@property (nonatomic,assign)SEL action;

 

@end

 

 

 

 

//

//  MyView.m

 

#import "MyView.h"

 

@implementationMyView

 

 

- (void)touchesBegan:(NSSet<UITouch*> *)touches withEvent:(UIEvent *)event{

      // 由sel的target 执行action方法。

   // 到此刻为止,要执行的方法,以及谁执行方法都是不定的。

   [self.targetperformSelector:self.actionwithObject:self];

 

}

 

@end

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: