我一直在努力做到这一点:向简单的自定义视图添加动作。我查看了互联网,找到了两个“简单”的解决方案:
UITapGestureRecognizer
UIButton
我想以编程方式执行此操作,我只需要处理点按。
到目前为止,这是我的代码,我已经分别尝试了两种解决方案,但它不起作用!
的.m
#import "AreaView.h"
@implementation AreaView
#define GREY 27.0/255.0
#define PINK_R 252.0/255.0
#define PINK_G 47.0/255.0
#define PINK_B 99.0/255.0
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity
{
self = [self initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:GREY green:GREY blue:GREY alpha:1];
self.userInteractionEnabled=YES;
//Init variables
_areaName=areaName;
_capacity=capacity;
_minimumSpending=minimumSpending;
//Image view
_logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 66, 50)];
//_logoImageView.image = [UIImage imageNamed:imageName];
_logoImageView.backgroundColor = [UIColor grayColor];
//Label
_areaNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 54, 76, 18)];
_areaNameLabel.textAlignment = NSTextAlignmentCenter;
_areaNameLabel.textColor = [UIColor whiteColor];
_areaNameLabel.font = [UIFont systemFontOfSize:12.0];
_areaNameLabel.text = areaName;
//button
_button = [[UIButton alloc]initWithFrame:self.bounds];
_button.userInteractionEnabled=YES;
_button.backgroundColor=[UIColor yellowColor];
[_button addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
//tap gesture racognizer
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self addGestureRecognizer:tapRecognizer];
[self addSubview:_logoImageView];
[self addSubview:_areaNameLabel];
[self addSubview:_button];
}
return self;
}
-(void)handleTap:(UIButton *)button
{
NSLog(@"tapped!");
}
-(void)tapped:(UITapGestureRecognizer *)recognizer
{
NSLog(@"tapped!");
}
@end
·H
#import <UIKit/UIKit.h>
@interface AreaView : UIView <UIGestureRecognizerDelegate>
@property (nonatomic) UIImageView *logoImageView;
@property (nonatomic) UILabel *areaNameLabel;
@property (nonatomic) NSString *areaName;
@property (nonatomic) int minimumSpending;
@property (nonatomic) int capacity;
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity;
@property (nonatomic, strong) UIButton *button;
@end
感谢您的帮助!
修改
问题是handleTap和tapped都不会被触发,即使我评论按钮解决方案或点击手势一个单独测试它们。对于按钮实现,我可以在我的界面上看到它,但点击它什么都不做。
然后在UIScrollview中以编程方式多次添加我的UIView(对于多个视图)。
编辑2
问题比这更复杂。自定义视图位于另一个不同的自定义视图内的scrollview内,其主要功能是重写hittest,因此滚动视图会保留此视图的触摸。 (Here is the purpose of all that)
似乎只要涉及到hittest,它就不起作用。
答案 0 :(得分:0)
实施此委托方法,这将对您有所帮助。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
id touchedView = gestureRecognizer.view;
if ([touchedView isKindOfClass:[UIButton class]])
{
return NO; //It won't invoke gesture method, But it'll fire button method.
}
return YES;
}
答案 1 :(得分:0)
我不确定为什么你的UIButton
和UITapGestureRecognizer
选择器没有被解雇。但是,处理视图上的点击的另一个选项是简单地覆盖touchesEnded:withEvent
方法:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//handle a tap
}
这样,您就不必创建UIButton
或UITapGestureRecognizer
对象了。