zoukankan      html  css  js  c++  java
  • IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView

    UIButton

    1 //1.设置UIButton 的左右移动
    2  .center属性 获得 CGPoint 来修改x y
    3 //1.设置UIButton 的放大缩小
    4   bounds属性 获得CGRect 然后通过size.height设置高 wight设置宽
    //3.或者使用frame 来设置空间的 移动以及大小

    代码创建一个UIButton

     1     // 1.创建一个按钮
     2     UIButton *btn = [[UIButton alloc] init];
     3     
     4     // 2.添加按钮
     5     [self.view addSubview:btn];
     6     
     7     // 3.设置按钮的frame
     8     btn.frame = CGRectMake(100, 100, 100, 100);
     9     
    10     // 4.给按钮的默认状态和高亮状态设置背景图片
    11     [btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
    12     [btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];
    13     
    14     // 5.给按钮的默认状态和高亮状态分别设置文字和文字的颜色
    15     [btn setTitle:@"点我啊" forState:UIControlStateNormal];
    16     [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    17     
    18     [btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
    19     [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    20 
    21     // 6.给按钮添加一个点击事件,监控按钮的点击
    22     [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    23 
    24 
    25 
    26 - (void)btnClick:(UIButton *)btn
    27 {
    28     NSLog(@"btnClick");
    29 }

    简易动画

     1 //简易动画的创建有两种方式
     2 //1.头尾式
     3     [UIView beginAnimations : nil context:nil];//开启动画
     4     [UIView setAnimationDuration:1];//设置动画执行时间
     5             //这里写入需要执行动画的代码
     6     [UIView commitAnimations];//提交动画
     7 
     8 //2.Block式
     9     [UIView animateWithDuration: 0.5 animations:^{
    10 
    11            //这里写入一个需要执行的动画代码
    12     }];

     transform

     1 //利用transform 可以修改空间的位移(位置)、缩放、旋转
     2 
     3 //创建一个transform属性
     4 CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,  CGFloat ty) ;
     5 CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);
     6 CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
     7 (注意:angle是弧度制,并不是角度制)
     8 
     9 //在某个transform的基础上进行叠加
    10 CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty);
    11 CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);
    12 CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);
    13 
    14 //清空之前设置的transform属性
    15 view.transform = CGAffineTransformIdentity;
    16 
    17 例如:
    18 
    19 @interface ViewController ()
    20 @property (weak, nonatomic) IBOutlet UIButton *btnIcon;
    21 // 移动
    22 - (IBAction)move;
    23 
    24 // 旋转
    25 - (IBAction)rotate;
    26 
    27 // 缩放
    28 - (IBAction)scale;
    29 - (IBAction)goBack:(id)sender;
    30 
    31 @end
    32 
    33 - (IBAction)move {
    34     
    35     // 2. 修改结构体值
    36     // 下面这句话的意思是:告诉控件, 平移到距离原始位置-50的位置
    37     //self.btnIcon.transform = CGAffineTransformMakeTranslation(0, -50); // 向上平移
    38     
    39     // 基于一个旧的值, 在进行平移
    40     // 基于现有的一个值, 再进行平移
    41     self.btnIcon.transform = CGAffineTransformTranslate(self.btnIcon.transform, 0, 50);
    42 }
    43 
    44 - (IBAction)rotate {
    45     // 45°
    46     //self.btnIcon.transform = CGAffineTransformMakeRotation(-M_PI_4);
    47     
    48     [UIView animateWithDuration:2.5 animations:^{
    49         self.btnIcon.transform = CGAffineTransformRotate(self.btnIcon.transform, -M_PI_4);
    50         self.btnIcon.transform = CGAffineTransformTranslate(self.btnIcon.transform, 0, 50);
    51         self.btnIcon.transform = CGAffineTransformScale(self.btnIcon.transform, 1.5, 1.5);
    52     }];
    53     
    54 }
    55 
    56 // 缩放
    57 - (IBAction)scale {
    58     //self.btnIcon.transform = CGAffineTransformMakeScale(0.5, 0.5);
    59     self.btnIcon.transform = CGAffineTransformScale(self.btnIcon.transform, 1.5, 1.5);
    60 }
    61 
    62 // 让控件回到原始的位置
    63 - (IBAction)goBack:(id)sender {
    64     self.btnIcon.transform = CGAffineTransformIdentity;
    65 }
    66 @end

     UIImageView

      1 利用一个小案例来说明image的属性
      2 @interface ViewController ()
      3 @property (weak, nonatomic) IBOutlet UIImageView *imgViewCat;
      4 
      5 - (IBAction)drink;
      6 
      7 
      8 - (IBAction)fart;
      9 
     10 - (IBAction)knockout;
     11 
     12 
     13 @end
     14 
     15 @implementation ViewController
     16 
     17 - (void)viewDidLoad {
     18     [super viewDidLoad];
     19     // Do any additional setup after loading the view, typically from a nib.
     20 }
     21 
     22 - (void)didReceiveMemoryWarning {
     23     [super didReceiveMemoryWarning];
     24     // Dispose of any resources that can be recreated.
     25 }
     26 // 喝牛奶的动画
     27 - (IBAction)drink {
     28     
     29     
     30     [self startAnimating:81 picName:@"drink"];
     31 }
     32 
     33 // 放P
     34 - (IBAction)fart {
     35    
     36     [self startAnimating:28 picName:@"fart"];
     37 }
     38 
     39 
     40 // 敲头
     41 - (IBAction)knockout {
     42     [self startAnimating:81 picName:@"knockout"];
     43 }
     44 
     45 
     46 
     47 
     48 // 执行动画的方法
     49 - (void)startAnimating:(int)count picName:(NSString *)picName
     50 {
     51     // 如果当前图片框正在执行动画, 那么直接return, 什么都不做(没有开启一个新动画)
     52     if (self.imgViewCat.isAnimating) {
     53         return;
     54     }
     55     
     56     // 1. 把图片加载到数组中
     57     // 0.动态加载图片到一个NSArray中
     58     NSMutableArray *arrayM = [NSMutableArray array];
     59     
     60     for (int i = 0; i < count; i++) {
     61         // 拼接图片名称
     62         NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg", picName, i];
     63         
     64         // 根据图片名称加载图片
     65         // 通过imageNamed: 这种方式加载图片, 加载好的图片会一直保存写在内存中, 不会释放.这样下次如果再使用同样的图片的时候就不需要再重新加载了, 因为内存里面已经有了。缺点就是: 如果加载了大量的图片, 那么这些图片会一直保留在内存中,导致应用程序占用内存过大(这就叫缓存)
     66         
     67         // 使用这种方式加载图片, 加载起来的图片即便没有强类型指针引用也不会销毁(会被缓存)
     68         //UIImage *imgCat = [UIImage imageNamed:imgName];
     69         
     70         
     71         
     72         
     73         
     74         // 使用下面这种方式加载的图片, 只要没有强类型指针引用就会被销毁了
     75         // 解决: 换一种加载图片的方式, 不要使用缓存
     76         // 获取图片的完成的路径
     77         NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
     78         
     79         // 这里的参数不能再传递图片名称了, 这里需要传递一个图片的完整路径
     80         UIImage *imgCat = [UIImage imageWithContentsOfFile:path];
     81         
     82         // 把图片加载到数组中
     83         [arrayM addObject:imgCat];
     84     }
     85     
     86     // 2. 设置UIImageView的animationImages属性为对应的图片集合
     87     self.imgViewCat.animationImages = arrayM;
     88     
     89     // 3. 动画持续时间
     90     self.imgViewCat.animationDuration = self.imgViewCat.animationImages.count * 0.1;
     91     
     92     
     93     // 4. 重复次数
     94     self.imgViewCat.animationRepeatCount = 1;
     95     
     96     // 5. 启动动画
     97     [self.imgViewCat startAnimating];
     98     
     99     
    100     // 清空图片集合
    101     // 这样些写的问题是, 当动画启动以后, 动画还没开始执行, 就已经让图片集合清空了, 也就是说self.imgViewCat.animationImages 里面已经没有图片了, 所以动画就不执行了。
    102     //self.imgViewCat.animationImages = nil;
    103     
    104     
    105     
    106     // self.imgViewCat.animationImages = nil; 需要延迟一段时间执行, 当动画执行完毕以后再清空这些图片
    107     //[self.imgViewCat setAnimationImages:nil];
    108     
    109     
    110     // 设置图片框在调用setAnimationImages:nil方法的时候延迟执行
    111     [self.imgViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imgViewCat.animationImages.count * 0.1];
    112 }
    113 
    114 
    115 
    116 
    117 @end
  • 相关阅读:
    Leetcode 15 3Sum
    Leetcode 383 Ransom Note
    用i个点组成高度为不超过j的二叉树的数量。
    配对问题 小于10 1.3.5
    字符矩阵的旋转 镜面对称 1.2.2
    字符串统计 连续的某个字符的数量 1.1.4
    USACO twofive 没理解
    1002 All Roads Lead to Rome
    USACO 5.5.1 求矩形并的周长
    USACO 5.5.2 字符串的最小表示法
  • 原文地址:https://www.cnblogs.com/developer-wang/p/4519335.html
Copyright © 2011-2022 走看看