iOS UI05_手势识别器
2015-08-05 08:41
411 查看
手势:点击,长按,旋转,捏合,拖拽,清扫,清扫的方向
// // MainViewController.m // UI05_手势识别器 // // Created by dllo on 15/8/4. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import "MainViewController.h" @interface MainViewController () @property(nonatomic,retain)UIImageView *imageView; @property(nonatomic,retain)UIAlertView *alertView; @end @implementation MainViewController -(void)dealloc { [_imageView release]; [_alertView release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //UIImageView UIImage *image=[UIImage imageNamed:@"footRight_03.jpg.jpg"]; self.imageView=[[UIImageView alloc] initWithImage:image]; self.imageView.frame=CGRectMake(50, 100, 300, 400); [self.view addSubview:self.imageView]; [_imageView release]; //把图片的用户交互打开,默认是关闭的,此外还有一个控件是label self.imageView.userInteractionEnabled=YES; //手势的使用 //1.点击 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]; //设置点击几次才会触发方法 tap.numberOfTapsRequired=2; //设置几根手指进行点击 tap.numberOfTouchesRequired=2; //将手势添加到对应的图片上 [self.imageView addGestureRecognizer:tap]; [tap release]; //2.长按 UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; //设置长按触发的最短时间 longPress.minimumPressDuration =2; //用户手指在长按过程中允许移动的距离 longPress.allowableMovement=200; //把手势添加到图片上 [self.imageView addGestureRecognizer:longPress]; [longPress release]; //3.旋转 //创建一个旋转的手势 UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; //把手势放到对应的图片上 [self.imageView addGestureRecognizer:rotation]; //释放 [rotation release]; //4.捏合 //创建 UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pincheAction:)]; //添加到图片上 [self.imageView addGestureRecognizer:pinch]; //释放 [pinch release]; //5.拖拽 UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [self.imageView addGestureRecognizer:pan]; [pan release]; //6.清扫 UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; [self.imageView addGestureRecognizer:swipe]; [swipe release]; //清扫的方向 swipe.direction=UISwipeGestureRecognizerDirectionUp; //屏幕边界手势 iOS7.0之后出现的手势 // UIScreenEdgePanGestureRecognizer } #pragma mark 点击方法 -(void)tapAction:(UITapGestureRecognizer *)tap { NSLog(@"djkdajdo"); self.imageView.image=[UIImage imageNamed:@"angry_05.jpg"]; } #pragma mark 长按时响应方法 -(void)longPressAction:(UILongPressGestureRecognizer *)longPress { //长按的状态 // longPress.state //长按之后弹出一个UIAlerView if (!self.alertView) { self.alertView=[[UIAlertView alloc] initWithTitle:@"404" message:@"报错" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil]; [self.alertView show]; [_alertView release]; } } #pragma mark 通过图片的旋转手势,让图片发生旋转 -(void)rotationAction:(UIRotationGestureRecognizer *)rotarion { //可以通过手势获取手势添加的视图是哪一个 UIImageView *imageView=(UIImageView *)rotarion.view; //进行旋转的操作 //通过视图的transform属性,让视图进行旋转 imageView.transform=CGAffineTransformRotate(imageView.transform,rotarion.rotation); rotarion.rotation=0; } #pragma mark 通过捏合手势,缩放图片 -(void)pincheAction:(UIPinchGestureRecognizer *)pinche { //通过手势找视图 UIImageView *imageView=(UIImageView *)pinche.view; //通过transform改变图片的尺寸 imageView.transform=CGAffineTransformScale(imageView.transform, pinche.scale, pinche.scale); pinche.scale=1; } #pragma mark 通过拖拽手势,让试图随着手势移动而移动 -(void)panAction:(UIPanGestureRecognizer *)pan { UIImageView *imageView=(UIImageView *)pan.view; //通过手势获得经过的点 CGPoint p=[pan translationInView:imageView]; //设置移动的位置 imageView.transform=CGAffineTransformTranslate(imageView.transform, p.x, p.y); //为了防止手势在操作的时候试图消失 [pan setTranslation:CGPointZero inView:imageView]; } #pragma mark 清扫的对应方法 -(void)swipeAction:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionUp ) { NSLog(@"上"); } } - (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
相关文章推荐
- 小案例:物体追随鼠标轨迹移动,关于Queue的运用
- 静态UITabelView
- UI04_UISwitch
- UI之窗口与视图
- 我的第一个UI程序
- UIKit 手势识别 UIGestureRecognizer
- UIKit 视图 UIView
- HDU 4343 Interval query(倍增思想+贪心)
- Installing Ubuntu on a Pre-Installed Windows 8 (64-bit) System (UEFI Supported)
- Windows和Ubuntu双系统,修复UEFI引导的两种办法
- Leetcode #95 Unique Binary Search Trees II
- Leetcode # 96 Unique Binary Search Trees
- 无法启动soapUI 提示:the jvm could not be started. the main method may have thrown an exception
- CodeForces 91B-K - Queue 二分
- [IOS]今天开始学UI---UIAlertController
- hdu4918 Query on the subtree
- UE4信息交互-蓝图类之间的信息交互
- 循环结构中break、continue、return和exit的区别
- [IOS]今天开始学UI---UIActivityIndicatorView
- PostgreSQL: Query for location of global tablespace?