手势

来源:互联网 发布:mysql.sock在哪 编辑:程序博客网 时间:2024/03/29 19:15
#import "AZRootViewController.h"@interface AZRootViewController ()@property(nonatomic,strong)UIImageView *imageview;@end@implementation AZRootViewController//懒加载-(UIImageView *)imageview{    if (!_imageview)    {        _imageview=[[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 200, 200)];        [self.view addSubview:_imageview];    }    return _imageview;}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    [self.imageview setImage:[UIImage imageNamed:@"0.JPG"]];                /***************************/    /*  UITapGestureRecognizer */    /*  ===================    */    /*          手势            */    /*  ===================    */    /***************************/            //开启用户交互    [self.imageview setUserInteractionEnabled:YES];        #if 0    //创建轻触手势    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];        //设置手指点击次数    [tap setNumberOfTapsRequired:1];        //设置点击屏幕的次数    [tap setNumberOfTouchesRequired:1];#endif    #if 0    //长按手势    UILongPressGestureRecognizer *tap=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];        /*        每种手势都有tap.view这个属性,表示手势上得视图,在这里就表示imageview。     */    #endif    #if  0    //移动手势    UIPanGestureRecognizer *tap=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];#endif        #if 0    //捏合手势    UIPinchGestureRecognizer *tap=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];#endif    #if 0    //旋转手势    UIRotationGestureRecognizer *tap=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaTap:)];    #endif        //滑动手势    UISwipeGestureRecognizer *tap=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(switTap:)];        //设置滑动方向    tap.direction=UISwipeGestureRecognizerDirectionLeft;    //默认只向右移有效        /*        如果要实现向左和向右都能滑动,那么可以添加两个滑动手势到同一个imageview上     */        //滑动手势    UISwipeGestureRecognizer *tap1=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(switTap:)];        //设置滑动方向    tap1.direction=UISwipeGestureRecognizerDirectionRight;     [self.imageview addGestureRecognizer:tap1];                //将手势添加到imageView上    [self.imageview addGestureRecognizer:tap];                }-(void)switTap:(UISwipeGestureRecognizer *)sender{    if (sender.direction==UISwipeGestureRecognizerDirectionRight)    {        NSLog(@"向右滑动");    }    if (sender.direction==UISwipeGestureRecognizerDirectionLeft)    {        NSLog(@"向左滑动");    }   }-(void)rotaTap:(UIRotationGestureRecognizer *)sender{    //sender.rotation  旋转弧度            self.imageview.transform=CGAffineTransformRotate(self.imageview.transform, sender.rotation);    //注意:每次都有让弧度为0    [sender setRotation:0];    }-(void)pinch:(UIPinchGestureRecognizer*)sender{    //sender.scale 缩放比例            self.imageview.bounds = CGRectMake(0, 0, self.imageview.bounds.size.width*sender.scale,self.imageview.bounds.size.height*sender.scale);        //注意:      //每次比例都按 1 开始计算      [sender setScale:1];    }-(void)pan:(UIPanGestureRecognizer *)sender{    //偏移量    CGPoint point=[sender translationInView:self.view];    self.imageview.center=CGPointMake(self.imageview.center.x+point.x, self.imageview.center.y+point.y);        //注意:      //清空偏移量      [sender setTranslation:CGPointZero inView:self.view];}-(void)tap:(id)sender{    self.imageview.transform=CGAffineTransformRotate(self.imageview.transform, 180);}-(void)longTap:(UILongPressGestureRecognizer *)sender{    //self.imageview.transform=CGAffineTransformRotate(self.imageview.transform, -180);    if (sender.state==UIGestureRecognizerStateBegan)    {        //长按开始    }    if (sender.state==UIGestureRecognizerStateChanged)    {            }    if (sender.state==UIGestureRecognizerStateEnded)    {        //长按结束    }}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

0 0