我尝试了很多东西,但仍然没有结果.
所以我在UIViewController的子类中以编程方式创建了以下按钮:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0.0, 0.0, 110.0, 40.0);
rightButton.titleLabel.font = [UIFont fontWithName:GAME_FONT_NAME_STRING size:20.0];
[rightButton setTitle:@"MyTitle" forState:UIControlStateNormal];
rightButton.backgroundColor = [UIColor clearColor];
[rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
Run Code Online (Sandbox Code Playgroud)
选择器在哪里:
- (void)myButton;
Run Code Online (Sandbox Code Playgroud)
我尝试了一切:
- (void)myButton;
- (void)myButton:(id)sender;
- (void)myButton:(id)sender forEvent:(UIEvent *)event;
- (IBAction)myButton;
- (IBAction)myButton:(id)sender;
- (IBAction)myButton:(id)sender forEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)
和相应的选择器,当然:
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton) …Run Code Online (Sandbox Code Playgroud) 对于UISegmentedControl,一旦出现弹出窗口或警报,控件变暗(灰色(去色调色调))
我正在构建自己的UIControl子类,它使用UILabel作为子视图
我想暗淡(去饱和)UILabel的文本颜色,与UISegmentedControl或(UIButton ......)相同
是否可以扩展结构 Reactive,其中基类是我从 UIControl 继承的自定义控件?
当我尝试以下代码时:
extension Reactive where Base: CustomControl {
public var value: ControlProperty<CGFloat> {
return CustomControl.rx.value(
self.base,
getter: { customControl in
customControl.customProperty
},
setter: { customControl, value in
customControl.customProperty = value
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Instance member "value" cannot be used on type 'Reactive<CustomControl>'
Run Code Online (Sandbox Code Playgroud)
如果您向我提供任何解释,我将不胜感激。
我目前有一个UIControl,它有许多子视图(图像,标签).
不幸的是,当我使用addTarget等时,它不会检测子视图上的触摸.
[myCustomView addTarget:self action:@selector(touchedView:)
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
UIControl是否可以检测子视图上的触摸,或者我应该以不同方式接近它.
我的自定义子类扩展UIControl(EditableImageView)基本上它包含3个子视图:
所有类都很好但现在我想将从这个类生成的一些事件连接到另一个.特别是当按下按钮时,我想将事件转发给所有者viewcontroller,以便我可以处理该事件.问题是我无法弄清楚如何实现这种行为.在EditableImageView中,我可以使用[button addTarget:self action:@selector(buttonPressed :) forControlEvents:UIControlEventTouchUpInside]来捕捉触摸事件,但我不知道如何在buttonPressed选择器内转发它.我也试图实现touchesBegan,但它似乎从未被称为...
我想以这种方式从viewcontroller捕获按钮按下事件:
- (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];
Run Code Online (Sandbox Code Playgroud)
}
这是我的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 …Run Code Online (Sandbox Code Playgroud) 我将UIControl子类化为包含不同标准控件的自定义控件.
对于此讨论,我们假设我的自定义UIControl仅包含UIButton.
我想要实现的是,单击自定义UIControl中的任何位置都会为该自定义UIControl生成单击事件.标准行为是UIButton将处理和使用(即不转发)click事件.
由于不鼓励对UIButton进行子类化,我无法真正找到实现这一目标的简单方法.
有什么建议?
我有一个UIControl子类.我使用以下代码更改它的背景颜色:
- (void)drawRect:(CGRect)rect
{
[self.backgroundColor setFill];
UIRectFill([self bounds]);
}
Run Code Online (Sandbox Code Playgroud)
这适用于所有颜色,除了[UIColor clearColor].如何制作UIControl透明背景?
我正在尝试使用以下代码将 UI 按钮设为切换按钮:
- (void)tapButton:(id)sender{
UIButton *button = sender;
if (!button.selected){
[self performSelector:@selector(highlight:) withObject:button afterDelay:0.0];
}else{
[self performSelector:@selector(removeHighlight:) withObject:button afterDelay:0.0];
}
}
- (void)highlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = YES;
}
- (void)removeHighlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = NO;
}
Run Code Online (Sandbox Code Playgroud)
但是,当按钮处于选定模式时,我遇到了奇怪的现象。
未选中的:

已选择:

在为UIButton定义回调时,我为同一个动作列出了几个事件
在目标中我希望能够区分触发回调的事件
[button addTarget:self action:@selector(callback:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchCancel];
-(void)callback:(UIButton *)button
{
// need to be able to distinguish between the events
if (event == canceled)
{
}
if (event == touchDown)
{
}
... etc
}
Run Code Online (Sandbox Code Playgroud) 当我在以新的 iOS13 自动样式模态呈现的 ViewController 中嵌入自定义 UIControltouchesCancelled时,只要平移手势移动超过几个点就会调用。
本地人UIKit UISlider不会这样做。您可以毫无问题地在automatic样式模态 ViewController 中平移 UISlider 。
UIScrollView 具有touchesShouldCancel(in view: UIView)您可以强制它允许在指定视图中进行触摸的地方,但我在文档中找不到这种新样式的模态演示文稿的任何内容。