如何在Xcode中的CALayer上方制作按钮或标签?

时间:2013-10-18 18:24:00

标签: ios core-graphics

在我的故事板中,我添加了一个按钮&一个标签。在我的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];

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


提前致谢

1 个答案:

答案 0 :(得分:1)

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

修改

好的,我弄清楚问题是什么。当您添加此行时:[graphic addSublayer:self.btn.layer];您的应用程序崩溃了,因为您的按钮已添加到视图层次结构中,因为您是使用故事板创建的。

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

警告

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

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

// 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

如果您有更多问题,请与我们联系。

希望这有帮助!