UITapGestureRecognizer 不适用于自定义 UIView 类
Posted
技术标签:
【中文标题】UITapGestureRecognizer 不适用于自定义 UIView 类【英文标题】:UITapGestureRecognizer not working on custom UIView class 【发布时间】:2015-09-08 05:33:15 【问题描述】:我制作了一个自定义视图类。我在我的视图控制器类中初始化它。我已经启用了用户交互,但它也不起作用。我在类似的问题中搜索过它,但大多数都说启用用户交互。 这是我写的代码。
@interface ProfileCreatorViewController ()
SectionTitleView *ProfileTitle;
CGRect mainFrame;
@end
@implementation ProfileCreatorViewController
- (void)viewDidLoad
[super viewDidLoad];
mainFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + NAVBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height);
CGRect profileFrame = CGRectMake(mainFrame.origin.x + 5, mainFrame.origin.y, mainFrame.size.width - 20, 50);
ProfileTitle = [[SectionTitleView alloc]initWithFrame:profileFrame withTitle:@"Profile" withUnderLineColor:[UIColor blackColor] withDownButton:[UIImage imageNamed:@"rightArrow"]];
[self.view addSubview:ProfileTitle];
UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downButtonClicked)];
[ProfileTitle addGestureRecognizer:recog];
ProfileTitle.userInteractionEnabled = YES;
-(void) downButtonClicked
NSLog(@"clicked");
【问题讨论】:
【参考方案1】:你可以在这里检查几件事 profileFrame的高宽都不是很小(打印profileFrame)
ProfileTitle 不完全透明(当视图的 alpha 非常接近 0 时,手势也不起作用)
ProfileTitle 不会被任何其他视图遮挡(为此使用可视化调试器)
【讨论】:
完全透明的视图是罪魁祸首,谢谢 :)【参考方案2】:@interface ProfileCreatorViewController ()<UIGestureRecognizerDelegate>
SectionTitleView *ProfileTitle;
CGRect mainFrame;
@end
@implementation ProfileCreatorViewController
- (void)viewDidLoad
[super viewDidLoad];
mainFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + NAVBAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height);
CGRect profileFrame = CGRectMake(mainFrame.origin.x + 5, mainFrame.origin.y, mainFrame.size.width - 20, 0);
ProfileTitle = [[SectionTitleView alloc]initWithFrame:profileFrame withTitle:@"Profile" withUnderLineColor:[UIColor blackColor] withDownButton:[UIImage imageNamed:@"rightArrow"]];
[self.view addSubview:ProfileTitle];
UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downButtonClicked:)];
recog.delegate=self;
[ProfileTitle addGestureRecognizer:recog];
ProfileTitle.userInteractionEnabled = YES;
-(void) downButtonClicked:(UITapGestureRecognizer *)sender
NSLog(@"clicked");
添加委托并更改您的方法签名
【讨论】:
不,还没醒。不过,谢谢。NSLog
youe view 检查视图是否被分配到点击手势【参考方案3】:
试试这些..
像这些一样创建您的自定义视图的属性..并将其导入到您的 ProfileCreatorViewController.h 文件中..
#import "SectionTitleView.h"
@property (nonatomic , strong) SectionTitleView *previewView;
添加 UIGestureRecognizerDelegate
@interface ProfileCreatorViewController ()<UIGestureRecognizerDelegate>
SectionTitleView *ProfileTitle;
CGRect mainFrame;
根据需要设置 NumberOfTapsRequired
UITapGestureRecognizer *recog =
[[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(downButtonClicked:)];
ProfileTitle.userInteractionEnabled = YES;
[recog setNumberOfTapsRequired:1]; //No. of taps..
[ProfileTitle addGestureRecognizer:recog];
还有它的方法
-(void) downButtonClicked:(UITapGestureRecognizer*)gesture
NSLog(@"Taped");
希望对你有帮助..
【讨论】:
【参考方案4】:您需要在头文件中使用UIGestureRecognizerDelegate
。
还要创建一个property
来将UITapGestureRecognizer
保存在里面:
@property (strong, nonatomic) UITapGestureRecognizer tapGesture;
您也不应该在父类中创建UITapGestureRecognizer
。 ProfileTitle 应自行生成。所以你最好将代码插入到 SectionTitleView 内的viewDidLoad
内。
仅供参考:不要使用大写的变量名
【讨论】:
以上是关于UITapGestureRecognizer 不适用于自定义 UIView 类的主要内容,如果未能解决你的问题,请参考以下文章
UITapGestureRecognizer 不适用于 UIImageView 自定义类
UITapGestureRecognizer 不适用于 UITextField 的自定义子类
UITapGestureRecognizer 不适用于 UIView 动画
UITapGestureRecognizer 适用于 UIImageView 但不适用于 UILabel