首页 技术 正文
技术 2022年11月15日
0 收藏 974 点赞 2,287 浏览 4610 个字

基本事件包括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(@”长按事件”);

}//长按处理

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848