事件手势,手势

本文介绍了iOS开发中处理触摸事件的方法及多种手势识别器的应用,包括轻拍、长按、捏合、拖拽和平移等,通过实例展示了如何响应手势进行图片视图的变化。

1.开始触摸事件:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"开始触摸%@", event);

}


2.正在触摸事件:

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"正在触摸%@", event);

}


3.取消触摸事件:

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 

    NSLog(@"取消触摸%@", event);

}


4.摇一摇开始事件:

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {    

    NSLog(@"摇一摇开始");

}


5.摇一摇结束事件:

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"摇一摇结束"); }


    二、手势

为了验证手势的作用,设置一张图片,在图片上做各种手势,看看图片的效果;

1.创建一个图片视图。并添加到根视图上。

UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

    imageview.center = self.view.center;

    imageview.image = [UIImage imageNamed:@"1.jpg"];

    imageview.userInteractionEnabled = YES;

    imageview.contentMode = UIViewContentModeScaleAspectFit;

    [self.view addSubview:imageview];

    

2.点击、轻拍(tap)

//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

////    执行方法需要点击(轻拍)的次数

//    tap.numberOfTapsRequired = 2;

////    需要几个手指点击(轻拍)

//    tap.numberOfTouchesRequired = 1;

////    将手势赋给视图

//    [imageview addGestureRecognizer:tap];


#pragma mark - 轻拍事件

- (void)tapAction:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍、点击手势");

//    NSLog(@"%@", tap.view);

    UIImageView *imageView = (UIImageView *)tap.view;

    [imageView setImage:[UIImage imageNamed:@"2.jpg"]];

}


3.长按(longPress)

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

////    判定为长按手势需要的时间 默认为0.5秒

//    longPress.minimumPressDuration = 2;

////  在判定过程中,允许用户手指移动的距离

//    longPress.allowableMovement = 200; 

//    [imageview addGestureRecognizer:longPress];


#pragma mark - 长按事件

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

    UIImageView *imageView = (UIImageView *)longPress.view;

    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];

}


4.捏合(pinch)

UIPinchGestureRecognizer *pinCh = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; 

    [imageview addGestureRecognizer:pinCh];


#pragma mark - 捏合事件

- (void)pinchAction:(UIPinchGestureRecognizer * )pinch {

    UIImageView *imageView = (UIImageView *)pinch.view;

    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1;

}


  5.拖拽/平移 (pan)

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panACtion:)];  

    [imageview addGestureRecognizer:pan];


#pragma mark - 拖拽/平移事件

- (void)panACtion:(UIPanGestureRecognizer *)pan {

    UIImageView *imageView = (UIImageView *)pan.view;

    CGPoint p = [pan translationInView:imageView];

//    NSLog(@"%f%f", p.x, p.y);

    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);

    [pan setTranslation:CGPointZero inView:imageView];

}


 6.旋转 (rotation)

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationACtion:)];

    [imageview addGestureRecognizer: rotation];


#pragma mark - 旋转事件

- (void)rotationACtion:(UIRotationGestureRecognizer *)rotation {

 UIImageView *imageView = (UIImageView *)rotation.view;

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);

    rotation.rotation = 0;

}


7.轻扫手势(swipe)

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeACtion:)];

    [imageview addGestureRecognizer:swipe];


#pragma mark - 轻扫手势事件

- (void)swipeACtion:(UISwipeGestureRecognizer *)swipe { 

    UIImageView *imageView = (UIImageView *)swipe.view;

}


 8.屏幕边缘轻扫识别器(screenEdgePan)

    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanACtion:)];


#pragma mark - 屏幕边缘轻扫手势

- (void)screenEdgePanACtion:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {

    UIImageView *imageView = (UIImageView *)screenEdgePan.view;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值