UIKit中 輕拍、長按、清掃、旋轉、捏合、拖拽 手勢詳解
在OC的UIKit 架構中有很多手勢,經常用的也無非就 輕拍、長按、清掃、旋轉、捏合、拖拽 這幾種。接下來一一介紹一下這些手勢的用法。
在OC中每一種手勢都是一個都已經封轉成一個相應的類,是以用起來很友善。
為了友善看出手勢的效果,我對一個圖檔進行操作,定義了一個 UIImageView 屬性。
一、輕拍
輕拍手勢 就是點選的意思,你可以設定點選一次 或者多次!
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)] ;
tap.numberOfTapsRequired = 2 ; // 輕拍的次數 輕按兩下 !
[imgV addGestureRecognizer:tap] ;// 把手勢添加到 imageView 上
上面代碼的意思是 執行個體化一個 UITapGestureRecognizer 對象,tap就可以實作 點選操作,參數 self 是 設定自己為代理,tapAction 是執行這個手勢後要調用的的函數。可以設定點選的次數。然後把這個手勢添加到 ImageView上。設定完後 别忘了在初始化的時候 橋上這句代碼
imgV.userInteractionEnabled = YES ;// 設定ImageView支援使用者互動 屬性
因為預設的imageView是不支援使用者手勢的。
現在就可以編寫 相應輕拍函數要調用的函數了
-(void) tapAction
{
imgV.image = [UIImage imageNamed:@"test.jpg"] ;
}
點選圖檔就會切換到test.jpg圖檔。
二、長按
長按手勢就是按着圖檔一定時間後執行的動作! 這是時間是可以設定的。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(topAction)] ;
longPress.minimumPressDuration = 1 ;// 長按的時間
[imgV addGestureRecognizer:longPress] ;
設定一下長按的時間為 1秒,把長按的手勢添加到imageview上。長按imageview 就會調用更換頭片的方法。
三、清掃
清掃手勢就是 上下左右滑動的意思。
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doswipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[imgV addGestureRecognizer:swipe] ;
swipe.direction 就是設定相應的手勢,在這裡設定的是 左劃 或右劃,就是左劃和右劃都會相應。就行手機上的相冊一樣,當我們左右滑動的适合,我們希望圖檔會左右切換。就可以在doswipe函數中判斷,然後再執行相應的方法。
要調用的方法是有參數的,不要忘記寫 doswipe後面的 冒号
四、旋轉 旋轉是多點觸控,就是用兩個或多個手指在螢幕畫圓,旋轉一定的角度。
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(dorotat:)] ; [imgV addGestureRecognizer:rotation] ;
-(void) rotat:(UIRotationGestureRecognizer *)sender {
imgV.transform = CGAffineTransformMakeRotation(sender.rotation) ; //參數是目前旋轉的角度
}
CGAffineTransformMakeRotation(sender.rotation)
函數是用來旋轉的旋轉手勢你計算了一個角度而已。
在imageView上執行這個手勢後 圖檔就會旋轉相應的角度。
五、捏合 捏合和旋轉差不多都是 多點觸控的,就是兩個手往中間滑動是 縮小,往兩邊滑動是放大。然後根據放大或縮小的比例對圖檔進行縮放。
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(dopinch:)] ;
[imgV addGestureRecognizer:pinch] ;
把捏合手勢放到imageview上。
-(void) dopinch:(UIPinchGestureRecognizer *)sender {
imgV.transform = CGAffineTransformMakeScale(sender.scale, sender.scale) ;
}
CGAffineTransformMakeScale(sender.scale, sender.scale) ;函數是 縮放函數, 兩個參數分别是 x軸方向 和 Y軸方向的縮放比例
六、拖拽手勢 按住圖檔 移動到指定位置
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dopan:) ];
[imgV addGestureRecognizer:pan] ;
-(void) dopan:(UIPanGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
oldPoint = [sender locationInView:self.view] ;
}
CGPoint newPoint = [sender locationInView:self.view] ;
CGFloat dx = newPoint.x -oldPoint.x ;
CGFloat dy = newPoint.y - oldPoint.y ;
imgV.center = CGPointMake(imgV.center.x+dx, imgV.center.y + dy) ;//重新設定 圖檔中心點坐标
oldPoint = newPoint ;// 記錄 第二個點
}
2013-7-20 與 北京
版權所有 ,轉載請注明出去。