使用某些按钮创建图层?

时间:2012-12-18 16:07:08

标签: ios uibutton calayer

我有一个UIImageView全屏,上面有按钮。按下这些按钮时,应打开一个包含按钮的较大图像。用于打开网站的按钮和用于关闭图像的另一个按钮。我想过使用一个图层来显示图像,但是即使按钮工作也没有任何结果。

  - (void)viewDidLoad
  {
       [super viewDidLoad];
        self.bigImageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SBS_visual_ok.jpg"]];
        self.bigImageView.frame = self.view.bounds;
        //....

        self.buttonInternational =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self.buttonInternational addTarget:self action:@selector(pressedInternational:) forControlEvents:UIControlEventTouchUpInside];
        self.buttonInternational.frame = CGRectMake (100, 100, 100, 100);
        //...

        [self.view addSubview:self.bigImageView];
        [self.bigImageView addSubview:self.buttonInternational];
  }

按下按钮我想用一些按钮拍照,但连图像都不出来。我已导入

  -(void)pressedInternational:(id)sender{

     CALayer *layer =[CALayer layer];
     layer.frame = CGRectMake(300, 400, 300, 200);
     layer.contents =(id)[UIImage imageNamed:@"International_Security_Service.png"].CGImage;

     [[self.bigImageView layer]addSublayer:layer]; 
     NSLog(@"The layer is %@", layer);
     //The layer is <CALayer: 0x757ba30>


   }

如何设置一层按钮?

1 个答案:

答案 0 :(得分:1)

您为什么要尝试使用CALayer?显示另一个UIView可能更容易,它可以有图像,按钮等等:

MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 300.0)];
[self.view addSubview:myView];

您可以轻松显示/隐藏

[self.myView setHidden:YES];

或推送新的视图控制器:

MyViewController *nextViewController = [[MyViewController alloc] init];
[self presentViewController:myViewController animated:YES completion:nil];

如有必要,您可以将动画应用于显示和删除其中任何一个。