IOS开发 UIGesture扩展手势

本节学习内容

1.UIGesture扩展手势类型

2.UIGesture扩展手势属性

3.UIGesture扩展手势用法

UIGesture扩展手势

【UIPan手势】

平移手势:可以用手指在屏幕上移动的手势

【UISwipe手势】

滑动手势:左滑,右滑,上滑,下滑

【UILongPress手势】

长按手势:长时间按住一个试图响应事件

【重点属性函数】

minimumPressDuration:长按时间长充

direction:滑动手势方向

UIGestureRecognizerStateBegan:长按时间状态

translationInView:获取平移手势位置

velocityInView:获取平移手势的速度


【ViewController.m】

UIImageView* iView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"17_2.jpg"]];

iView.frame=CGRectMake(50,50,200,300);

iView.userInteractionEnabled=YES;

//创建一个平易手势,参数1:事件函数处理对象,参数2:事件函数

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

//将手势添加到图像视图中

[iView addGestureRecognizer:pan];

//将移动事件手势从图像中取消

[iView removeGestureRecognizer:pan];

[self.view addSubview:iView];

//移动事件函数,只要手指坐标在屏幕上发生变化时,函数就被调用

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

//获取移动的从标,现对于视图的坐标系统,参数:相对的视图对象

CGPoint pt=[pan translationInView:self.view];

//打印移动坐标位置

//NSLog(@“pt.x=%f,pt.y=%f”,pt.x,pt.y);

//获取移动时的相对速度

CGPoint pv=[pan velocityInView:self.view];

NSLog(@“pv.x=%.2f,pv.y=%.2f”,pt.x,pt.y);

//创建一个滑动手势

UISwipeGestureRecognizer* swipe=[[UISwipeGestureRecognizer allock]initWithTarget:self action:@selector(swipeAct:)];

//设定滑动手势接受事件的类型,UISwipeGestureRecognizerDirectionLeft:向械滑动,UISwipeGestureRecognizerDirectionRight:向左滑动,UISwipeGestureRecognizerDirectionUp:向上滑动,UISwipeGestureRecognizerDirectionDown:向下滑动

//用‘|‘表示支持多个事件,或的关系

swipe.direction=UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight;

[iView addGestureRecognizer:swipe];

//创建长按手势

UILongPressGestureRecgnizer* longPress=[UILongPressGestureRecognizer alloc]initWithTarget:self acion:@selector(pressLong:)];

//设置长按手势时间,默认0.5秒时间长按手势

longPress.minimumPressDuration=0.5;

}

-(void)pressLong:(UILongPressGestureRecognizer*)press{

//手势的状态对象,到达规定时间,秒钏触发函数

if(press.state==UIGestureRecognizerStateBegan){

NSLog(@"状态开始!");

}

//当手指离开屏幕时,结束状态

else if(press.state==UIGestureRecognizerStateEended){

NSLog(@"结束状态!");

}

NSLog(@"长按手势!");

}

//指有向指定方滑动才执行

-(void)swipeAct(UISwipeGestureRecognizer*) swipe{

if(swipe.direction & UISwipeGestureRecognizerDirectionLeft){

NSLog(@"向左滑动!");

}else if(swipe.direction & UISwipeGestureRecognizerDirectionRight){

NSLog(@"向右滑动!");

}

}

你可能感兴趣的:(IOS开发 UIGesture扩展手势)