禁用全部手势

禁用全部手势

给一个imageview添加多个个手势如何在使用一个手势时 禁用另外其他全部手势

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
Tender10 2017-08-18 11:05:50

您好,您的问题我已经知道了,但是您的代码写得有些命名不太规范,这里我在我上一份的案例中给您解决了这个问题,你可以自己去练习一下,感受一下。如果还有什么不明白的地方,可以继续提问。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#import "ViewController.h"
 
@interface ViewController ()
 
@property (nonatomic,strong)NSArray *labelArray;
@property (nonatomic,strong)UIImageView *imageView;
 
@end
 
@implementation ViewController
 
 
- (void)viewDidLoad {
    [super viewDidLoad];
     
    [self imageViewAddGesture];
    [self setFrame];
}
 
- (void)imageViewAddGesture {
     
    // 设置一个图片的大小、位置、颜色
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    self.imageView.userInteractionEnabled = YES;
    self.imageView.backgroundColor = [UIColor redColor];
     
    [self.view addSubview: self.imageView];
 
     
}
 
// 点击、长按、滑动手势的响应事件
-(void)addTapGesture:(UITapGestureRecognizer *)tap {
    NSLog(@"点击");
    tap.enabled = NO;
}
 
-(void)addLongGesture:(UILongPressGestureRecognizer *)longGesture {
    if (longGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按");
    }
    longGesture.enabled = NO;
}
 
-(void)addPanGesture:(UIPanGestureRecognizer *)panGesture {
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"滑动");
    }
    panGesture.enabled = NO;
}
 
// 按钮位置
- (void)setFrame {
     
    for (int i = 0; i < 3; i++) {
         
        UIButton *gestureButton = [[UIButton alloc] initWithFrame:CGRectMake(100+i*60, 100, 40, 30)];
        gestureButton.backgroundColor = [UIColor yellowColor];
        [gestureButton addTarget:self action:@selector(gestureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [gestureButton setTitle:self.labelArray[i] forState:UIControlStateNormal];
        [gestureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        gestureButton.tag = i;
        [self.view addSubview:gestureButton];
    }
     
}
 
// 存储按钮的Title
- (NSArray *)labelArray {
     
    if (_labelArray == nil) {
        _labelArray = [NSArray arrayWithObjects:@"点击",@"长按",@"滑动",nil];
    }
    return _labelArray;
}
 
// 按钮的点击响应事件
- (void)gestureButtonClick:(UIButton *)sender {
     
    NSInteger tag = sender.tag;
     
    if (tag == 0) {
        UITapGestureRecognizer *singleTap =
        [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addTapGesture:)];
        [self.imageView addGestureRecognizer:singleTap];
         
    else if(tag == 1){
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addLongGesture:)];
        [self.imageView addGestureRecognizer:longGesture];
    else{
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(addPanGesture:)];
        [self.imageView addGestureRecognizer:panGesture];
 
    }
     
}
 
@end


提问者 慕粉1330426222 2017-08-17 20:17:49

http://img1.sycdn.imooc.com/climg//5995889e0001d9fc04050079.jpg

http://img1.sycdn.imooc.com/climg//5995889f000168a410570782.jpg

这个是测试的 第一张图是测试结果  测试过程是点击单击按钮后点击imageview触发点击手势打印 点击 俩字 第二个是我点击完单击后 点击长按按钮 在imageview中做出单击的手势 结果也打印出来 了 单击 二字 在我点击长按的时候 imageview上并没有禁用 单击手势

麻烦老师帮忙指出错误! 

Tender10 2017-08-17 17:37:39

下面给你写了一个图片添加多个手势的案例,并且使用一个手势禁用其他全部手势

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
- (void)viewDidLoad {
    [super viewDidLoad];
     
    // 设置一个图片的大小、位置、颜色
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    imageView.userInteractionEnabled = YES;
    imageView.backgroundColor = [UIColor redColor];
     
    // 创建点击、长按、滑动三个手势
    UITapGestureRecognizer *singleTap =
    [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addTapGesture:)];
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addLongGesture:)];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(addPanGesture:)];
     
    // 给同一个图片添加三个手势
    [imageView addGestureRecognizer:singleTap];
    [imageView addGestureRecognizer:longGesture];
    [imageView addGestureRecognizer:panGesture];
     
    [self.view addSubview: imageView];
     
}
 
// 点击、长按、滑动手势的响应事件
-(void)addTapGesture:(UITapGestureRecognizer *)tap {
    NSLog(@"点击");
}
 
-(void)addLongGesture:(UILongPressGestureRecognizer *)longGesture {
    if (longGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按");
    }
}
 
-(void)addPanGesture:(UIPanGestureRecognizer *)panGesture {
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"滑动");
    }
   
}

如果看不懂,或者还是有疑问可以继续提问。

  • 提问者 慕粉1330426222 #1
    我想问:当我点击A按钮时给B imageview 添加一个点击手势 当我点击B按钮时 给imageview 添加一个长按手势 当我A、B分别点击后imageview上就有了点击和长按俩个手势 如何让我再次点击B按钮是imageview上只有长按手势响应 而点击按钮不响应 同样点击按钮也是
    2017-08-17 19:10:17
  • Tender10 回复 提问者 慕粉1330426222 #2
    您好,上面的代码建议你先运行一下实现一下效果,其实上面的代码就是一张图片上添加多个手势,当使用的是点击手势时,其他按钮就无法响应。你下面的那个需求,其实每个按钮的响应事件都不同,响应方法都不同,根本就不会出现混乱的情况。建议你自己可以先尝试一下。
    2017-08-17 19:22:40
  • 提问者 慕粉1330426222 回复 Tender10 #3
    麻烦老师看我回复的图和文字 谢谢老师了
    2017-08-17 20:18:43
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码