iOS开发之UI手势
来源:互联网 发布:直播就是网络乞丐 编辑:程序博客网 时间:2024/06/06 10:00
基础手势
@property (nonatomic,strong)UIImageView *imageView;//该方法是一个懒加载,在需要的地方才调用,节省内存- (UIImageView *)imageView{//方法名要与属性相同 if (!_imageView) { _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2)]; _imageView.image = [UIImage imageNamed:@"1.jpg"]; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView];//此处相当于调用了懒加载方法 //开启交互事件的响应,默认为NO _imageView.userInteractionEnabled = YES; //添加手势对象 /** UITapGestureRecognizer 点按手势 参数1:响应事件拥有者,self表示当前视图控制器 参数2:响应事件方法,tapOneAction: */ UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneAction:)]; //表示识别事件的类型:几次点击触发,默认值是1 tapGesture.numberOfTapsRequired = 1; //几个手指点击时触发事件 tapGesture.numberOfTouchesRequired = 1; //把手势添加到视图里面,视图可以响应事件 [_imageView addGestureRecognizer:tapGesture]; //添加双击手势 UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwoAction:)]; tapGesture2.numberOfTapsRequired = 2;//点击两次才触发事件 tapGesture2.numberOfTouchesRequired = 1; [_imageView addGestureRecognizer:tapGesture2]; //当单击操作遇到双击操作的时候,使单击操作失败(tapGesture) [tapGesture requireGestureRecognizerToFail:tapGesture2]; }//单击事件响应方法,这里使图片放大,即改变imageView的frame- (void)tapOneAction:(UITapGestureRecognizer *)gesture{ NSLog(@"单击"); //获取手势监控的视图,在该imageView外面不能触发 UIImageView *imageView = (UIImageView *)gesture.view; //开始动画 [UIView beginAnimations:nil context:nil]; //设置动画时间 [UIView setAnimationDuration:2]; //放大图片 imageView.frame = self.view.bounds; }//双击事件响应方法,这里使图片缩小为原来的大小- (void)tapTwoAction:(UITapGestureRecognizer *)gesture{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2]; //设置_imageView为初始的frame _imageView.frame = CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2); //提交动画 [UIView commitAnimations]; }
高级手势
#import "ViewController.h"@interface ViewController ()<UIGestureRecognizerDelegate>//UIGestureRecognizerDelegate 同时进行旋转、缩放的协议@property (nonatomic,strong)UIImageView *imageView;//定义一个缩放手势手势,用来对视图进行放大缩小//捏合手势@property (nonatomic,strong)UIPinchGestureRecognizer *pinGesture;//旋转手势@property (nonatomic,strong)UIRotationGestureRecognizer *rotationGesture;@end@implementation ViewController//懒加载- (UIImageView *)imageView { if (!_imageView) { _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2)]; _imageView.image = [UIImage imageNamed:@"2.jpg"]; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //开启交互 _imageView.userInteractionEnabled = YES; //创建一个捏合手势 _pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGesture:)]; [_imageView addGestureRecognizer:_pinGesture]; //创建一个旋转手势 _rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; [_imageView addGestureRecognizer:_rotationGesture]; //设置两种手势的代理为self _pinGesture.delegate = self; _rotationGesture.delegate = self;}//实现协议的方法//是否可以同时响应两个手势- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {//Simultaneously 同时的 //如果返回YES,可以同时响应 return YES;}- (void)pinGesture:(UIPinchGestureRecognizer *)gesture { //获取监控图像视图 UIImageView *imgView = (UIImageView *)gesture.view; //对图像视图对象进行矩阵计算并且赋值 //transform:表示图像学中的变换矩阵 //CGAffineTransformScale:通过缩放产生一个新的矩阵(在原来的基础上) //p1:原来的矩阵 //p2:x方向缩放的比例 //p3:y方向缩放的比例 imgView.transform = CGAffineTransformScale(imgView.transform, gesture.scale, gesture.scale); //返回的是新的缩放之后变化矩阵的比例 gesture.scale = 1;}- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture { //获取监控图像视图 UIImageView *imgView = (UIImageView *)rotationGesture.view; imgView.transform = CGAffineTransformRotate(imgView.transform, rotationGesture.rotation); //对角度进行清零 rotationGesture.rotation = 0;}
扩展手势
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>@property (nonatomic,strong)UIImageView *imageView;@end@implementation ViewController- (UIImageView *)imageView { if (!_imageView) { _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, self.view.bounds.size.width-40, self.view.bounds.size.height/2)]; _imageView.image = [UIImage imageNamed:@"2.jpg"]; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; _imageView.userInteractionEnabled = YES; //创建一个平移手势 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; //添加手势 [_imageView addGestureRecognizer:panGesture]; //创建一个轻扫手势 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionUp; [_imageView addGestureRecognizer:swipeGesture]; //创建一个长按手势 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)]; //设置长按手势的时间,默认0.5秒为长按手势 longGesture.minimumPressDuration = 1.0; [_imageView addGestureRecognizer:longGesture];}//平移手势//只要手指在屏幕坐标上发生变化的时候就会调用该方法- (void)panAction:(UIPanGestureRecognizer *)panGesture {// NSLog(@"平移手势");// //获得移动的坐标,相对于视图的坐标系 CGPoint point = [panGesture translationInView:self.view];//translation 平移 _imageView.center = CGPointMake(_imageView.center.x + point.x, _imageView.center.y + point.y); //在指定视图的坐标系中设置平移值 [panGesture setTranslation:CGPointZero inView:self.view];// NSLog(@"point.x = %f,point.y = %f",point.x,point.y);// //获取平移时的相对速度(每秒在屏幕上滑动移动像素的值) CGPoint pv = [panGesture velocityInView:self.view];// NSLog(@"pv.x = %.2f,pv.y = %.2f",pv.x,pv.y);}//轻扫手势- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture { NSLog(@"向上");}//长按手势- (void)longAction:(UILongPressGestureRecognizer *)longGesture { if (longGesture.state == UIGestureRecognizerStateBegan) { NSLog(@"开始状态"); UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"保存到相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"保存到相册"); UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); }]; [alertC addAction:action1]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"打开相册"); /** sourceType: UIImagePickerControllerSourceTypePhotoLibrary, 图片列表 UIImagePickerControllerSourceTypeCamera, 摄像头 UIImagePickerControllerSourceTypeSavedPhotosAlbum 相册 */ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; //资源类型 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //选择图片后是否可以被编辑 picker.allowsEditing = YES; //推送picker控制器 [self presentViewController:picker animated:YES completion:nil]; }]; [alertC addAction:action2]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"打开相机"); //要使用真机来测试 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIBarButtonSystemItemCamera; picker.delegate = self; picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; }]; [alertC addAction:action3]; UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; [alertC addAction:action4]; //推出alertC!! [self presentViewController:alertC animated:YES completion:^{ }]; } else if (longGesture.state == UIGestureRecognizerStateEnded){ NSLog(@"结束状态"); } NSLog(@"长按");}//保存图片方法- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (!error) { NSLog(@"图片保存成功"); } else { NSLog(@"图片保存失败"); }}#pragma mark --UIImagePickerControllerDelegate--- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) { NSLog(@"%@",info); //照片的原始数据 _imageView.image = info[UIImagePickerControllerOriginalImage]; //退出picker!!! } else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { _imageView.image = info[UIImagePickerControllerOriginalImage]; } [picker dismissViewControllerAnimated:YES completion:nil];}//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {// // NSLog(@"取消");//}@end
0 0
- iOS开发之UI手势
- iOS开发 UI 触摸手势
- IOS开发之手势
- iOS开发之 手势
- iOS 开发之手势
- IOS开发之手势(UIGestureRecognizer)
- IOS开发之手势UIGestureRecognizer
- iOS开发之手势(UIGestureRecognizer)
- iOS开发之手势识别
- IOS开发之手势UIGestureRecognizer
- iOS开发之手势篇
- iOS开发之手势识别
- iOS开发之手势识别
- iOS开发之手势识别
- iOS开发之自定义手势
- iOS开发之手势识别
- iOS开发之手势识别
- iOS开发之手势识别
- Zookeeper Api(java)入门与应用(转)
- esclipse 添加xsd 校验
- mysql--用户管理
- 如何用git命令行上传本地代码到github
- Sublime text 3 中文文件名显示方框怎么解决
- iOS开发之UI手势
- Openlayers3加载DXF文件作为底图实现
- 素数
- 分分钟看懂 Maven配置、创建导入项目及Maven基础
- 从demo开始学dubbo
- svn服务端安装
- hadoop运行的幕后角色
- 删除某个用户时报ORA-01940
- PHP 实现页面抓取