gpt4 book ai didi

ios - 未调用 UIButton 函数

转载 作者:行者123 更新时间:2023-11-28 19:53:27 29 4
gpt4 key购买 nike

您好,在此先感谢您的支持:)

所以我正在做的是:1.创建一个 ScrollView 并将其添加到主视图2.然后向 ScrollView 添加另一个与 ScrollView 一样大的UIView;3. 在 UIView 中添加一个以编程方式创建的按钮,我还以编程方式添加:标签、图像、大小。

添加(育儿)对象的顺序:UIView->UIScrollView->UIView->UIButton

这是我使用的代码:

#import "ViewController.h"

@interface ViewController ()
{
UIButton *button;
UIView *buttonHolderView;
}
@end

@implementation ViewController

@synthesize scroll;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//scroll view is added using storyboards
[scroll setScrollEnabled:YES];
[scroll setContentSize:CGSizeMake(1840, 1752)];
scroll.bounces = NO;
[scroll setBackgroundColor:[UIColor blackColor]];
//UPDATE 4
//here changed to integer coordinates and now it works
buttonHolderView = [[UIView alloc] initWithFrame:CGRectMake( scroll.frame.origin.x, scroll.frame.origin.y, scroll.frame.size.width, scroll.frame.size.height)];
[buttonHolderView setBackgroundColor:[UIColor blueColor]];
[scroll addSubview:buttonHolderView];

NSString *title = @"";
int tagForTheButton = 0;

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.adjustsImageWhenHighlighted = NO;
button.frame = CGRectMake(xPossition, yPossition, 200, 64);
button.tag = tagForTheButton;
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed: ) forControlEvents:UIControlEventTouchUpInside];
[buttonHolderView addSubview:button];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"Tile.png"]] forState:UIControlStateNormal];
//other scroll and pinch gesture adjustments
//UPDATE WITH CODE FOR PINCH AND SCROLL


[scroll setUserInteractionEnabled:YES];
UIPinchGestureRecognizer *pitchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePitch:)];
[scroll addGestureRecognizer:pitchGestureRecognizer];

imageMaxScale = 1.1;
imageMinScale = 0.2;
imageCurrentScale = 1.0;



yMyCharacterFrame = 200;
xMyCharacterFrame = 200;
myCharacterFrame = [[UIImageView alloc]init];
myCharacterFrame.frame = CGRectMake(xMyCharacterFrame, yMyCharacterFrame, 100, 100);
myCharacterFrame.image = [UIImage imageNamed:@"image0.png"];
[myCharacterFrame setUserInteractionEnabled:YES];
[scroll addSubview:myCharacterFrame];
}

-(void)handlePitch:(UIPinchGestureRecognizer *) pinchGesture{

if (imageCurrentScale * [pinchGesture scale] > imageMinScale && imageCurrentScale * [pinchGesture scale] <imageMaxScale) {
imageCurrentScale = imageCurrentScale * [pinchGesture scale];
CGAffineTransform zoomTransform = CGAffineTransformMakeScale(imageCurrentScale, imageCurrentScale);
[[pinchGesture view] setTransform:zoomTransform];
}


[pinchGesture setScale:1.0];
CGRect scrollFrame;
scrollFrame.origin = _mainViewHolder.frame.origin;
scrollFrame.size = CGSizeMake(568, 320);
scroll.frame = scrollFrame;

}

-(void)buttonPressed:(UIButton *)sender{
NSLog(@"button pressed");
}

按钮绘制在屏幕上,但当我尝试点击它时,它没有进入 buttonPressed 函数(不是 NSLog)。

有什么建议吗?我也不明白 addTarget 指的是什么(我已经阅读了苹果文档)。

更新 2:如果我将按钮添加到 ScrollView ,不仅会显示它,而且我还可以点击它。

更新 3:我已经运行:po [滚动递归描述]。它返回:

UIScrollView: 0x7f95c9c7adf0; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7f95c9c7f5f0>; layer = <CALayer: 0x7f95c9c79820>; contentOffset: {0, 0}; contentSize: {1840, 1752}>
| <UIImageView: 0x7f95c9c7ed40; frame = (317.5 561; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7f95c9c7ec20>>
| <UIImageView: 0x7f95c9c7d2f0; frame = (313 565.5; 7 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7f95c9c7dda0>>
| <UIButton: 0x7f95c9d9f9c0; frame = (0 0; 320 568); opaque = NO; layer = <CALayer: 0x7f95c9d9e750>>
| | <UIButton: 0x7f95c9d9fea0; frame = (0 0; 200 64); opaque = NO; layer = <CALayer: 0x7f95c9d96540>>
| | | <UIImageView: 0x7f95c9cc4590; frame = (0 0; 200 64); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7f95c9cc4690>>

和:

(lldb) po [button recursiveDescription]
<UIButton: 0x7f95cd24e1f0; frame = (0 0; 200 64); opaque = NO; tag = 0; layer = <CALayer: 0x7f95cd24e100>>
| <UIImageView: 0x7f95cd251b70; frame = (0 0; 200 64); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7f95cd251c70>>

更新 4:更改为整数后,代码似乎可以正常工作: buttonHolderView = [[UIView alloc] initWithFrame:CGRectMake(scroll.frame.origin.x, scroll.frame.origin.y, scroll.frame.size.width, scroll.frame.size.height)];

感谢您的所有帮助,尤其是对 DoertyDoerk 的帮助谁向我展示了一些全新的东西。

最佳答案

您的按钮代码本身看起来不错。除了 xPossition 之外,yPossition 位编译得很好并且正在调用 buttonPressed:。我怀疑您的 View 层次结构存在问题,正如之前评论中所建议的那样。我会在你的 `viewDidLoad: 方法的末尾设置一个断点。然后在控制台的中断处键入以下命令并检查您的按钮是否被覆盖。

po [[UIWindow keyWindow] recursiveDescription]

您可能希望将结果复制并粘贴到文本编辑器中以便于阅读。

希望对您有所帮助! enter image description here

关于ios - 未调用 UIButton 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888281/

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