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(@"向右滑動!");

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容