Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-延迟“;“着陆”;UITableViewCell中UIButton的事件_Ios_Objective C_Uitableview_Uibutton - Fatal编程技术网

iOS-延迟“;“着陆”;UITableViewCell中UIButton的事件

iOS-延迟“;“着陆”;UITableViewCell中UIButton的事件,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我有一个自定义UITableViewCell,它由以下内容初始化: - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { NSArray *nibArray = [[NSBu

我有一个自定义UITableViewCell,它由以下内容初始化:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        self = [nibArray objectAtIndex:0];

        [self setSelectionStyle:UITableViewCellSelectionStyleNone];

        [self.downButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
        [self.downButton setBackgroundImage:[UIImage imageNamed:@"buttonSelected"] forState:UIControlStateHighlighted];
    }
    return self;
}
按钮正确显示,并带有相应的背景图像,但按下/单击按钮时,高亮显示的图像不会立即显示。相反,在改变发生之前,你必须按住它一两秒钟。另一方面,释放按钮确实会立即变回原始背景图像

在切换到高亮显示的图像时,为了减轻延迟的变化,我采用以下方法进行了更改:

- (IBAction)downDown:(id)sender {
    [self.downButton setBackgroundColor:[UIColor redColor]];
}
上面的方法设置为“触地”(与更常见的“内部触地”相反),我已删除突出显示状态的
setBackgroundImage:forState:
。与上述问题相同。颜色最终会变为红色,但只有在点击并按住按钮一两秒钟后才会变为红色

我有一个按钮的方法,当“内部润色”发生时调用该方法,并且该方法执行时不会出现问题-无论我是快速点击按钮,还是在释放之前单击并按住按钮一段时间

那么为什么要延迟“触地”或
uicontrol状态突出显示
?我试图向用户提供即时反馈,以显示按钮已按下


如果需要,我可以提供更多的代码,但这些是唯一与背景外观有关的位。

这是由
UIScrollView
属性
delaysContentTouches
造成的

对于
UITableView
本身,只需将该属性设置为
NO
就足够了,但这仅适用于未封装在另一个
UIScrollView
中的表的子视图

UITableViewCells
在iOS 7中包含一个内部滚动视图,因此您需要在单元格级别上为所有带有按钮的单元格更改此属性的值

以下是您需要做的:

1.在
viewDidLoad
或类似的地方,一旦您的
UITableView
被初始化,将其放入:

self.tableView.delaysContentTouches = NO;
2.对于iOS 7支持,在您的
UITableViewCell
initWithStyle:reuseIdentifier:
initWithCoder:
用于NIB)的初始化方法中,将其放在末尾:

for (UIView *currentView in self.subviews)
{
    if([currentView isKindOfClass:[UIScrollView class]])
    {
        ((UIScrollView *)currentView).delaysContentTouches = NO;
        break;
    }
}

不幸的是,这不是一个100%永久性的解决方案,因为苹果可以在将来再次更改单元格内的视图层次结构(可能将滚动视图向下移动到另一层,或者需要在其中嵌套另一个循环),但直到它们以某种方式将类或至少属性呈现给开发人员,这是我们最好的答案。

也许这个答案更简单。只需添加一个动画延迟。如下图所示:

    [self.publishButton bk_addEventHandler:^(id sender) {
    @strongify(self);
    // reset to normal state with delay
    [UIView animateWithDuration:0.1 animations:^{
        self.publishButton.backgroundColor = [UIColor whiteColor];
    }];
    } forControlEvents:UIControlEventTouchUpInside];

   [self.publishButton bk_addEventHandler:^(id sender) {
    //Set your Image or color
       self.publishButton.backgroundColor = [UIColor redColor];
   } forControlEvents:UIControlEventTouchDown];

Swift

首先,在成功加载后关闭
UITableView
中的延迟,例如,在
viewdiload()方法中:

someTableView.delaysContentTouches = false
然后关闭
UITableView
中包含的滚动视图中的延迟:

for case let scrollView as UIScrollView in someTableView.subviews {
    scrollView.delaysContentTouches = false
}

iOS7注意:您可能必须禁用
UITableViewCell
中的延迟。(检查迪玛的答案)。您还可以找到一些其他提示。

我在代码中通过对UIButton子类化来解决这个问题

目标C

@interface TBButton : UIButton

@end

@implementation TBButton

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.highlighted = true;
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.highlighted = false;
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.highlighted = false;
    [super touchesCancelled:touches withEvent:event];
}

@end
@接口TBButton:UIButton
@结束
@实现按钮
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
self.highlighted=true;
[超级触摸开始:触摸事件:事件];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
self.highlighted=false;
[超级触控:触控事件:事件];
}
-(无效)触控取消:(NSSet*)触控事件:(UIEvent*)事件{
self.highlighted=false;
[超级触控取消:触控事件:事件];
}
@结束
迅捷的

class-TBButton:ui按钮{
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
突出显示=真
super.touchsbegind(touchs,withEvent:event)
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
突出显示=错误
super.touchesend(touchs,withEvent:event)
}
覆盖功能触摸已取消(触摸:设置?,带事件:UIEvent?){
突出显示=错误
super.touchscancelled(touchs,withEvent:event)
}
}
斯威夫特3

class TBButton: UIButton {
    override func touchesBegan(_ touches: Set<UITouch>, with event:     UIEvent?) {
        isHighlighted = true
        super.touchesBegan(touches, with: event)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event:     UIEvent?) {
        isHighlighted = false
        super.touchesEnded(touches, with: event)
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesCancelled(touches, with: event)
    }
}
class-TBButton:ui按钮{
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
isHighlighted=true
super.touchesbeated(touches,with:event)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
isHighlighted=错误
super.touchesend(触摸,带有:事件)
}
覆盖功能触摸已取消(touchs:Set,带有事件:UIEvent?){
isHighlighted=错误
super.touchscancelled(触摸,带有:事件)
}
}

这里有一个递归解决方案,您可以添加到视图控制器中:

+ (void)disableDelayedTouches:(UIView*)view
{
    for(UIView *currentView in view.subviews) {
        if([currentView isKindOfClass:[UIScrollView class]]) {
            ((UIScrollView *)currentView).delaysContentTouches = NO;
        }
        [self disableDelayedTouches:currentView];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.class disableDelayedTouches:self.view];
}
Swift 3版本的:

override func touchsbegind(touch:Set,带有事件:UIEvent?){
isHighlighted=true
super.touchesbeated(touches,with:event)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
isHighlighted=错误
super.touchesend(触摸,带有:事件)
}
覆盖功能触摸已取消(touchs:Set,带有事件:UIEvent?){
isHighlighted=错误
super.touchscancelled(触摸,带有:事件)
}
那么为什么要延迟“触地”或
uicontrol状态突出显示
?我试图向用户提供即时反馈,以显示按钮已按下

苹果还没有解释吗

立即:
  • 如果要滚动视图,且手指触摸任何可高亮显示的内容(例如,具有默认选择样式的任何表格单元格),则会出现以下闪烁瑕疵:

    真的吗,他们不知道是什么原因
    + (void)disableDelayedTouches:(UIView*)view
    {
        for(UIView *currentView in view.subviews) {
            if([currentView isKindOfClass:[UIScrollView class]]) {
                ((UIScrollView *)currentView).delaysContentTouches = NO;
            }
            [self disableDelayedTouches:currentView];
        }
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.class disableDelayedTouches:self.view];
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = true
        super.touchesBegan(touches, with: event)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesEnded(touches, with: event)
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesCancelled(touches, with: event)
    }