zoukankan      html  css  js  c++  java
  • 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
     
     
     
     
  • 相关阅读:
    C语言进阶—— 单引号和双引号14
    C语言进阶——注释符号12
    C语言进阶——enum, sizeof, typedef 分析11
    算法01
    vim+软件安装——06
    if(xx)和(a==b) 关于数据类型的转换
    浏览器的渲染机制,白屏和FOUC
    BFC的概念和解决外边距合并
    CSS有哪几种引入方式
    块级元素和行内元素的区别,常见的块级元素和行内元素有哪些
  • 原文地址:https://www.cnblogs.com/guiyangxueyuan/p/5260013.html
Copyright © 2011-2022 走看看