1. 程式人生 > >史上最簡單的iOS教程(二)

史上最簡單的iOS教程(二)

本節目錄

  • UILabel
  • UIimage
  • UIimage contentMode屬性
  • UIimage小語法點
  • UIimage initWithImage:方法
  • UIImageView的frame設定
  • UIimage 修改frame的3種方式
  • 抽取重複程式碼
  • UIImageView的幀動畫
  • 延遲做一些事情
  • 音訊檔案的簡單播放
  • 監聽按鈕點選
  • UIControl
  • UIAlertView

UILabel

    // 1.1 建立UILabel物件
    UILabel *label = [[UILabel alloc] init];

    // 1.2 設定frame
    label.frame
= CGRectMake(100, 100, 202, 175); // 1.3 設定背景顏色 label.backgroundColor = [UIColor redColor]; // 1.4 設定文字 label.text = @"opooc opooc opooc "; // 1.5 居中 label.textAlignment = NSTextAlignmentCenter; // 1.6 設定字型大小 label.font = [UIFont systemFontOfSize:20.f]; label.font = [UIFont
boldSystemFontOfSize:25.f]; label.font = [UIFont italicSystemFontOfSize:20.f]; // 1.7 設定文字的顏色 label.textColor = [UIColor whiteColor]; // 1.8 設定陰影(預設是有值) label.shadowColor = [UIColor blackColor]; label.shadowOffset = CGSizeMake(-2, 1); // 1.9 設定行數(0:自動換行) label.numberOfLines
= 1; // 1.10 顯示模式 label.lineBreakMode = NSLineBreakByTruncatingHead; /* NSLineBreakByWordWrapping = 0, // 單詞包裹,換行的時候會以一個單詞換行 NSLineBreakByCharWrapping, // 字元包裹換行,換行的時候會以一個字元換行 NSLineBreakByClipping, // 裁剪超出的內容 NSLineBreakByTruncatingHead, // 一行中頭部省略(注意:numberOfLines要為1): "...wxyz" NSLineBreakByTruncatingTail, // 一行中尾部省略: "abcd..." NSLineBreakByTruncatingMiddle // 一行中中間部省略: "ab...yz" */

UIimage

     // 1.建立UIImageView物件
    UIImageView *imageView = [[UIImageView alloc] init];

    // 2. 設定尺寸
    //    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    imageView.frame = self.view.bounds;

    // 3. 設定背景顏色
    imageView.backgroundColor = [UIColor redColor];

    // 4. 設定背景圖片
    imageView.image = [UIImage imageNamed:@"1"];

    // 5.設定圖片的內容模式
    imageView.contentMode = UIViewContentModeScaleAspectFill;

    // 6.加毛玻璃

    // 6.1 建立UIToolBar物件
    UIToolbar *toolBar = [[UIToolbar alloc] init];
    // 6.2 設定toolBar的frame(這個地方用的是bound,如果是用frame也可以,不過應該對應的是self.view,導致每次調整imageView的時候 ,還需要修改toolBar的frame)
    toolBar.frame = imageView.bounds;

    // 6.3 設定毛玻璃的樣式
    toolBar.barStyle = UIBarStyleBlack;
    toolBar.alpha = 0.98;
    // 6.4 加到imageView中
    [imageView addSubview:toolBar];

    // 加到控制器的view中
    [self.view addSubview:imageView];

UIimage contentMode屬性

  • 帶有scale單詞的:圖片有可能會拉伸

    • UIViewContentModeScaleToFill
      • 將圖片拉伸至填充整個imageView
      • 圖片顯示的尺寸跟imageView的尺寸是一樣的
    • 帶有aspect單詞的:保持圖片原來的寬高比
      • UIViewContentModeScaleAspectFit
        • 保證剛好能看到圖片的全部
      • UIViewContentModeScaleAspectFill
        • 拉伸至圖片的寬度或者高度跟imageView一樣
  • 沒有scale單詞的:圖片絕對不會被拉伸,保持圖片的原尺寸

    • UIViewContentModeCenter
    • UIViewContentModeTop
    • UIViewContentModeBottom
    • UIViewContentModeLeft
    • UIViewContentModeRight
    • UIViewContentModeTopLeft
    • UIViewContentModeTopRight
    • UIViewContentModeBottomLeft
    • UIViewContentModeBottomRight

UIimage小語法點

  • 不能直接修改:OC物件的結構體屬性的成員
  • 下面的寫法是錯誤的
imageView.frame.size = imageView.image.size;
  • 正確寫法
CGRect tempFrame = imageView.frame;
tempFrame.size = imageView.image.size;
imageView.frame = tempFrame;

UIimage initWithImage:方法

  • 利用這個方法創建出來的imageView的尺寸和傳入的圖片尺寸一樣

UIImageView的frame設定

  /*方式一、三、四常用


    // 設定frame的方式
    // 方式一
      UIImageView *imageView = [[UIImageView alloc] init];
      imageView.image = [UIImage imageNamed:@"1"];

//    imageView.frame = CGRectMake(100, 100, 267, 400);
//    imageView.frame = (CGRect){{100, 100},{267, 400}};
    */

    // 方式二
    /*
    UIImageView *imageView = [[UIImageView alloc] init];
    // 建立一個UIImage物件
    UIImage *image = [UIImage imageNamed:@"1"];
    // 設定frame
    imageView.frame = CGRectMake(100, 10, image.size.width, image.size.height);
    // 設定圖片
    imageView.image = image;
     */

    // 方式三
    /*
    // 建立一個UIImage物件
    UIImage *image = [UIImage imageNamed:@"1"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, image.size.width, image.size.height)];
    imageView.image = image;
     */

    // 方式四

    // 建立一個UIimageview物件
    // 注意: initWithImage 預設就有尺寸--->圖片的尺寸
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];

    // 改變位置
//    imageView.center = CGPointMake(200, 150);

    imageView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);

    [self.view addSubview:imageView];    

UIimage 修改frame的3種方式

  • 直接使用CGRectMake函式
imageView.frame = CGRectMake(100, 100, 200, 200);
  • 利用臨時結構體變數
CGRect tempFrame = imageView.frame;
tempFrame.origin.x = 100;
tempFrame.origin.y = 100;
tempFrame.size.width = 200;
tempFrame.size.height = 200;
imageView.frame = tempFrame;
  • 使用大括號{}形式
imageView.frame = (CGRect){{100, 100}, {200, 200}};

抽取重複程式碼

- 將相同程式碼放到一個新的方法中
- 不用的東西就變成方法的引數
  • 圖片的載入方式
    • 有快取
      objc
      UIImage *image = [UIImage imageNamed:@"圖片名"];

      • 使用場合:圖片比較小、使用頻率較高
      • 建議把需要快取的圖片直接放到Images.xcassets
    • 無快取
      objc
      NSString *file = [[NSBundle mainBundle] pathForResource:@"圖片名" ofType:@"圖片的副檔名"];
      UIImage *image = [UIImage imageWithContentsOfFile:@"圖片檔案的全路徑"];

      • 使用場合:圖片比較大、使用頻率較小
      • 不需要快取的圖片不能放在Images.xcassets
    • 放在Images.xcassets裡面的圖片,只能通過圖片名去載入圖片

UIImageView的幀動畫

    NSMutableArray<UIImage *> *imageArr = [NSMutableArray array];
    for (int i=0; i<20; i++) {
        // 獲取圖片的名稱
        NSString *imageName = [NSString stringWithFormat:@"%d", i+1];
        // 建立UIImage物件
        UIImage *image = [UIImage imageNamed:imageName];
        // 加入陣列
        [imageArr addObject:image];
    }
    // 設定動畫圖片
    self.imageView.animationImages = imageArr;

    // 設定動畫的播放次數
    self.imageView.animationRepeatCount = 0;

    // 設定播放時長
    // 1秒30幀, 一張圖片的時間 = 1/30 = 0.03333 20 * 0.0333
    self.imageView.animationDuration = 1.0;

    // 開始動畫
    [self.imageView startAnimating];

延遲做一些事情

[abc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
// 10s後自動呼叫abc的stand:方法,並且傳遞@"123"引數

音訊檔案的簡單播放

// 建立一個音訊檔案的URL(URL就是檔案路徑物件)
NSURL *url = [[NSBundle mainBundle] URLForResource:@"音訊檔名" withExtension:@"音訊檔案的副檔名"];
// 建立播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];

或者

// 建立一個音訊檔案的URL(URL就是檔案路徑物件)
 NSString* str = [[NSBundle mainBundle]pathForResource:@"音訊檔名" ofType:@"音訊檔案的副檔名"];

NSURL *url = [NSURL fileURLWithPath:str];
// 建立播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];

監聽按鈕點選

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

UIControl

  • 凡是繼承自UIControl的控制元件,都可以通過addTarget:…方法來監聽事件

UIAlertView

 // 建立物件
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"輸入有誤" message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];

    // 顯示
    [alertView show];

相關推薦

關於海康威視sdk與海康威視web的精細的教程

小夥伴們都在下方留言要開發包 web: https://download.csdn.net/download/qq_35583089/10537585    sdk:    https://download.csdn.net/download/qq_35583089/10

簡單iOS教程

本節目錄 UILabel UIimage UIimage contentMode屬性 UIimage小語法點 UIimage initWithImage:方法 UIImageView的frame設定

簡單MySQL教程詳解基礎篇之多表聯合查詢

常用術語 內連線 外連線 左外連線 右外連線 注意事項: 自連線 子查詢 在上篇文章史上最簡單MySQL教程詳解(基礎篇)之資料庫設計正規化及應用舉例我們介紹過,在關係型資料庫中,我們通常為了減少資料的冗餘量將對資料表進行規範,將

簡單MySQL教程詳解進階篇之儲存引擎介紹及預設引擎設定

什麼是儲存引擎? 與其他資料庫例如Oracle 和SQL Server等資料庫中只有一種儲存引擎不同的是,MySQL有一個被稱為“Pluggable Storage Engine Architecture”(可替換儲存引擎架構)的特性,也就意味著My

Intellij idea簡單教程之Linux下安裝與破解Intellij idea2017

成功 zxvf java 新建 pre form 旗艦版 lan intel 一、前言 這一節我們介紹在Linux下如何安裝與破解Intellij idea2017。現在有很多公司開發環境都是Linux,所以掌握在Linux環境下使用Idea辦公也是咱們必須得掌握的技能。

【Android】從無到有:手把手一步步教你使用簡單的Fragment

轉載請註明出處,原文連結:https://blog.csdn.net/u013642500/article/details/80579389 【本文適用讀者】         targetSdkVersion 版本大於等於 21,即 app 即將有可能

.Net Core 在 Linux-Centos的部署實戰教程

上篇我們說了 如何在Linux上部署.net core  但是有心的同學會發現你關閉掉終端網站就不能訪問了,這個原因是因為直接 dotnet GetConfigFile.dll --server.urls http://*:5000 這麼執行不是後臺執行,我們讓他後臺執行就可以了 使用nohup設

簡單的 MySQL 教程十三「資料的高階操作 之 查詢

溫馨提示:本系列博文已經同步到 GitHub,地址為「mysql-tutorial」,歡迎感興趣的童鞋Star、Fork,糾錯。 資料的高階操作 查詢資料(上) 基本語法: select + 欄位列表/* + from + 表名 + [whe

簡單的 MySQL 教程十五「外來鍵」

外來鍵外來鍵:foreign key,外面的鍵,即不在自己表中的鍵。如果一張表中有一個非主鍵的欄位指向另外一張表的主鍵,那麼將該欄位稱之為外來鍵。每張表中,可以有多個外來鍵。新增外來鍵外來鍵既可以在建立表的時候增加,也可以在建立表之後增加(但是要考慮資料的問題)。第 1 種:在建立表的時候,增加外來鍵基本語法

簡單的 MySQL 教程十四「連線查詢」

連線查詢連線查詢:將多張表(大於等於 2 張表)按照某個指定的條件進行資料的拼接,其最終結果記錄數可能有變化,但欄位數一定會增加。連線查詢的意義:在使用者查詢資料的時候,需要顯示的資料來自多張表。連線查詢為join,使用方式為:左表join右表。左表:join左邊的表;右表:join右邊的表。連線查詢分類:在

簡單iOS教程

本節目錄 UIButton 綜合示例 九宮格公式 UIbutton 按鈕的作用 可以和使用者互動 技能顯示圖片,也能顯示文字 按鈕的狀態 normal(普通狀態) 預設情況(D

簡單的SpringCloud教程 | 第二篇: 服務消費者rest+ribbon

image tree 開啟 then rom cat learn 替代 官網 最新Finchley版本:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/或者http://blog.csdn.n

簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現Eureka

add 過程 sdn 需要 2.3 boot one ini tail 最新Finchley版本請訪問:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/或者http://blog.csdn.n

簡單的SpringCloud教程 | 第十篇: 斷路器監控(Hystrix Dashboard)

詳細 pre 良好的 依次 alt ews 需要 ext 數據監控 最新Finchley版本,請訪問:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/或者http://blog.csdn.net

簡單的SpringCloud教程 | 第四篇:斷路器Hystrix

技術分享 熔斷器 enable layer get local nsh 12c host 在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證

簡單的SpringCloud教程 | 第四篇:斷路器Hystrix(Finchley版本)

stat api serve 依賴 網頁 固定 lock 不能 mar 在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證其高可用,單個服務

簡單的SpringCloud教程 | 第三篇: 服務消費者Feign

最新Finchley版本請訪問: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 或者 http://blog.csdn.net/forezp/article/details/810409

簡單的SpringCloud教程大全Finchley版本

史上最簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現Eureka(Finchley版本) https://blog.csdn.net/forezp/article/details/81040925 史上最簡單的SpringCloud教程 | 第二篇: 服務

簡單的SpringCloud教程 | 第二篇: 服務消費者rest+ribbon(Finchley版本)

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於rib

簡單的SpringCloud教程 | 第十篇: 斷路器監控(Hystrix Dashboard)(Finchley版本)

在我的第四篇文章斷路器講述瞭如何使用斷路器,並簡單的介紹了下Hystrix Dashboard元件,這篇文章更加詳細的介紹Hystrix Dashboard。 一、Hystrix Dashboard簡介 在微服務架構中為例保證程式的可用性,防止程式出錯導致網路阻塞,出現了斷