iOS 常見的六種手勢

六種手勢如下:

[UITapGestureRecognizer](點(diǎn)擊)

[UIPinchGestureRecognizer](捏合)

[UIRotationGestureRecognizer](旋轉(zhuǎn))

([UISwipeGestureRecognizer]滑動,快速移動)

[UIPanGestureRecognizer](平移,慢速移動)

[UILongPressGestureRecognizer](長按)

以上六種只是常用的一些手勢,在這里作此簡述


創(chuàng)建圖片對象

// 創(chuàng)建圖片對象

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

// 設(shè)置圖片內(nèi)容

imageView.image = [UIImage imageNamed:@"自己圖片名"];

// 設(shè)置圖片居中

imageView.center = self.view.center;

// 設(shè)置用戶交互

imageView.userInteractionEnabled = YES;

// 添加到視圖上

[self.view addSubview:imageView];

UITapGestureRecognizer? (點(diǎn)擊手勢)

//1.創(chuàng)建手勢對象

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];

//2.手勢相關(guān)的屬性

//點(diǎn)擊次數(shù)(默認(rèn)1)

tapGesture.numberOfTapsRequired = 1;

//手指的個數(shù)(默認(rèn)1)

tapGesture.numberOfTouchesRequired = 1;

//3.把手勢與視圖相關(guān)聯(lián)

[imageView addGestureRecognizer:tapGesture];

// 點(diǎn)擊事件

-(void)tapClick:(UITapGestureRecognizer *)tap{

NSLog(@"點(diǎn)擊圖片了");

}

UIPinchGestureRecognizer? (捏合手勢)

//捏合手勢

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichClick:)];

[imageView addGestureRecognizer:pinchGesture];

// 捏合事件

-(void)pichClick:(UIPinchGestureRecognizer *)pinch{

//縮放的系數(shù)

NSLog(@"%.2lf",pinch.scale);

//固定寫法

pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

//重置縮放系數(shù)(否則系數(shù)會累加)

pinch.scale = 1.0;

}

UIRotationGestureRecognizer? (旋轉(zhuǎn)手勢)

//旋轉(zhuǎn)手勢

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:)];

// 設(shè)置輕掃的方向

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(@"響應(yīng)了。。。");

//通過pan手勢,能夠獲取到pan.view在self.view上的偏移量

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

NSLog(@"x=%.2lf y=%.2lf",point.x,point.y);

//改變中心點(diǎn)坐標(biāo)(原來的中心點(diǎn)+偏移量=當(dāng)前的中心點(diǎn))

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;

//每次調(diào)用之后,需要重置手勢的偏移量,否則偏移量會自動累加

[pan setTranslation:CGPointZero inView:self.view];

}

UILongPressGestureRecognizer? (長按手勢)

//長按手勢

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];

//用幾個手指觸屏,默認(rèn)1

longPressGesture.numberOfTouchesRequired = 1;

//設(shè)置最短長按時間,單位為秒(默認(rèn)0.5)

longPressGesture.minimumPressDuration = 1;

//設(shè)置手勢識別期間所允許的手勢可移動范圍

longPressGesture.allowableMovement = 10;

[imageView addGestureRecognizer:longPressGesture];

// 長按事件

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

//state屬性是所有手勢父類提供的方法,用于記錄手勢的狀態(tài)

if(press.state == UIGestureRecognizerStateBegan){

NSLog(@"長按手勢開始響應(yīng)!");

}else if (press.state == UIGestureRecognizerStateChanged){

NSLog(@"長按手勢狀態(tài)發(fā)生改變!");

}else{

NSLog(@"長按手勢結(jié)束!");

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容