Swift版CardView实现

Cards是MaterialDesign的一个数据集展示设计方案,也是现在很多Android应用所使用的列表展示方案。

今天介绍一个Swift版IOS的CardView实现库,轻松实现响应迅速、支持滑动动画和点击动画的Cards

先上地址:https://github.com/icaksama/CardViewList

Cocoapods集成方式:

pod 'CardViewList'

集成完成后在项目中导入:

import CardViewList

初次导入时XCode可能会报错找不到模块,Build一下即可消除错误

使用方法:

1.新建一个CardViewController作为子视图控制器,在CardViewController中设置每个Card的内容并为Cards添加手势识别器

import UIKit

class CardViewController: UIViewController {
    // MARK: - 懒加载需要的View
    var image: UIImageView = {
        let image = UIImageView()
        image.frame = CGRect.init(x: 10, y: 10, width: 100, height: 100)
        image.image = UIImage.init(named: "1")
        return image
    }()
    var label : UILabel = {
        let label = UILabel.init(frame: CGRect(x: 120, y: 10, width: 100, height: 40))
        label.text = "Label"
        label.textAlignment = .center
        return label
    }()
    // MARK: - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        //将Views添加到主视图
        view.addSubview(label)
        view.addSubview(image)
        //设置主视图(Card)背景色和Size
        view.backgroundColor = .white
        view.frame = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height: 140.0))
        //为Card添加单击手势识别器
        addTapGesture()
        
        // Do any additional setup after loading the view.
        //setData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
//MARK: - 扩展CardViewController
extension CardViewController {
    //设置Card内容和Tag的方法
    public func setData(image: String = "1" , text: String = "Card 0", tagID: Int = 0){
        self.image.image = UIImage.init(named: "\(image)")
        self.label.text = "\(text)"
        self.view.tag = tagID
        //print("initDone + \(label.text)")
    }
    //添加手势识别器
    private func addTapGesture(){
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickView))
        self.view.addGestureRecognizer(tapGesture)
    }
    //单击手势事件
    @objc private func clickView(sender : UITapGestureRecognizer){
        print(self.view.tag)
    }
}

2.在ViewController中实现CardViewListDelegete协议,选择需要的方法实现

// Response when CardView will Display.
func cardView(willDisplay scrollView: UIScrollView, identifierCards identifier: String) {}

// Response when every single of CardView from UIView will attach.
func cardView(_ scrollView: UIScrollView, willAttachCardView cardView: UIView, identifierCards identifier: String, index: Int) {}

// Response when every single of CardView from UIViewController will attach.
func cardView(_ scrollView: UIScrollView, willAttachCardViewController cardViewController: UIViewController, identifierCards identifier: String, index: Int) {}

// Response when CardView from UIView did finish attached.
func cardView(_ scrollView: UIScrollView, didFinishDisplayCardViews cardViews: [UIView], identifierCards identifier: String) {}

// Response when CardView from UIViewController did finish attached.
func cardView(_ scrollView: UIScrollView, didFinishDisplayCardViewControllers cardViewsController: [UIViewController], identifierCards identifier: String) {}

// Response when CardView is selected.
func cardView(_ scrollView: UIScrollView, didSelectCardView cardView: UIView, identifierCards identifier: String, index: Int) {}

3.实例化一个CardViewList,为其设置Delegete

fileprivate var cardViewList:CardViewList = CardViewList()
cardViewList.delegete = self
4.实例化一个CardViewController的集合,并调用setData方法设置CardView的内容和Tag
  var cardViewControllers = [UIViewController]()
for i in 1...25 {
   cardViewController = CardViewController()
   cardViewController.setData(image: "1", text: "Card\(i)", tagID: i)
   print("forinViewTag + \(cardViewController.view.tag)")
   cardViewControllers.append(cardViewController)
}
5.设置CardViewList的属性
        /** Set disable/enable shadow of CardView. Default is true */
        cardViewList.isShadowEnable = true

        /** Set disable/enable click of CardView. Default is false */
        cardViewList.isClickable = false

        /** Set multiple touch of CardView. Default is false */
        cardViewList.isMultipleTouch = true

        /** Set corner radius of card view in pixel. Default is 12.0 */
        cardViewList.cornerRadius = 5.0

        /** Set shadow size of card view in pixel. Default is 5.0 */
        cardViewList.shadowSize = 5.0

        /** Set shadow opacity of card view in 0 - 1. Default is 0.9 */
        cardViewList.shadowOpacity = 0.5

        /** Set shadow color of card view. Default color is black */
        cardViewList.shadowColor = UIColor.black

        /** Set CardView margin in percent(%) of containerView. Default is 5 */
        cardViewList.margin = 4

        /** Set margin between CardView in percent(%) of containerView. Default is 5 */
        cardViewList.marginCards = 4

        /** Set list type horizontal, vertical, horizontalVertical. Default is vertical */
        cardViewList.listType = .vertical

        /** Set grid List of CardView. Default is 1 */
        cardViewList.grid = 1

        /** Set size of CardView. Default is autoSize */
        cardViewList.cardSizeType = .autoSize

        /** Set max width of CardView in percent(%) of containerView. Default is 100 */
        cardViewList.maxWidth = 100

        /** Set max height of CardView in percent(%) of containerView. Default is 100 */
        cardViewList.maxHeight = 100

        /** Set animation when CardView showing. Default is none */
        cardViewList.animationScroll = .scaleBounce

        /** Set animation click for CardView. Default is none */
        cardViewList.clickAnimation = .none

6.将CardViewList添加到父视图

cardViewList.generateCardViewList(containerView: self.view, viewControllers: cardViewControllers, listType: .vertical, identifier: "CardWithUIViewController")

7.Run    Success~






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,可以使用 CardView 控件来实现四周阴影效果。CardView 内部已经实现了阴影效果,只需要将需要添加阴影的布局放在 CardView 中即可。 以下是使用 CardView 实现四周阴影效果的示例代码: 1. 在布局文件中添加 CardView 控件,并将需要添加阴影效果的布局放在 CardView 中。 ``` <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" app:cardCornerRadius="8dp" app:cardElevation="8dp" app:cardUseCompatPadding="true"> <!-- 需要添加阴影效果的布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个示例布局" android:textSize="24sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个示例布局的内容" android:textSize="16sp" /> </LinearLayout> </androidx.cardview.widget.CardView> ``` 在 CardView 控件中,使用 app:cardCornerRadius 属性可以设置圆角半径,使用 app:cardElevation 属性可以设置阴影大小,使用 app:cardUseCompatPadding 属性可以设置是否使用兼容性内边距。 2. 运行应用程序,即可看到添加了四周阴影效果的布局。 这样就可以使用 CardView 控件来实现 Android 中的四周阴影效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值