ios 手勢(shì)使用

手勢(shì)的分類(lèi)

(一) 手勢(shì)分類(lèi)
state 狀態(tài) (UIGestureRecognizerStateBegan Change ended)不同狀態(tài)
view當(dāng)前被點(diǎn)擊的View

  • tap 輕觸
    • tap.numberOfTapsRequired = 2 連續(xù)點(diǎn)擊次數(shù)
    • tap.numberOfTouchesRequired = 2 手指的個(gè)數(shù)
  • swipe 輕掃
    direction = directionLeft | directionRight 但是 這會(huì)改變direction的值,所以一般是手動(dòng)添加多個(gè)
  • pan 拖拽
    CGPoint panPoint = [ pan translationInView : pan.view ]
  • rotate 旋轉(zhuǎn)
    rotation 角度 每一次的調(diào)度變化都會(huì)累加的
  • pinch 捏合
    scale 放大比例 每一縮放都會(huì)累加
  • longPress 長(zhǎng)按
    • minimumPressDuration 長(zhǎng)按多長(zhǎng)時(shí)間觸發(fā)
    • allowableMovement 誤差值
    • if (longPress . state == UIGestureRecognizerStateBegan ){ //長(zhǎng)按開(kāi)始 執(zhí)行 } 分狀態(tài)執(zhí)行方法

拖拽簡(jiǎn)單使用

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *purpleView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

-(void)viewDidLoad {
[super viewDidLoad];

_imageView.userInteractionEnabled = YES;
_imageView.multipleTouchEnabled = YES;

// 輕觸

// 1. 實(shí)例化手勢(shì)識(shí)別對(duì)象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];


[_imageView addGestureRecognizer:pan];

}

// 3. 實(shí)現(xiàn)監(jiān)聽(tīng)方法
-(void)pan:(UIPanGestureRecognizer *)pan {

CGPoint translatePoint = [pan translationInView:pan.view];

// 讓view進(jìn)行移動(dòng)
pan.view.transform = CGAffineTransformTranslate(pan.view.transform,translatePoint.x, translatePoint.y);


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

}

@end


旋轉(zhuǎn)的簡(jiǎn)單使用

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *purpleView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

-(void)viewDidLoad {
[super viewDidLoad];

_imageView.userInteractionEnabled = YES;
_imageView.multipleTouchEnabled = YES;

// 輕觸

// 1. 實(shí)例化手勢(shì)識(shí)別對(duì)象
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];


[_imageView addGestureRecognizer:rotate];

}

// 3. 實(shí)現(xiàn)監(jiān)聽(tīng)方法
-(void)rotate:(UIRotationGestureRecognizer *)rotate {

NSLog(@"------ %f", rotate.rotation);


// 如果手指頭停止旋轉(zhuǎn), 當(dāng)再次旋轉(zhuǎn)的時(shí)候 就會(huì)從0 開(kāi)始

// rotate.view.transform = CGAffineTransformMakeRotation(rotate.rotation);

rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);

// 每一次旋轉(zhuǎn)之后, 旋轉(zhuǎn)的角度都從0開(kāi)始計(jì)算
rotate.rotation = 0;

}

@end


其他手勢(shì)都是類(lèi)似步驟,根據(jù)上面的關(guān)鍵字都可以提示的.

最后編輯于
?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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