【问题标题】:How to make a button or label above a CALayer in Xcode?如何在 Xcode 中的 CALayer 上方制作按钮或标签?
【发布时间】:2013-10-18 18:23:59
【问题描述】:

在我的故事板中,我添加了一个按钮和一个标签。在我的 ViewController 中,我以编程方式定义了一个 CALayer 并将其作为子层添加到 ViewController 的视图中。当我测试应用程序时,子层位于按钮和标签上方,但我想将子层置于按钮和标签下方。


这是代码

.h 文件

// The CALayer declaration
CALayer *graphic;

.m 文件

//The screen width and height
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

//The CALayer definition
graphic = [CALayer layer];
graphic.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
graphic.position = CGPointMake(screenHeight/2, screenWidth/2);
graphic.backgroundColor = [UIColor blackColor].CGColor;
graphic.opacity = 0.1f;
[self.view.layer addSublayer:graphic];

如何让子图层(图形)出现在按钮和标签下方?


提前致谢

【问题讨论】:

  • 仅使用xcode 标签来回答有关 IDE 本身的问题。谢谢!
  • [ self.view.layer insertSublayer:graphic below:self.button.layer ]

标签: ios core-graphics


【解决方案1】:

您可以做的一件事是以编程方式将按钮和标签添加到图层。

编辑

好的,我发现了问题所在。当您添加此行时:[graphic addSublayer:self.btn.layer]; 您的应用程序崩溃了,因为您的按钮已经添加到视图层次结构中,因为您使用情节提要创建它。

我所做的是声明一个新标签和按钮,而不将其添加到情节提要中(参见注释“A”)。之后,我在方法 viewDidLoad 中实例化了它们,然后将它们添加到您创建的层中。

警告

我在这里展示的这段代码将有效地在CALayer 顶部显示您的标签和按钮,但请记住CALayers 用于绘图和动画,而不是用于用户交互。与UIView 不同,CALayer 不继承自UIResponder,因此它无法接收UIView 接收的触摸。

但是,有一个解决方法。您可以使用手势识别器来检测用户的触摸和交互,而不是使用 Target-Action 机制。在这个例子中,我添加了一个简单的UITapGestureRecognizer 来说明它是如何完成的。每次点击按钮时,控制台中都会显示“Button Tapped”消息。

// This is the *.m file
#import "ViewController.h"

@interface ViewController ()

// Comment A. Another label and button added to the class without adding them to the
// storyboard
@property (nonatomic, strong) UIButton *firstButton;
@property (nonatomic, strong) UILabel *mylabel;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.firstButton setTitle:@"Just a Button" forState:UIControlStateNormal];
    self.firstButton.frame = CGRectMake(50, 50, 300, 40);
    UIGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(theAction:)];
    [self.firstButton addGestureRecognizer:tapGesture];

    self.mylabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 200, 40)];
    self.mylabel.text = @"Hello World";
    self.mylabel.backgroundColor = [UIColor cyanColor];

    //The screen width and height
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;

    //The CALayer definition
    CALayer *graphic = nil;
    graphic = [CALayer layer];
    graphic.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
    graphic.position = CGPointMake(screenHeight/2, screenWidth/2);
    graphic.backgroundColor = [UIColor whiteColor].CGColor;
    graphic.opacity = 1.0f;
    [graphic addSublayer:self.firstButton.layer];
    [graphic addSublayer:self.mylabel.layer];
    [self.view.layer addSublayer:graphic];
}

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

- (IBAction)theAction:(id)sender {

    NSLog(@"Button Tapped");
}
@end

如果您还有其他问题,请告诉我。

希望这会有所帮助!

【讨论】:

  • 对不起,但这不起作用。我将 .button 替换为 ->button 应用已暂停
  • 你是如何声明你的按钮的?你能展示你的整个班级吗?
  • 全班是什么意思?
  • 我的意思是你的 *.h 和 *.m 文件中的内容
  • 因为UIViews 处理用户交互而CALayers 不处理。如果您查看这两个类的类层次结构,您会发现UIView 继承自UIResponder,但CALayer 没有。这就是为什么它无法接收用户的任何操作。解决方法是将UIGestureRecognizer 附加到它上面,这样就可以解决问题。就像我给你的例子一样。您可能还想看看这个链接:raptureinvenice.com/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 2013-01-12
  • 2018-04-02
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多