未调用 UIButton 函数
UIButton function is not called
您好,在此先感谢您的支持:)
所以我正在做的是:
1.创建一个滚动视图并将其添加到主视图
2.然后向滚动视图添加另一个与滚动视图一样大的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:
如果我将按钮添加到滚动视图,不仅会显示它,而且我还可以点击它。
更新 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 向我展示了一些全新的东西。
也许试试这个:
[buttonHolderView bringSubviewToFront:button];
您的按钮代码本身看起来不错。除了 xPossition 之外,yPossition 位编译得很好并且正在调用 buttonPressed:
。我怀疑您的视图层次结构存在问题,正如之前评论中所建议的那样。我会在你的 `viewDidLoad: 方法的末尾设置一个断点。然后在控制台中输入以下命令并检查你的按钮是否被覆盖。
po [[UIWindow keyWindow] recursiveDescription]
您可能希望将结果复制并粘贴到文本编辑器中以便于阅读。
希望对您有所帮助!
您好,在此先感谢您的支持:)
所以我正在做的是: 1.创建一个滚动视图并将其添加到主视图 2.然后向滚动视图添加另一个与滚动视图一样大的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: 如果我将按钮添加到滚动视图,不仅会显示它,而且我还可以点击它。
更新 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 向我展示了一些全新的东西。
也许试试这个:
[buttonHolderView bringSubviewToFront:button];
您的按钮代码本身看起来不错。除了 xPossition 之外,yPossition 位编译得很好并且正在调用 buttonPressed:
。我怀疑您的视图层次结构存在问题,正如之前评论中所建议的那样。我会在你的 `viewDidLoad: 方法的末尾设置一个断点。然后在控制台中输入以下命令并检查你的按钮是否被覆盖。
po [[UIWindow keyWindow] recursiveDescription]
您可能希望将结果复制并粘贴到文本编辑器中以便于阅读。
希望对您有所帮助!