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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM