Jef*_*eff 49 uibutton uiscrollview ios touchescancelled
我在我的应用程序中遇到了一个小问题.
我基本上有一系列UIButtons
添加为子视图,UIScrollView
它是一个笔尖的一部分.每次按下按钮,按钮突出显示之前都会有明显的延迟.在按钮变暗并显示选中之前,我基本上必须按住它约半秒钟.
我假设这是因为UIScrollView
需要确定触摸是否是滚动或者是否是针对子视图的触摸.
无论如何,我对如何进行有点不确定.我只想让按钮在点击它时立即显示.
任何帮助表示赞赏!
编辑:
我已经尝试设置delaysContentTouches
为NO但滚动变得几乎不可能,因为我的大部分scrollView都被填充UIButtons
.
jlo*_*g64 50
杰夫的解决方案并不适合我,但这个类似的解决方案:http://charlesharley.com/2013/programming/uibutton-in-uitableviewcell-has-no-highlight-state
除了覆盖touchesShouldCancelInContentView
滚动视图子类之外,您仍需要设置delaysContentTouches
为false
.最后,您需要返回true
而不是false
按钮.以下是上述链接的修改示例.正如评论者建议的那样,它会检查任何子类UIControl
而不是UIButton
特定的子类,以便此行为适用于任何类型的控件.
Objective-C的:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.delaysContentTouches = false;
}
return self;
}
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
if ([view isKindOfClass:UIControl.class]) {
return true;
}
return [super touchesShouldCancelInContentView:view];
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UIControl {
return true
}
return super.touchesShouldCancel(in: view)
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*eff 41
好的,我已经通过子类化UIScrollView
和覆盖解决了这个问题touchesShouldCancelInContentView
现在我的UIButton
那个被正确地标记为99个亮点,我的滚动视图正在滚动!
myCustomScrollView.h:
@interface myCustomScrollView : UIScrollView {
}
@end
Run Code Online (Sandbox Code Playgroud)
和myCustomScrollView.m:
@implementation myCustomScrollView
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
NSLog(@"touchesShouldCancelInContentView");
if (view.tag == 99)
return NO;
else
return YES;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21475 次 |
最近记录: |