ios 手勢使用

手勢的分類

(一) 手勢分類
state 狀態(tài) (UIGestureRecognizerStateBegan Change ended)不同狀態(tài)
view當前被點擊的View

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

拖拽簡單使用

@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. 實例化手勢識別對象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];


[_imageView addGestureRecognizer:pan];

}

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

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

// 讓view進行移動
pan.view.transform = CGAffineTransformTranslate(pan.view.transform,translatePoint.x, translatePoint.y);


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

}

@end


旋轉(zhuǎ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. 實例化手勢識別對象
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];


[_imageView addGestureRecognizer:rotate];

}

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

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


// 如果手指頭停止旋轉(zhuǎn), 當再次旋轉(zhuǎn)的時候 就會從0 開始

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

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

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

}

@end


其他手勢都是類似步驟,根據(jù)上面的關鍵字都可以提示的.

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

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

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