iOS UIGestureRecognizer(手勢、單擊、拖拽 、拖拽、旋轉、輕掃 、長按、邊緣滑動,功能介紹)

//聯系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄

#import"ViewController.h"

@interfaceViewController ()

{

UITapGestureRecognizer *_tap;//單擊

UIPanGestureRecognizer *_pan;//拖拽

UIPinchGestureRecognizer *_pinch;//拖拽

UIRotationGestureRecognizer *_rotation;//旋轉

UISwipeGestureRecognizer *_swipe;//輕掃

UILongPressGestureRecognizer *_longpress;//長按

UIScreenEdgePanGestureRecognizer *_edgePan;//邊緣滑動手勢

}

@property(nonatomic,weak)IBOutletUIView*testView;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

[selfaddTapGesture];

[selfaddPanGesture];

[selfaddPinchGesture];

[selfaddRotationGesture];

[selfaddSwipeGesture];

[selfaddLongpressGesture];

[selfaddEdgePanGesture];

//手勢謙讓

[selfgestureHumility];

}

#pragma mark -手勢

//單擊

- (void)addTapGesture{

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

_tap.numberOfTapsRequired =1;

[_testView addGestureRecognizer:_tap];

}

//拖拽

- (void)addPanGesture{

_pan = [[UIPanGestureRecognizer alloc]initWithTarget:selfaction:@selector(panAction:)];

_pan.minimumNumberOfTouches =1;

[_testView addGestureRecognizer:_pan];

}

//拖拽

- (void)addPinchGesture{

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

[_testView addGestureRecognizer:_pinch];

}

//旋轉

- (void)addRotationGesture{

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

[_testView addGestureRecognizer:_rotation];

}

//輕掃

- (void)addSwipeGesture{

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

//指定掃動方向

_swipe.direction = UISwipeGestureRecognizerDirectionDown;

[_testView addGestureRecognizer:_swipe];

}

//長按

- (void)addLongpressGesture{

_longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:selfaction:@selector(longpressAction:)];

_longpress.minimumPressDuration =1.0;

[_testView addGestureRecognizer:_longpress];

}

//邊緣滑動手勢

- (void)addEdgePanGesture{

_edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:selfaction:@selector(edgePanAction:)];

_edgePan.edges = UIRectEdgeLeft;

[self.view addGestureRecognizer:_edgePan];

}

#pragma mark -動作

//單擊

- (void)tapAction:(UITapGestureRecognizer*)tap{

NSLog(@"單擊");

}

//拖拽

- (void)panAction:(UIPanGestureRecognizer*)pan{

NSLog(@"拖拽");

CGPointpoint = [pantranslationInView:pan.view];

//? ? pan.view.transform =CGAffineTransformMakeTranslation(point.x, point.y);

pan.view.transform=CGAffineTransformTranslate(pan.view.transform, point.x, point.y);

[pansetTranslation:CGPointZeroinView:pan.view];

}

//捏合

- (void)pinchAction:(UIPinchGestureRecognizer*)pinch{

NSLog(@"捏合");

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

}

//旋轉

- (void)rotationAction:(UIRotationGestureRecognizer*)rotation{

NSLog(@"旋轉");

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

}

//輕掃

- (void)swipeAction:(UISwipeGestureRecognizer*)swipe{

NSLog(@"向下輕掃");

}

//長按

- (void)longpressAction:(UILongPressGestureRecognizer*)longpress{

NSLog(@"長按");

}

//邊緣滑動手勢

- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer*)edgePan{

NSLog(@"左邊緣滑動");

UIColor*random = [UIColorcolorWithRed:arc4random()%256/255.0green:arc4random()%256/255.0blue:arc4random()%256/255.0alpha:1.0];

self.view.backgroundColor = random;

}

#pragma mark - privatemethods//手勢謙讓

- (void)gestureHumility{

[_pan requireGestureRecognizerToFail:_swipe];

}

@end

謝謝!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容