iOS 常見的六種 手勢
六種手勢如下:
[UIPinchGestureRecognizer](捏合)
[UIRotationGestureRecognizer](旋轉)
([UISwipeGestureRecognizer]滑動,快速移動)
[UIPanGestureRecognizer](平移,慢速移動)
[UILongPressGestureRecognizer](長按)
以上六種只是常用的一些手勢,在這裡作此簡述
建立圖片物件
// 建立圖片物件
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
// 設定圖片內容
imageView.image = [UIImage imageNamed:@“自己圖片名”];
// 設定圖片居中
imageView.center = self.view.center;
// 設定使用者互動
imageView.userInteractionEnabled = YES;
// 新增到檢視上
[self.view addSubview:imageView];
UITapGestureRecognizer (點選手勢)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//點選次數(預設1)
tapGesture.numberOfTapsRequired = 1;
//手指的個數(預設1)
tapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGesture];
// 點選事件
-(void)tapClick:(UITapGestureRecognizer *)tap{
NSLog(@“點選圖片了”);
}
UIPinchGestureRecognizer (捏合手勢)
//捏合手勢
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichClick:)];
[imageView addGestureRecognizer:pinchGesture];
// 捏合事件
-(void)pichClick:(UIPinchGestureRecognizer *)pinch{
//縮放的係數
NSLog(@"%.2lf",pinch.scale);
//固定寫法
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//重置縮放係數(否則係數會累加)
pinch.scale = 1.0;
}
UIRotationGestureRecognizer (旋轉手勢)
//旋轉手勢
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];
[imageView addGestureRecognizer:rotationGesture];
-(void)rotationClick:(UIRotationGestureRecognizer *)rotation{
NSLog(@“qqq”);
}
UISwipeGestureRecognizer (滑動手勢)
// 滑動手勢
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
// 設定輕掃的方向
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
// 右掃
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipe];
// 滑動事件
-(void)swipeClick:(UISwipeGestureRecognizer *)swpie{
// 如果是左掃
if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {
NSLog(@“左掃!”);
}else{
NSLog(@“右掃!”);
}
}
UIPanGestureRecognizer (平移手勢)
//平移手勢
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
[imageView addGestureRecognizer:panGesture];
// 平移事件
-(void)panClick:(UIPanGestureRecognizer *)pan{
NSLog(@“響應了。。。”);
//通過pan手勢,能夠獲取到pan.view在self.view上的偏移量
CGPoint point = [pan translationInView:self.view];
NSLog(@“x=%.2lf y=%.2lf”,point.x,point.y);
//改變中心點座標(原來的中心點+偏移量=當前的中心點)
CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);
// CGPointZero<==>CGPointMake(0,0)
//限制拖動範圍
newCenter.y = MAX(pan.view.frame.size.height/2, newCenter.y);
newCenter.y = MIN(self.view.frame.size.height - pan.view.frame.size.height/2, newCenter.y);
newCenter.x = MAX(pan.view.frame.size.width/2, newCenter.x);
newCenter.x = MIN(self.view.frame.size.width - pan.view.frame.size.width/2, newCenter.x);
pan.view.center = newCenter;
//每次呼叫之後,需要重置手勢的偏移量,否則偏移量會自動累加
[pan setTranslation:CGPointZero inView:self.view];
}
UILongPressGestureRecognizer (長按手勢)
//長按手勢
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
//用幾個手指觸屏,預設1
longPressGesture.numberOfTouchesRequired = 1;
//設定最短長按時間,單位為秒(預設0.5)
longPressGesture.minimumPressDuration = 1;
//設定手勢識別期間所允許的手勢可移動範圍
longPressGesture.allowableMovement = 10;
[imageView addGestureRecognizer:longPressGesture];
// 長按事件
-(void)longPressClick:(UILongPressGestureRecognizer *)press{
//state屬性是所有手勢父類提供的方法,用於記錄手勢的狀態
if(press.state == UIGestureRecognizerStateBegan){
NSLog(@“長按手勢開始響應!”);
}else if (press.state == UIGestureRecognizerStateChanged){
NSLog(@“長按手勢狀態發生改變!”);
}else{
NSLog(@“長按手勢結束!”);
}
}