绘图 - 9

来源:互联网 发布:闲杂软件 编辑:程序博客网 时间:2024/05/17 06:04
- (void)bringSubviewToFront:(UIView *)view;- (void)sendSubviewToBack:(UIView *)view;

接触到两个UIView方法,看文档的意思就是将视图放在“前后”或者是“后面”。避免了被遮挡的情况。

#import "LBDrawing2Controller.h"@interface LBDrawing2Controller ()@property (nonatomic, strong) UIView* blockedView;@property (nonatomic, strong) UIView* blockingView;@end@implementation LBDrawing2Controller- (void)viewDidLoad{    [super viewDidLoad];        [self.view setBackgroundColor:[UIColor redColor]];        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];    [button setFrame:CGRectMake(0, 0, 50, 50)];    [button setCenter:self.view.center];    [button setTitle:@"改" forState:UIControlStateNormal];    [button setBackgroundColor:[UIColor blackColor]];    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];        UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];    [button1 setFrame:CGRectMake(0, 0, 50, 50)];    [button1 setCenter:CGPointMake(self.view.center.x, button.frame.origin.y + 80)];    [button1 setTitle:@"改" forState:UIControlStateNormal];    [button1 setBackgroundColor:[UIColor blueColor]];    [button1 addTarget:self action:@selector(button1Clicked:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button1];        self.blockedView = [[UIView alloc] init];    [self.blockedView setFrame:CGRectMake(0, 0, 80, 80)];    [self.blockedView setBackgroundColor:[UIColor yellowColor]];    [self.view addSubview:self.blockedView];        self.blockingView = [[UIView alloc] init];    [self.blockingView setFrame:CGRectMake(0, 0, 100, 100)];    [self.blockingView setBackgroundColor:[UIColor greenColor]];    [self.view addSubview:self.blockingView];    }- (void)buttonClicked:(id)sender{    UIButton* button = (UIButton*)sender;    button.selected = !button.isSelected;        if (button.isSelected)    {        [self.view sendSubviewToBack:self.blockingView];    }    else    {        [self.view sendSubviewToBack:self.blockedView];    }}- (void)button1Clicked:(id)sender{    UIButton* button = (UIButton*)sender;    button.selected = !button.isSelected;        if (button.isSelected)    {        [self.view bringSubviewToFront:self.blockedView];            }    else    {        [self.view bringSubviewToFront:self.blockingView];    }}

效果:




0 0
原创粉丝点击