欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS應(yīng)用中使用Auto Layout實現(xiàn)自定義cell及拖動回彈

 更新時間:2016年03月06日 09:31:26   作者:JohnLui  
這篇文章主要介紹了iOS應(yīng)用中使用Auto Layout實現(xiàn)自定義cell及拖動回彈的方法,自定義UITableViewCell并使用Auto Layout對其進行約束可以方便地針對多尺寸屏幕進行調(diào)整,代碼為Swift語言,需要的朋友可以參考下

自定義 cell 并使用 Auto Layout
創(chuàng)建文件
我們可以一次性創(chuàng)建 xib 文件和類的代碼文件。

新建 Cocoa Touch Class:

20163692249976.jpg (730×430)

設(shè)置和下圖相同即可:

20163692312710.jpg (730×430)

檢查成果

20163692333493.jpg (1920×988)

分別選中上圖中的 1、2 兩處,檢查 3 處是否已經(jīng)自動綁定為 firstTableViewCell,如果沒有綁定,請先檢查選中的元素確實是 2,然后手動綁定即可。

完成綁定工作
切換一頁,如下圖進行 Identifier 設(shè)置:

20163692358965.jpg (258×288)

新建 Table View Controller 頁面
新建一個 Table View Controller 頁面,并把我們之前創(chuàng)建的 Swift on iOS 那個按鈕的點擊事件綁定過去,我們得到:

20163692415659.jpg (1387×749)

然后創(chuàng)建一個名為 firstTableViewController 的 UITableViewController 類,創(chuàng)建流程跟前面基本一致。不要創(chuàng)建 xib。然后選中 StoryBoard 中的 Table View Controller(選中之后有藍色邊框包裹),在右側(cè)對它和 firstTableViewController 類進行綁定:

20163692431858.jpg (946×787)

調(diào)用自定義 cell
修改 firstTableViewController 類中的有效代碼如下:

復(fù)制代碼 代碼如下:

import UIKit

class firstTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var nib = UINib(nibName: "firstTableViewCell", bundle: nil)
        self.tableView.registerNib(nib, forCellReuseIdentifier: "firstTableViewCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("firstTableViewCell", forIndexPath: indexPath) as firstTableViewCell

        cell.textLabel?.text = indexPath.row.description

        return cell
    }
}


viewDidLoad() 中添加的兩行代碼是載入 xib 的操作。最下面的三個 func 分別是定義:

self.tableView 中有多少個 section
每個 section 中分別有多少個條目
實例化每個條目,提供內(nèi)容
如果你得到以下頁面,說明你調(diào)用自定義 cell 成功了!

20163692456608.jpg (375×689)

給自定義 cell 添加元素并使用 Auto Layout 約束
首先向 Images.xcassets 中隨意加入一張圖片。

然后在左側(cè)文件樹中選中 firstTableViewCell.xib,從右側(cè)組件庫中拖進去一個 Image View,并且在右側(cè)將其尺寸設(shè)置如下圖右側(cè):

20163692516167.jpg (1245×472)

給 ImageView 添加約束:

20163692533342.jpg (1191×729)

選中該 ImageView(左箭頭所示),點擊自動 Auto Layout(右箭頭所示),即可。

給 ImageView 設(shè)置圖片:

20163692550545.jpg (1209×544)

再從右側(cè)組件庫中拖入一個 UILabel,吸附到最右側(cè),垂直居中,為其添加自動約束,這一步不再贅述。

在 firstTableViewCell 類中綁定 xib 中拖進去的元素
選中 firstTableViewCell.xib,切換到雙視圖,直接進行拖動綁定:

20163692609674.jpg (1403×903)

綁定完成!

約束 cell 的高度
在 firstTableViewController 中添加以下方法:

復(fù)制代碼 代碼如下:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 50
}

給自定義的 UILabel 添加內(nèi)容
修改 firstTableViewController 中以下函數(shù)為:
復(fù)制代碼 代碼如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("firstTableViewCell", forIndexPath: indexPath) as firstTableViewCell

    cell.firstLabel.text = indexPath.row.description

    return cell
}


查看結(jié)果
4.0 寸:

20163692631719.jpg (320×590)

4.7 寸:

20163692646755.jpg (375×689)

如果你得到以上結(jié)果,那么恭喜你自定義 cell 并使用 Auto Layout 成功!


22 行代碼實現(xiàn)拖動回彈
搭建界面
刪除首頁中間的按鈕,添加一個 View ,設(shè)置一種背景色便于辨認(rèn),然后對其進行絕對約束:

20163692714280.jpg (1363×857)

拖動一個 UIPanGestureRecognizer 到該 View 上:

20163692730213.jpg (1358×884)

界面搭建完成。

屬性綁定
切換到雙向視圖,分別右鍵拖動 UIPanGestureRecognizer 和該 View 的 Top Space 的 Auto Layout 屬性到 ViewController 中綁定:

20163692804484.jpg (1383×743)

然后將 UIPanGestureRecognizer 右鍵拖動綁定:

20163692824786.jpg (1343×533)

編寫代碼

復(fù)制代碼 代碼如下:

class ViewController: UIViewController {
   
    var middleViewTopSpaceLayoutConstant: CGFloat!
    var middleViewOriginY: CGFloat!
   
    @IBOutlet weak var middleView: UIView!
    @IBOutlet weak var middleViewTopSpaceLayout: NSLayoutConstraint!
    @IBOutlet var panGesture: UIPanGestureRecognizer!
    override func viewDidLoad() {
        super.viewDidLoad()
       
        panGesture.addTarget(self, action: Selector("pan"))
        middleViewTopSpaceLayoutConstant = middleViewTopSpaceLayout.constant
        middleViewOriginY = middleView.frame.origin.y
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    func pan() {
        if panGesture.state == UIGestureRecognizerState.Ended {
            UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                self.middleView.frame.origin.y = self.middleViewOriginY
                }, completion: { (success) -> Void in
                    if success {
                        self.middleViewTopSpaceLayout.constant = self.middleViewTopSpaceLayoutConstant
                    }
            })
            return
        }
        let y = panGesture.translationInView(self.view).y
        middleViewTopSpaceLayout.constant = middleViewTopSpaceLayoutConstant + y
    }

}


查看效果

20163692843461.gif (435×726)

22 行代碼,拖動回彈效果完成!

相關(guān)文章

  • iOS15適配小結(jié)

    iOS15適配小結(jié)

    本文主要介紹了iOS15適配小結(jié),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • iOS實現(xiàn)簡單的頭部縮放功能

    iOS實現(xiàn)簡單的頭部縮放功能

    這篇文章主要介紹了iOS 簡單的頭部縮放效果,頭部伴隨模糊效果放大縮小,并在一定位置時懸停充當(dāng)導(dǎo)航欄,本文給大家提供實現(xiàn)思路,需要的朋友可以參考下
    2018-08-08
  • iOS圖片模糊效果的實現(xiàn)方法

    iOS圖片模糊效果的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了iOS圖片模糊效果的三種實現(xiàn)方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • swift 隱式可選型實例詳解

    swift 隱式可選型實例詳解

    這篇文章主要介紹了 swift 隱式可選型實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS常用小功能(獲得屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size)

    iOS常用小功能(獲得屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size)

    本文主要介紹了iOS常用小功能:獲得屏幕圖像,label的動態(tài)size,時間戳轉(zhuǎn)化為時間,RGB轉(zhuǎn)化成顏色,加邊框,壓縮圖片,textfield的placeholder,圖片做灰度處理的方法。下面跟著小編一起來看下吧
    2017-03-03
  • iOS實現(xiàn)簡易鐘表

    iOS實現(xiàn)簡易鐘表

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)簡易鐘表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • IOS 網(wǎng)絡(luò)請求中設(shè)置cookie

    IOS 網(wǎng)絡(luò)請求中設(shè)置cookie

    這篇文章主要介紹了IOS 網(wǎng)絡(luò)請求中設(shè)置cookie的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS實現(xiàn)簡易的計算器

    iOS實現(xiàn)簡易的計算器

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)簡易的計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • iOS通過UISwitch控制搖一搖

    iOS通過UISwitch控制搖一搖

    這篇文章主要為大家詳細介紹了iOS通過UISwitch控制搖一搖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • iOS中使用RSA加密詳解

    iOS中使用RSA加密詳解

    本文主要介紹了iOS中使用RSA加密的方法,具有一定的參考價值,下面跟著小編一起來看下吧
    2016-12-12

最新評論