gpt4 book ai didi

带有按钮的 iOS subview 不响应触摸。

转载 作者:行者123 更新时间:2023-11-28 18:42:32 24 4
gpt4 key购买 nike

我有一个 UIView 子类,它有一个带有按钮的框架/图像。我正在尝试将它添加到我的 View Controller 中,但是当我这样做时它会显示出来,但是没有一个按钮会记录任何触摸。

这是我的 UIView 子类。

    #import "ControlView.h"

@implementation ControlView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect slideFrame = CGRectMake(0, 402, 320, 82);
slider = [[UIView alloc] initWithFrame:slideFrame];
slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"shelf.png"]];
[self addSubview:slider];

UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];

btnImage = [UIImage imageNamed:@"NavBar_01.png"];
btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(12, 28, 70, 50);
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn1];
[slider bringSubviewToFront:btn1];
[btn1 addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];

btnImage = [UIImage imageNamed:@"NavBar_02.png"];
btnImageSelected = [UIImage imageNamed:@"NavBar_02_s.png"];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(87, 28, 70, 50);
[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn2];
[slider bringSubviewToFront:btn2];
// [btn2 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

btnImage = [UIImage imageNamed:@"NavBar_03.png"];
btnImageSelected = [UIImage imageNamed:@"NavBar_03_s.png"];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(162, 28, 70, 50);
[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn3];
[slider bringSubviewToFront:btn3];
// [btn3 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

btnImage = [UIImage imageNamed:@"NavBar_04.png"];
btnImageSelected = [UIImage imageNamed:@"NavBar_04_s.png"];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
btn4.frame = CGRectMake(237, 28, 70, 50);
[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn4];
[slider bringSubviewToFront:btn4];
// [btn4 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside];

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self addGestureRecognizer:recognizer];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:recognizer];

}
return self;
}

-(void)slideUp
{
// Slide up based on y axis
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 320;
slider.frame = frame;
[UIView commitAnimations];
}

-(void)slideDown
{
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 425;
slider.frame = frame;
[UIView commitAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

这就是我将它添加到我的 ViewController 的方式。

 ControlView *cont = [[ControlView alloc]init];
[homeView.view addSubview:cont];

我尝试将相同的代码添加到 ViewController 和按钮等。工作正常,所以我确信这对于 Delegates/Self 等来说是简单/愚蠢的。在 subview 和 ViewController 之间,但是我有一个心理障碍。

最佳答案

你在做这个吗?

 ControlView *cont = [[ControlView alloc]init]; 

你应该做的

ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];

关于带有按钮的 iOS subview 不响应触摸。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509671/

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