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
原创粉丝点击