UITapGestureRecognizer //點(diǎn)擊手勢(shì)
UILongPressGestureRecognizer // 長(zhǎng)按手勢(shì)
UISwipeGestureRecognizer // 輕掃手勢(shì)
UIRotationGestureRecognizer // 旋轉(zhuǎn)手勢(shì)
UIPinchGestureRecognizer //捏合手勢(shì)
UIPanGestureRecognizer //拖拽手勢(shì)
- UIGestureRecognizerDelegate
// 是否允許開始觸發(fā)手勢(shì)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
// 是否允許同時(shí)支持多個(gè)手勢(shì),默認(rèn)是不支持多個(gè)手勢(shì)
// 返回yes表示支持多個(gè)手勢(shì)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// 是否允許接收手指的觸摸點(diǎn)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// 獲取當(dāng)前的觸摸點(diǎn)
CGPoint curP = [touch locationInView:self.imageView];
//下面表示當(dāng)觸摸點(diǎn)在self.imageView 在中心點(diǎn)左面點(diǎn)擊不觸發(fā)事件块差,在右面則觸發(fā)點(diǎn)擊事件
if (curP.x < self.imageView.bounds.size.width * 0.5) {
return NO;
}else{
return YES;
}
}
// 創(chuàng)建點(diǎn)按手勢(shì)
- (void)setUpTap
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.delegate = self;
//連續(xù)點(diǎn)擊兩次
tap.numberOfTapsRequired = 2;
//需要兩跟手指一起敲擊
tap.numberOfTouchesRequired =2;
[_imageView addGestureRecognizer:tap];
}
- (void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@"%s",__func__);
}
//創(chuàng)建長(zhǎng)按手勢(shì)
- (void)setUpLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 默認(rèn)會(huì)觸發(fā)兩次侵续,所以需要加上狀態(tài)去限制
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"%s",__func__);
}
}
//創(chuàng)建輕掃手勢(shì)
- (void)setUpSwipe
{
// 默認(rèn)輕掃的方向是往右
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
//設(shè)置輕掃手勢(shì)的方向
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
// 如果以后想要一個(gè)控件支持多個(gè)方向的輕掃,必須創(chuàng)建多個(gè)輕掃手勢(shì)憨闰,一個(gè)輕掃手勢(shì)只支持一個(gè)方向
// 默認(rèn)輕掃的方向是往右
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:swipeDown];
}
- (void)swipe
{
NSLog(@"%s",__func__);
}
//創(chuàng)建旋轉(zhuǎn)手勢(shì)
- (void)setUpRotation
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
}
// 默認(rèn)傳遞的旋轉(zhuǎn)的角度都是相對(duì)于最開始的位置
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
// 可以不加下面這句復(fù)位的代碼試一下效果
// 復(fù)位
rotation.rotation = 0;
// 獲取手勢(shì)旋轉(zhuǎn)的角度
NSLog(@"%f",rotation.rotation);
}
//創(chuàng)建捏合手勢(shì)
- (void)setUpPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
//默認(rèn)傳遞的的縮放比例都是相對(duì)于最開始的大小
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 復(fù)位
pinch.scale = 1;
}
//創(chuàng)建拖拽手勢(shì)
- (void)setUpPan
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
//一樣是默認(rèn)是相對(duì)于最初位置
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取手勢(shì)的觸摸點(diǎn)
// CGPoint curP = [pan locationInView:self.imageView];
// 移動(dòng)視圖
// 獲取手勢(shì)的移動(dòng)状蜗,也是相對(duì)于最開始的位置
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 復(fù)位
[pan setTranslation:CGPointZero inView:self.imageView];
// NSLog(@"%@",NSStringFromCGPoint(curP));
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者