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
     
     
     
     
  • 相关阅读:
    【深入学习MySQL】MySQL的索引结构为什么使用B+树?
    【Python爬虫】爬了七天七夜,终于爬出了博客园粉丝数排行榜!
    【BAT面试题系列】面试官:你了解乐观锁和悲观锁吗?
    深入学习MySQL事务:ACID特性的实现原理
    深入学习Redis(5):集群
    深入学习Redis(4):哨兵
    谈谈微信支付曝出的漏洞
    深入学习Redis(3):主从复制
    深入学习Redis(2):持久化
    Spring中获取request的几种方法,及其线程安全性分析
  • 原文地址:https://www.cnblogs.com/guiyangxueyuan/p/5260013.html
Copyright © 2011-2022 走看看