iOS开发基础 - UITouch的使用
来源:互联网 发布:vmware linux 安装 编辑:程序博客网 时间:2024/06/06 14:20
UITouch的使用#pragma mark --touch触摸事件- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{#pragma mark --UITouch对象属性// 因为单击时touches里面只有一个UITouch对象,所以可以用anyObject方法取里面存储的对象 UITouch *touch = [touches anyObject];// 记录点击时的事件,是一个时间戳 NSTimeInterval time = touch.timestamp; NSLog(@"%f",time);// 判断点击次数 NSInteger count = touch.tapCount; NSLog(@"%ld",(long)count);// 获取到我们点击的当前的view UIView *view00 = [touch view]; NSLog(@"%@",view00);// - (CGPoint)locationInView:(UIView *)view;// - (CGPoint)previousLocationInView:(UIView *)view;// 后面的参数说明坐标是相对于当前参数的坐标 CGPoint prePoint = [touch previousLocationInView:self.view]; NSLog(@"上一次点击时的坐标:%@",NSStringFromCGPoint(prePoint)); CGPoint currentPoint = [touch locationInView:self.view]; NSLog(@"当前点击的坐标:%@",NSStringFromCGPoint(currentPoint));}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"ssss");}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"33333");}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint prePoint = [touch previousLocationInView:self.view]; NSLog(@"上一次点击时的坐标:%@",NSStringFromCGPoint(prePoint)); CGPoint currentPoint = [touch locationInView:self.view]; NSLog(@"当前点击的坐标:%@",NSStringFromCGPoint(currentPoint)); CGPoint pointDelt = CGPointMake(currentPoint.x-prePoint.x, currentPoint.y-prePoint.y); CGPoint center = CGPointMake(_myView.center.x+pointDelt.x, _myView.center.y+pointDelt.y); [_myView setCenter:center];}UIGesture手势#import "ViewController.h"@interface ViewController ()<UIGestureRecognizerDelegate>@property (nonatomic,strong) UIImageView *imageV;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.imageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)]; self.imageV.image = [UIImage imageNamed:@"yellow"]; self.imageV.userInteractionEnabled = YES; [self.view addSubview:self.imageV]; //command + option// [self createTap];// [self createLongPress];// [self createPan]; [self createPinch]; [self createRotation];// [self createSwipe];}//手势 用来描述手指与屏幕具体的接触- (void)createTap{ //UIGestureRecognizer 这是一个手势类 是一个抽象基类 通常我们不会直接使用这个类 而是使用它的子类 //点击手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)]; //默认就是1 //点击几次 tap.numberOfTapsRequired = 1; //需要几个手指 tap.numberOfTouchesRequired = 1; //手势需要添加到某个视图上 [self.imageV addGestureRecognizer:tap];}- (void)tapImage:(UITapGestureRecognizer *)tap{ NSLog(@"点击了"); //当前点击的视图 UIImageView *imagV = (id)tap.view;}//长按手势- (void)createLongPress{ //长安手势 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressImage:)]; //最短长按时间 longPress.minimumPressDuration = 2.0; [self.imageV addGestureRecognizer:longPress];}- (void)longPressImage:(UILongPressGestureRecognizer *)press{ NSLog(@"长按");}//滑动手势- (void)createPan{ UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panImage)]; [self.imageV addGestureRecognizer:pan];}- (void)panImage{ NSLog(@"滑动");}//缩放- (void)createPinch{ UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchImage:)]; //设置代理 pinch.delegate = self; [self.imageV addGestureRecognizer:pinch];}- (void)pinchImage:(UIPinchGestureRecognizer *)pinch{ //缩放系数pinch.scale self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale, pinch.scale); //重置缩放系数 否则缩放系数会叠加 pinch.scale = 1.0;// CGAffineTransformMakeScale(<#CGFloat sx#>, <#CGFloat sy#>)}//旋转- (void)createRotation{ UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationImage:)]; rotation.delegate = self; [self.imageV addGestureRecognizer:rotation];}- (void)rotationImage:(UIRotationGestureRecognizer *)rotation{ //rotation.rotation旋转的角度 self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation); //重置旋转角度 否则会叠加 rotation.rotation = 0;}//轻扫- (void)createSwipe{ UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeImage:)]; //轻扫方向 swipe.direction = UISwipeGestureRecognizerDirectionRight; /* UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << */ [self.imageV addGestureRecognizer:swipe];}- (void)swipeImage:(UISwipeGestureRecognizer *)swipe{ self.imageV.center = CGPointMake(self.imageV.center.x + 10, self.imageV.center.y);}//是否允许添加多个手势- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ //如需添加多个手势 判断手势所在的view是不是一个view if (gestureRecognizer.view == otherGestureRecognizer.view) { return YES; } return NO;}@end
0 0
- iOS开发基础 - UITouch的使用
- iOS基础:UITouch
- IOS开发之UITouch
- IOS开发之UITouch
- iOS开发之UITouch
- 使用UITouch隐藏UITextField/UITextView输入键盘的方法-iOS开发
- IOS学习 UITouch基础学习
- IOS开发之触摸UITouch
- UITouch 的使用
- Swift UITouch 的使用
- IOS学习之——UItouch的相关使用
- iOS开发:UITouch控件与触摸事件
- UITouch使用
- ios UITouch 触摸事件的使用方法
- [ios开发基础之]KVO的使用
- IOS 基础开发之 UIScrollView的使用
- iOS开发基础之GTMBase64的使用
- iOS开发-基础:keychain的使用
- NFS ROOT 启动内核的配置(RedBoot)
- java
- GCD之线程小记
- OpenStack 部署分布式应用的一个坑
- QMediaPlayer视频播放器
- iOS开发基础 - UITouch的使用
- iOS开发~CocoaPods使用详细说明
- document.getElementByTagName的用法
- 内点法
- iOS中url的特殊字符转换
- java关于获取系统时间及比较时间大小
- Lucene Suggest的使用
- iOS 观察者模式
- C++之:虚函数表