gpt4 book ai didi

ios - 自定义 UIControl 和手势识别器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:36 25 4
gpt4 key购买 nike

我正在尝试创建一个类似于 slider 的自定义 UIControl。

此控件是一个 View 的 subview ,该 View 还附加了一个点击手势识别器。

现在的问题是这个点击手势识别器取消了发送到我的控件的触摸。有没有一种方法可以从我的控件代码中覆盖它?

如果我查看 iOS 中的标准控件,它看起来好像 UIButton 有一种方法可以覆盖点击手势识别器,但 UISlider 没有。因此,如果我用 UIButton 替换我的自定义控件,点击手势识别器不会触发它的 Action ,但如果我用 slider 替换它,它会触发。

编辑:我在 Xcode 中做了一个小项目来玩。在这里下载 https://dl.dropboxusercontent.com/u/165243/TouchConcept.zip并尝试改变它,这样

  • UICustomControl 不知道点击手势识别器
  • 当用户点击黄色框时,UICustomControl 不会被取消
  • UICustomControl 没有继承自 UIButton(这是一种感觉不对的解决方案,以后可能会让我更头疼)

代码:

// inherit from UIButton will give the wanted behavior, inherit from UIView (or UIControl) gives
// touchesCancelled by the gesture recognizer
@interface UICustomControl : UIView

@end

@implementation UICustomControl

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ NSLog(@"touchesBegan"); }

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{ NSLog(@"touchesMoved"); }

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ NSLog(@"touchesEnded"); }

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{ NSLog(@"touchesCancelled"); }

@end
@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad
{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logTap:)];
[self.view addGestureRecognizer:tapRecognizer];
UIView *interceptingView = [[UICustomControl alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
interceptingView.userInteractionEnabled = YES;
interceptingView.backgroundColor = [UIColor yellowColor];
[self.view addSubview: interceptingView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void) logTap: (id) sender
{
NSLog(@"gesture recognizer fired");
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

您可以使用“取消 View 中的触摸”属性将手势识别器配置为取消它附加的 View 中的触摸:

myGestureRecognizer.cancelsTouchesInView = NO;

关于ios - 自定义 UIControl 和手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18576744/

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