ios手势操作,四个基本事件与六个常用事件

基本事件包括begin,canceled,move,ended四项,如果对象的hidden属性为yes,则无效果,hidden属性必须为no;才能使用:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//触碰开始

//    NSLog(@"%ld",[touches count]);

if ([[event allTouches]count]==2) {

NSArray * one =[[event allTouches]allObjects];

_tiLabel.hidden=NO;

_yLabel.hidden=NO;

_tiLabel.center=[[one objectAtIndex:0] locationInView:self.view];

_yLabel.center=[[one objectAtIndex:1] locationInView:self.view];

}

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{//触控发生意外终止是

_tiLabel.hidden=YES;_yLabel.hidden=YES;

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{//触控结束时

_tiLabel.hidden=YES;_yLabel.hidden=YES;

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//触控移动时

{

//    NSLog(@"%ld",[[event allTouches]count]);

if ([[event allTouches]count]==2) {

NSArray * one =[[event allTouches] allObjects];

_tiLabel.hidden=NO;

_yLabel.hidden=NO;

_tiLabel.center=[[one objectAtIndex:0] locationInView:self.view];

_yLabel.center=[[one objectAtIndex:1] locationInView:self.view];

}

}

下面是六大常用事件,包括:点击,拖动,捏合,旋转,长按以及轻扫

  点击事件:顾名思义(UITapGestureRecognizer)

  拖动事件:拖动view内的对象(UIPanGestureRecognizer)

  捏合事件:主要用于操作对象的方法以及缩小(UIPinchGestureRecognizer)

  旋转事件:主要用于控制对象的旋转角度(UIRotationGestureRecognizer)

  长按事件:顾名思义(UILongPressGestureRecognizer)

  清扫事件:主要add在view内,轻扫又可以按照属性分为上下左右四向的清扫(UISwipeGestureRecognizer)

想对某个对象添加六大事件,对象的userinteractionEnable属性必须为yes;否则六大事件会无响应:

- (void)viewDidLoad {

[super viewDidLoad];

UIImage * pic =[UIImage imageNamed:@"rmb.jpg"];

UIImageView * imgView = [[UIImageView alloc]initWithImage:pic];

imgView . backgroundColor = [UIColor blackColor];

imgView.frame =CGRectMake(0, 0, 300, 200);

imgView . userInteractionEnabled=YES;/**********该项设置必须为yes************/

[self.view addSubview:imgView];//imageview对象的代码创建

/*------------------------下面是事件对象的创建以及add操作---------------------*/

//点击

UITapGestureRecognizer * tapGestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

tapGestureRecognizer.numberOfTapsRequired=1;

tapGestureRecognizer.numberOfTouchesRequired=1;

  //设置点击事件的单击次数以及手指个数

[imgView addGestureRecognizer:tapGestureRecognizer];

//拖动

UIPanGestureRecognizer * panGestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

[imgView addGestureRecognizer:panGestureRecognizer];

//旋转

UIRotationGestureRecognizer * rotationGestureRecognizer =  [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotation:)];

[imgView addGestureRecognizer:rotationGestureRecognizer];

//捏合

UIPinchGestureRecognizer * pinchGestureRecognizer= [[UIPinchGestureRecognizer alloc]initWithTarget:self  action:@selector(handlePinch:)];

[imgView addGestureRecognizer:pinchGestureRecognizer];

//长按

UILongPressGestureRecognizer * longGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLong:)];

[imgView addGestureRecognizer:longGestureRecognizer];

//清扫

UISwipeGestureRecognizer * leftGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

leftGestureRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;//设置清扫的方向

UISwipeGestureRecognizer * rightGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

rightGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;//设置清扫的方向

[self.view addGestureRecognizer:leftGestureRecognizer];

[self.view addGestureRecognizer:rightGestureRecognizer];

}

-(void)handleTap:(UITapGestureRecognizer * )recognizer {

//     NSLog(@"向左清扫");

[[[UIAlertView alloc]initWithTitle:@"提示" message:@"点击事件发生" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok",nil] show];

}//点击处理

-(void)handlePan:(UIPanGestureRecognizer *) recognizer{

UIImageView * current=(UIImageView *)recognizer.view;//获取imageview对象

CGPoint translaation=[recognizer translationInView:recognizer.view];//获得移动的坐标

current.center=CGPointMake(current.center.x+translaation.x, current.center.y+translaation.y);

//使用原先坐标加上移动后的坐标,赋值给imageview对象

[recognizer setTranslation:CGPointZero inView:self.view];

//清零,防止再次移动

}//拖动处理

-(void)handleRotation:(UIRotationGestureRecognizer *) recognizer{

recognizer . view . transform=CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);

}//旋转处理

-(void) handlePinch:(UIPinchGestureRecognizer *) recognizer{

recognizer . view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);

recognizer.scale=1;

}//捏合处理

-(void) handleSwipe:(UISwipeGestureRecognizer *) recognizer{

if (recognizer.direction==UISwipeGestureRecognizerDirectionRight) {

NSLog(@"向右清扫");

}else if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft){

NSLog(@"向左清扫");

}

}//清扫处理

-(void) handleLong:(UILongPressGestureRecognizer *) recognizer{

NSLog(@"长按事件");

}//长按处理

时间: 2024-07-30 13:01:08

ios手势操作,四个基本事件与六个常用事件的相关文章

iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UITapGestureRecognizer(点按) UILo

iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义

1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UITapGestureRecognizer(点按) UILo

ios的手势操作之UIGestureRecognizer

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

【转】 ios的手势操作之UIGestureRecognizer浅析

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

iOS开发——仿Clear纯手势操作的UITableView

前言 在Clear应用中,用户无需任何按钮,纯靠不同的手势就可以完成对ToDoItem的删除.完成.添加.移动.具体来说,功能上有左划删除,右划完成,点击编辑,下拉添加.捏合添加.长按移动.这里将这些功能实现并记录. 左划删除与右划完成 所谓的左右滑动,就是自定义一个cell然后在上面添加滑动手势.在处理方法中计算偏移量,如果滑动距离超过cell宽度一半,就删除它,或者是为文本添加删除线等来完成它:如果没有超过一半,那么就用动画把cell归位. 效果图如下: 关键代码如下: - (void)ha

iOS-画板程序(手势操作无)

// // HMPaintView.h // 画板 // // Created by YaguangZhu on 15/9/10. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <UIKit/UIKit.h> @interface HMPaintView : UIView @property(nonatomic,assign)CGFloat width; @property(nonatomic,strong

iOS 手势及触摸

转自:http://justsee.iteye.com/blog/1885538 一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResponder的子类, 响应者链是一个由不同对象组成的层次结构,其中的每个对象将依次获得响应事件消息的机会.当发生事件时,事件首先被发送给第一响应者,第一响应者往往是事 件发生的视图,也就是用户触摸屏幕的地方.事件将沿着响

IOS 手势事件的冲突

关于手操作需要强调几点: UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionEnabled=YES(在iOS中UILabel.UIImageView的userInteractionEnabled默认都是NO,UIButton.UITextField.UIScrollView.UITableView等默认都是YES). 轻扫手势虽然是连续手势但是它的操作事件只会在识别结束时调用

FingerGestures研究院之初探Unity手势操作

最近研究了一下Unity中的一个手势操作的插件FingerGestures.它能很方便监听到Unity中的各种手势事件:上下左右四方向的滑动事件.按下事件.抬起事件.移动事件.连击事件.长按事件等等.它同时支持触摸屏操作与鼠标操作,总起来说使用起来还是比较方便的,今天写下教程记录这个插件的详细使用步骤.首先下载这个插件,大家可以在圣典上找这个插件的下载地址,当然也可以在本文最后下载该插件.  我看了一下这个插件底层的实现步骤,他是通过C#代理的形式来实现手势操作的.如下图红圈内所示,这五个重要的