iOS中用按钮NSbutton实现视图的放大与缩小功能

在.h文件中
首先声明需要的属性;
 
 
在.m文件
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     创建控制放大的按钮
    按钮类型
              self.aButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
   按钮的frame
            self.aButton.frame=CGRectMake(150, 500, 100, 100);
     按钮的背景图
    [self.aButton setBackgroundImage:[UIImage imageNamed:@"print1.png"] forState:UIControlStateNormal];

    。。。。。调用放大方法
    [self.aButton addTarget:self action:@selector(testBig) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.aButton];

    //控制缩小的按钮
    self.bButton=[UIButton buttonWithType:UIButtonTypeInfoLight];
    self.bButton.frame=CGRectMake(200, 500, 100, 100);
    [self.bButton setBackgroundImage:[UIImage imageNamed:@"print1.png"] forState:UIControlStateNormal];
    //    [self.Button setTitle:@"放大" forState:UIControlStateNormal];
    self.bButton.titleLabel.font=[UIFont systemFontOfSize:40 weight:30];
    [self.bButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //调用缩小方法
    [self.bButton addTarget:self action:@selector(testSmall) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.bButton];
   
   
    self.aView=[[UIView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    self.aView.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.aView];
    self.bView=[[UIView alloc]initWithFrame:CGRectMake(125, 225, 150, 150)];

    [self.view addSubview:self.bView];
 
    self.cView=[[UIView alloc]initWithFrame:CGRectMake(150, 250, 100, 100)];
    self.cView.backgroundColor=[UIColor blueColor];
    [self.view addSubview:self.cView];
   
}
//缩小方法的实现
-(void)testSmall
{
    //缩小
    if (self.aView.frame.size.width!=1&&self.bView.frame.size.width!=1&&self.aView.frame.size.width!=1) {
       
        self.aView.frame=CGRectInset(self.aView.frame, 10, 10);
        self.bView.frame=CGRectInset(self.bView.frame, 10, 10);
        self.cView.frame=CGRectInset(self.cView.frame, 10, 10);

    }
}
//放大方法的实现
-(void)testBig
{
    if (self.aView.frame.size.width!=self.view.frame.size.width&&self.bView.frame.size.width!=self.view.frame.size.width&&self.aView.frame.size.width!=self.view.frame.size.width)
    //放大
    { self.aView.frame=CGRectInset(self.aView.frame, -10, -10)
    self.bView.frame=CGRectInset(self.bView.frame, -10, -10);
    self.cView.frame=CGRectInset(self.cView.frame, -10, -10);
    }
}

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

@end
 
 
 
 
posted @ 2016-03-09 22:26  右手指尖轻轻触  阅读(797)  评论(0编辑  收藏  举报