gpt4 book ai didi

iphone - 从自定义 UIControl 子类转发事件

转载 作者:行者123 更新时间:2023-12-03 19:11:04 27 4
gpt4 key购买 nike

我的自定义子类扩展 UIControl (EditableImageView)基本上它包含 3 个 subview :

  • UIButton(UIButtonTypeCustom)
  • UIImageView
  • UI标签

所有类都运行良好,但现在我想将从该类生成的一些事件连接到另一个类。特别是当按下按钮时,我想将事件转发给所有者 View Controller ,以便我可以处理该事件。问题是我不知道如何实现这种行为。在 EditableImageView 中,我可以使用 [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] 捕获触摸事件,但我不知道如何在 buttonPressed 选择器内部转发它。我也尝试实现touchesBegan,但似乎从未被调用过...

我想以这种方式从 View Controller 捕获按钮按下事件:

- (void)viewDidLoad {
[super viewDidLoad];

self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)];
[imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imageButton];
[imageButton setEditing:NO];

}

这是我的 UIControl 子类初始化方法:

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
[button setImage:[UIImage imageNamed:@"nene_70x70.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];

transparentLabelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"editLabelBackground.png"]];
transparentLabelBackground.hidden = YES;
[self addSubview:transparentLabelBackground];

// create edit status label
editLabel = [[UILabel alloc] initWithFrame:CGRectZero];
editLabel.hidden = YES;
editLabel.userInteractionEnabled = NO; // without this assignment the button will not be clickable
editLabel.textColor = [UIColor whiteColor];
editLabel.backgroundColor = [UIColor clearColor];
editLabel.textAlignment = UITextAlignmentLeft;
UIFont *labelFont = [UIFont systemFontOfSize:16.0];
editLabel.font = labelFont;
editLabel.text = @"edit";
labelSize = [@"edit" sizeWithFont:labelFont];

[self addSubview:editLabel];
}

return self;
}

谢谢。

最佳答案

我是这样解决的:

EditableImageView.h:

@interface EditableImageView : UIControl {
UIButton *button;
UIImageView *transparentLabelBackground;
UILabel *editLabel;
CGSize labelSize;

BOOL editing;
}

@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *editLabel;
@property (nonatomic, retain) UIImageView *transparentLabelBackground;
@property (nonatomic, getter=isEditing) BOOL editing;

- (void)buttonPressed:(id)sender;
@end

EditableImageView.m:

 (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:[UIColor clearColor]];

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
[button setImage:[UIImage imageNamed:@"nene_70x70.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
.......
}
}


- (void)buttonPressed:(id)sender {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}

MyController.m:

- (void)viewDidLoad {
[super viewDidLoad];
self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)];
[imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imageButton];
}

- (void)buttonPressed:(id)sender {
NSLog(@"buttonPressed!");
}

关于iphone - 从自定义 UIControl 子类转发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2893777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com