UI手勢

UIGestureRecognizer手勢識(shí)別器

手勢:有規(guī)律的觸摸

UIGestureRecognizer抽象類

七種手勢:輕拍(tap)長按(longPress)旋轉(zhuǎn)(rotation)捏合(pinch)拖拽(pan)輕掃(swipe)屏幕邊緣拖拽(screenEdgePan)

輕拍tap

創(chuàng)建對(duì)象

獲取到輕拍手勢時(shí)讓self調(diào)用tapAction:方法

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapAction:)];

添加手勢

[imgView addGestureRecognizer:tap];

內(nèi)存管理

[tap release];

點(diǎn)擊次數(shù)

tap.numberOfTapsRequired =2;

手指個(gè)數(shù)

tap.numberOfTouchesRequired =2;

#pragma mark -輕拍tap

- (void)tapAction:(UITapGestureRecognizer*)tap

{

NSLog(@"輕拍");

}

長按longPress

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(longPressAction:)];

[imgView addGestureRecognizer:longPress];

[longPress release];

長按時(shí)間

longPress.minimumPressDuration =1;

#pragma mark -長按longPress

- (void)longPressAction:(UILongPressGestureRecognizer*)longPress

{

if(longPress.state==UIGestureRecognizerStateBegan) {

NSLog(@"長按");

}

}

旋轉(zhuǎn)rotation

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotationAction:)];

[imgView addGestureRecognizer:rotation];

[rotation release];

#pragma mark -旋轉(zhuǎn)rotation

- (void)rotationAction:(UIRotationGestureRecognizer*)rotation

{

*UIView transform屬性專門用來進(jìn)行形變(位置position/旋轉(zhuǎn)rotation/縮放scale)設(shè)置

UIImageView*imgView = (UIImageView*)rotation.view;

imgView.transform=CGAffineTransformMakeRotation(rotation.rotation);

NSLog(@"旋轉(zhuǎn)");

}

捏合pinch

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinchAction:)];

[imgView addGestureRecognizer:pinch];

[pinch release];

#pragma mark -捏合pinch

- (void)pinchAction:(UIPinchGestureRecognizer*)pinch

{

UIImageView*imgView = (UIImageView*)pinch.view;

imgView.transform=CGAffineTransformMakeScale(pinch.scale, pinch.scale);

//縮放scale(比例)(下面兩行代碼效果等同于上一行)

//??? imgView.transform = CGAffineTransformScale(imgView.transform, pinch.scale, pinch.scale);

//??? pinch.scale = 1;

NSLog(@"捏合");

}

拖拽pan

UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];

[imgViewaddGestureRecognizer:pan];

[panrelease];

#pragma mark -拖拽pan

- (void)panAction:(UIPanGestureRecognizer*)pan

{

NSLog(@"拖拽");

UIImageView*imgView = (UIImageView*)pan.view;

CGPointp = [pantranslationInView:imgView];

imgView.transform=CGAffineTransformMakeTranslation(p.x, p.y);

//設(shè)置跟隨屬性(下面兩行代碼效果等同于上一行)

//??? imgView.transform = CGAffineTransformTranslate(imgView.transform, p.x, p.y);

//??? [pan setTranslation:CGPointZero inView:imgView];

}

輕掃swipe

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeAction:)];

默認(rèn)只識(shí)別向右

設(shè)置方向時(shí)最多只能設(shè)置水平(左/右)或者垂直(上/下)

swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

[imgView addGestureRecognizer:swipe];

[swipe release];

#pragma mark -輕掃swipe

- (void)swipeAction:(UISwipeGestureRecognizer*)swipe

{

if(swipe.direction==UISwipeGestureRecognizerDirectionLeft) {

NSLog(@"向左");

}

NSLog(@"輕掃");

}

屏幕邊緣拖拽screenEdgePan

UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];

sep.edges=UIRectEdgeLeft;

[self.viewaddGestureRecognizer:sep];

[seprelease];

#pragma mark -屏幕邊緣拖拽screenEdgePan

- (void)sepAction:(UIScreenEdgePanGestureRecognizer*)sep

{

NSLog(@"屏幕邊緣");

}

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

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

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