iOS 常见的六种 手势
六種手勢(shì)如下:
UITapGestureRecognizer
[UIPinchGestureRecognizer](捏合)
[UIRotationGestureRecognizer](旋轉(zhuǎn))
([UISwipeGestureRecognizer]滑動(dòng),快速移動(dòng))
[UIPanGestureRecognizer](平移,慢速移動(dòng))
[UILongPressGestureRecognizer](長(zhǎng)按)
以上六種只是常用的一些手勢(shì),在這里作此簡(jiǎn)述
創(chuàng)建圖片對(duì)象
// 創(chuàng)建圖片對(duì)象
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)擊手勢(shì))
//1.創(chuàng)建手勢(shì)對(duì)象
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//2.手勢(shì)相關(guān)的屬性
//點(diǎn)擊次數(shù)(默認(rèn)1)
tapGesture.numberOfTapsRequired = 1;
//手指的個(gè)數(shù)(默認(rèn)1)
tapGesture.numberOfTouchesRequired = 1;
//3.把手勢(shì)與視圖相關(guān)聯(lián)
[imageView addGestureRecognizer:tapGesture];
// 點(diǎn)擊事件
-(void)tapClick:(UITapGestureRecognizer *)tap{
NSLog(@“點(diǎn)擊圖片了”);
}
UIPinchGestureRecognizer (捏合手勢(shì))
//捏合手勢(shì)
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ù)會(huì)累加)
pinch.scale = 1.0;
}
UIRotationGestureRecognizer (旋轉(zhuǎn)手勢(shì))
//旋轉(zhuǎn)手勢(shì)
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];
[imageView addGestureRecognizer:rotationGesture];
-(void)rotationClick:(UIRotationGestureRecognizer *)rotation{
NSLog(@“qqq”);
}
UISwipeGestureRecognizer (滑動(dòng)手勢(shì))
// 滑動(dòng)手勢(shì)
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];
// 滑動(dòng)事件
-(void)swipeClick:(UISwipeGestureRecognizer *)swpie{
// 如果是左掃
if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {
NSLog(@“左掃!”);
}else{
NSLog(@“右掃!”);
}
}
UIPanGestureRecognizer (平移手勢(shì))
//平移手勢(shì)
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
[imageView addGestureRecognizer:panGesture];
// 平移事件
-(void)panClick:(UIPanGestureRecognizer *)pan{
NSLog(@“響應(yīng)了。。。”);
//通過pan手勢(shì),能夠獲取到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)
//限制拖動(dòng)范圍
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)用之后,需要重置手勢(shì)的偏移量,否則偏移量會(huì)自動(dòng)累加
[pan setTranslation:CGPointZero inView:self.view];
}
UILongPressGestureRecognizer (長(zhǎng)按手勢(shì))
//長(zhǎng)按手勢(shì)
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
//用幾個(gè)手指觸屏,默認(rèn)1
longPressGesture.numberOfTouchesRequired = 1;
//設(shè)置最短長(zhǎng)按時(shí)間,單位為秒(默認(rèn)0.5)
longPressGesture.minimumPressDuration = 1;
//設(shè)置手勢(shì)識(shí)別期間所允許的手勢(shì)可移動(dòng)范圍
longPressGesture.allowableMovement = 10;
[imageView addGestureRecognizer:longPressGesture];
// 長(zhǎng)按事件
-(void)longPressClick:(UILongPressGestureRecognizer *)press{
//state屬性是所有手勢(shì)父類提供的方法,用于記錄手勢(shì)的狀態(tài)
if(press.state == UIGestureRecognizerStateBegan){
NSLog(@“長(zhǎng)按手勢(shì)開始響應(yīng)!”);
}else if (press.state == UIGestureRecognizerStateChanged){
NSLog(@“長(zhǎng)按手勢(shì)狀態(tài)發(fā)生改變!”);
}else{
NSLog(@“長(zhǎng)按手勢(shì)結(jié)束!”);
}
}
總結(jié)
以上是生活随笔為你收集整理的iOS 常见的六种 手势的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我损失几百万换来的教训
- 下一篇: ARM指令集详解(超详细!带实例!)