添加手势

#import "ViewController.h"

@interface ViewController ()

{

    UITextField *tf;

    UIImageView *myview;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    self.view.backgroundColor=[UIColor cyanColor];

    //添加图片,myimage设成全局变量

    UIImage *image=[UIImage imageNamed:@"liu.jpg"];

    myview=[[UIImageView alloc]initWithImage:image];

    myview.frame=self.view.frame;

    myview.contentMode=UIViewContentModeScaleAspectFit;//图片的填充方式

    [self.view addSubview:myview];

    

    //添加点记的手势

    UITapGestureRecognizer *tagGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close:)];

    //添加一个捏合的手势

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

    [self.view addGestureRecognizer:pinchGR];

    //一个旋转的手势

    UIRotationGestureRecognizer *Rgr=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

    [self.view addGestureRecognizer:Rgr];

    //一个拖动的手势

    UIPanGestureRecognizer *Pgr=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pantion:)];

    //默认的静态图片,不支持拖动

    myview.userInteractionEnabled=YES;//加上即可支持

    [myview addGestureRecognizer:Pgr];

    

    

    tagGR.numberOfTapsRequired=1;//属性:一个手指头,键盘消失。属性:点击两次键盘消失

    [self.view addGestureRecognizer:tagGR];

    

    tf=[[UITextField alloc]initWithFrame:CGRectMake(90, 50, 100, 100)];

    tf.backgroundColor=[UIColor yellowColor];

    

    [tf.layer setMasksToBounds:YES];

    [tf.layer setCornerRadius:50.0];

    [tf.layer setBorderWidth:1.0];

    tf.placeholder=@"输入";

    [self.view addSubview:tf];

    //清扫的手势

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

    swip.direction=UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;

    [myview addGestureRecognizer:swip];

    //常按手势

    UILongPressGestureRecognizer *lon=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longg:)];

    [myview addGestureRecognizer:lon];

    tf.delegate=self;//收键盘

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [tf resignFirstResponder];//收键盘 

    return YES;

}

-(void)longg:(UILongPressGestureRecognizer *)l

{

    NSLog(@"nihao");//长按

}

-(void)swip:(UISwipeGestureRecognizer *)swipGR

{//清扫的手势,按住,往右扫一下,像翻页一样

    //UISwipeGestureRecognizer

}

-(void)pantion:(UIPanGestureRecognizer *)pan

{

    CGPoint dpoint=[pan translationInView:myview];

    

    myview.transform=CGAffineTransformTranslate(myview.transform, dpoint.x, dpoint.y);//拖动手势

    [pan setTranslation:CGPointZero inView:myview];//使拖动不用那么灵敏

}

-(void)rotation:(UIRotationGestureRecognizer *)rgr

{

    myview.transform=CGAffineTransformRotate(myview.transform,rgr.rotation);

    rgr.rotation=0;//旋转在每次应该在上次的基础上,旋转手势

}

-(void)pinchGR:(UIPinchGestureRecognizer *)pinchGRR

{

    myview.transform=CGAffineTransformScale(myview.transform,pinchGRR.scale, pinchGRR.scale);//点击手势

    pinchGRR.scale=1;//加一的原因是:

//    NSLog(@"%f",pinchGRR.scale);//比例

    NSLog(@"%@",NSStringFromCGRect(myview.bounds));//不变。frame变了

}

-(void)close:(UITapGestureRecognizer *)tapGR

{

    CGPoint location=[tapGR locationInView:self.view];

    NSLog(@"你点击的位置:%@",NSStringFromCGPoint(location));

    [tf resignFirstResponder];

}

//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//    [tf resignFirstResponder];

//}//点空白处手键盘

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/linximu/p/4409115.html