本節(jié)學(xué)習(xí)內(nèi)容
1.UIGesture擴(kuò)展手勢類型
2.UIGesture擴(kuò)展手勢屬性
3.UIGesture擴(kuò)展手勢用法
UIGesture擴(kuò)展手勢
【UIPan手勢】
平移手勢:可以用手指在屏幕上移動的手勢
【UISwipe手勢】
滑動手勢:左滑,右滑溯乒,上滑柴底,下滑
【UILongPress手勢】
長按手勢:長時間按住一個試圖響應(yīng)事件
【重點屬性函數(shù)】
minimumPressDuration:長按時間長充
direction:滑動手勢方向
UIGestureRecognizerStateBegan:長按時間狀態(tài)
translationInView:獲取平移手勢位置
velocityInView:獲取平移手勢的速度
【ViewController.m】
UIImageView* iView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"17_2.jpg"]];
iView.frame=CGRectMake(50,50,200,300);
iView.userInteractionEnabled=YES;
//創(chuàng)建一個平易手勢者铜,參數(shù)1:事件函數(shù)處理對象,參數(shù)2:事件函數(shù)
UIPanGestureRecognizer* pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAct:)];
//將手勢添加到圖像視圖中
[iView addGestureRecognizer:pan];
//將移動事件手勢從圖像中取消
[iView removeGestureRecognizer:pan];
[self.view addSubview:iView];
}
//移動事件函數(shù)泌射,只要手指坐標(biāo)在屏幕上發(fā)生變化時崔慧,函數(shù)就被調(diào)用
-(void)panAct:(UIPanGestureRecognizer*)pan{
//獲取移動的從標(biāo),現(xiàn)對于視圖的坐標(biāo)系統(tǒng)颓鲜,參數(shù):相對的視圖對象
CGPoint pt=[pan translationInView:self.view];
//打印移動坐標(biāo)位置
//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);
//創(chuàng)建一個滑動手勢
UISwipeGestureRecognizer* swipe=[[UISwipeGestureRecognizer allock]initWithTarget:self action:@selector(swipeAct:)];
//設(shè)定滑動手勢接受事件的類型,UISwipeGestureRecognizerDirectionLeft:向械滑動典予,UISwipeGestureRecognizerDirectionRight:向左滑動,UISwipeGestureRecognizerDirectionUp:向上滑動,UISwipeGestureRecognizerDirectionDown:向下滑動
//用‘|‘表示支持多個事件乐严,或的關(guān)系
swipe.direction=UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight;
[iView addGestureRecognizer:swipe];
//創(chuàng)建長按手勢
UILongPressGestureRecgnizer* longPress=[UILongPressGestureRecognizer alloc]initWithTarget:self acion:@selector(pressLong:)];
//設(shè)置長按手勢時間瘤袖,默認(rèn)0.5秒時間長按手勢
longPress.minimumPressDuration=0.5;
}
-(void)pressLong:(UILongPressGestureRecognizer*)press{
//手勢的狀態(tài)對象,到達(dá)規(guī)定時間昂验,秒釧觸發(fā)函數(shù)
if(press.state==UIGestureRecognizerStateBegan){
NSLog(@"狀態(tài)開始捂敌!");
}
//當(dāng)手指離開屏幕時,結(jié)束狀態(tài)
else if(press.state==UIGestureRecognizerStateEended){
NSLog(@"結(jié)束狀態(tài)既琴!");
}
NSLog(@"長按手勢占婉!");
}
//指有向指定方滑動才執(zhí)行
-(void)swipeAct(UISwipeGestureRecognizer*) swipe{
if(swipe.direction & UISwipeGestureRecognizerDirectionLeft){
NSLog(@"向左滑動!");
}else if(swipe.direction & UISwipeGestureRecognizerDirectionRight){
NSLog(@"向右滑動甫恩!");
}
}