如何在Xcode中的CALayer上方制作按钮或标签? [英] How to make a button or label above a CALayer in Xcode?

查看:96
本文介绍了如何在Xcode中的CALayer上方制作按钮或标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




这是代码< br>



.h文件

  // CALayer声明
CALayer *图形;

.m文件

  //屏幕的宽度和高度
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

// CALayer定义
graphic = [CALayer层];
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];

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





预先感谢

解决方案

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



编辑



好的,我找出了问题所在。当您添加以下行时: [graphic addSublayer:self.btn.layer]; 您的应用程序崩溃是因为您的按钮已经添加到视图层次结构中,因为您正在使用



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



警告



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



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

  //这是* .m文件
#import ViewController.h

@interface ViewController()

//注释A.另一个标签和按钮已添加到类中而不添加它们到
//故事板
@property(非原子的,坚固的)UIButton * firstButton;
@property(非原子的,强的)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];

//屏幕的宽度和高度
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

// CALayer定义
CALayer * graphic = nil;
graphic = [CALayer层];
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];
//处理所有可以重新创建的资源。
}

-(IBAction)theAction:(id)sender {

NSLog(@ Button Tapped);
}
@end

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

希望这会有所帮助!


In my storyboard I added a button & a label. In my ViewController I programmatically defined a CALayer and added it as a sublayer to the ViewController's view. When I test the app the sublayer is above the button and the label but I want to make the sublayer below the button and the label.


here's the code

.h file

// The CALayer declaration
CALayer *graphic;

.m file

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

How can I make the sublayer (graphic) appear below the button and label?


Thanks in advance

解决方案

One thing you can do is to add your button and label programmatically to your layer.

EDIT

Ok, I figured out what the problem was. When you added this line: [graphic addSublayer:self.btn.layer]; your application was crashing because your button was already added to the view hierarchy because you were creating it using a Storyboard.

What I did was to declare a new label and button without adding it to the storyboard (see comment "A"). After that I instantiated them inside method viewDidLoad and then added them to the layer you created.

WARNING

This code I'm showing here will effectively display your label and button on top of the CALayer however keep in mind that CALayers are used for drawing and animation, not for user interaction. Unlike a UIView a CALayer doesn't inherit from UIResponder and for this reason it cannot receive the touches a UIView receives.

However, there's a workaround. Instead of using Target-Action mechanism you can use Gesture Recognizers to be able to detect touches and interaction from the user. In this example I added a simple UITapGestureRecognizer to illustrate how it's done. Every time you tap on the button a "Button Tapped" message will be displayed in the console.

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

Let me know if you have more questions.

Hope this helps!

这篇关于如何在Xcode中的CALayer上方制作按钮或标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆