UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.view addGestureRecognizer:tap];
//轻拍次数
tap.numberOfTapsRequired = 1;
//需要几个手指
tap.numberOfTouchesRequired = 1;
- (void)tapAction:(UITapGestureRecognizer *)tap
{
//得到 tag 值
NSInteger number = tap.view.tag;
NSString *number1 = [NSString stringWithFormat:@"%ld", number];
}
//向右滑动 取消模态
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
[swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRecognizer];
//右滑手势
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)aaa
{
//如果是右滑
if (aaa.direction == UISwipeGestureRecognizerDirectionRight) {
[self.navigationController popViewControllerAnimated:YES];
}
}
// 创建长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
// 触发长按的时间
longPressGesture.minimumPressDuration = 0.5;
[cell addGestureRecognizer:longPressGesture];
-(void)longPressGesture:(UILongPressGestureRecognizer *)longPressGesture
{
if (longPressGesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按手势开启");
longPressGesture.view .backgroundColor = [UIColor redColor];
}
if (longPressGesture.state == UIGestureRecognizerStateEnded) {
NSLog(@"长按手势结束");
longPressGesture.view .backgroundColor = [UIColor yellowColor];
}else {
NSLog(@"================");
}
}
// 创建拖拽手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
[self.button addGestureRecognizer:panGesture];
//拖拽手势
-(void)panGestureAction:(UIPanGestureRecognizer *)panGesture
{
//frame写法
CGPoint newCenter = CGPointMake([panGesture translationInView:panGesture.view].x + self.button.center.x, [panGesture translationInView:panGesture.view].y + self.button.center.y);
self.button.center = newCenter;
[panGesture setTranslation:CGPointZero inView:panGesture.view];
//masonry的初始化
[self.button mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(CGPointMake(0, 0)).priorityLow();
make.size.mas_equalTo(CGSizeMake(100, 100));
make.left.greaterThanOrEqualTo(self.view);
make.top.greaterThanOrEqualTo(self.view).offset(0);
make.bottom.right.lessThanOrEqualTo(self.view);
}];
//masonry写法
UIView *button = panGesture.view;
CGPoint newCenter = CGPointMake([panGesture translationInView:panGesture.view].x + button.center.x - [UIScreen mainScreen].bounds.size.width / 2, [panGesture translationInView:panGesture.view].y + button.center.y - [UIScreen mainScreen].bounds.size.height / 2);
[button mas_updateConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(newCenter).priorityLow();
}];
[panGesture setTranslation:CGPointZero inView:panGesture.view];
}
locationInView:获取到的是手指点击屏幕实时的坐标点;
translationInView:获取到的是手指移动后,在相对坐标中的偏移量
轻拍手势,左滑手势 右滑手势 长按手势 拖拽手势
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- //联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄 #import"ViewController.h"...
- 1、手势识别器——UIGestureRecognizer 介绍在ios开发中,除了有关触摸的这组方法来控制使用用者...
- #import "ViewController.h" @interface ViewController (){ ...
- 上运行时图 使用方式,百公里加速只需要三行代码: ItemTouchHelperCallback callback...