Skip to content

Commit 124459d

Browse files
author
lilin1
committed
完善表情包控件
1 parent 0075d68 commit 124459d

File tree

3 files changed

+176
-149
lines changed

3 files changed

+176
-149
lines changed

LLSegmentViewController.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
1807E1CA2685C0BF00A0661D /* EmoticonViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1807E1C92685C0BF00A0661D /* EmoticonViewController.swift */; };
11+
1807E1CC2685E02F00A0661D /* EmoticonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1807E1CB2685E02F00A0661D /* EmoticonView.swift */; };
1112
1813AAEF21ECD1EA0005FA4A /* BackColorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1813AAEE21ECD1EA0005FA4A /* BackColorViewController.swift */; };
1213
1813AAF121ECE3AE0005FA4A /* TitleImageItemViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1813AAF021ECE3AE0005FA4A /* TitleImageItemViewController.swift */; };
1314
1818A8A321FC48D900588CB4 /* GoodsDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1818A8A221FC48D900588CB4 /* GoodsDetailViewController.swift */; };
@@ -77,6 +78,7 @@
7778

7879
/* Begin PBXFileReference section */
7980
1807E1C92685C0BF00A0661D /* EmoticonViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmoticonViewController.swift; sourceTree = "<group>"; };
81+
1807E1CB2685E02F00A0661D /* EmoticonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmoticonView.swift; sourceTree = "<group>"; };
8082
1813AAEE21ECD1EA0005FA4A /* BackColorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackColorViewController.swift; sourceTree = "<group>"; };
8183
1813AAF021ECE3AE0005FA4A /* TitleImageItemViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TitleImageItemViewController.swift; sourceTree = "<group>"; };
8284
1818A8A221FC48D900588CB4 /* GoodsDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoodsDetailViewController.swift; sourceTree = "<group>"; };
@@ -185,6 +187,7 @@
185187
F23B25D822451F4E00262FC0 /* SimpDemoViewController.swift */,
186188
F2D939E722F8039700132DD4 /* NavViewController.swift */,
187189
1807E1C92685C0BF00A0661D /* EmoticonViewController.swift */,
190+
1807E1CB2685E02F00A0661D /* EmoticonView.swift */,
188191
F2055B4421D39CB300FF961B /* IndicatorViewController */,
189192
F28F1C7021F9B75400153A61 /* DetailViewController */,
190193
F2DE12D821E0588400D13738 /* CustomItemView */,
@@ -538,6 +541,7 @@
538541
F28F1C6F21F9599700153A61 /* LLCtlPageView.swift in Sources */,
539542
F2D33167220185F900025A48 /* LLSegmentItemTitleImageView.swift in Sources */,
540543
18AF01F321E255AF00FD5F14 /* MixViewController.swift in Sources */,
544+
1807E1CC2685E02F00A0661D /* EmoticonView.swift in Sources */,
541545
F23D2F7B21CA53830067B399 /* LLToolFunc.swift in Sources */,
542546
F2D939E822F8039700132DD4 /* NavViewController.swift in Sources */,
543547
F28F1C6E21F9599700153A61 /* LLContainerScrollView.swift in Sources */,
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
//
2+
// EmoticonView.swift
3+
// LLSegmentViewController
4+
//
5+
// Created by apple on 2021/6/25.
6+
// Copyright © 2021 lilin. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
12+
class EmoticonView: UIView {
13+
let segCtlHeight:CGFloat = 40
14+
let segCtlView:LLSegmentedControl
15+
var colView:UICollectionView
16+
var items = [LLSegmentItemTitleImageTabBarItem]()
17+
18+
var indexToList = [Int:UIView]()
19+
override init(frame: CGRect) {
20+
let colFrame = CGRect.init(x: 0, y: segCtlHeight, width: frame.width, height: frame.height - segCtlHeight)
21+
self.items = EmoticonView.creatItem()
22+
self.colView = EmoticonView.creatCol(frame: colFrame)
23+
24+
let segFrame = CGRect.init(x: 0, y: 0, width: frame.width, height: segCtlHeight)
25+
segCtlView = LLSegmentedControl.init(frame: segFrame, tabBarItems: items)
26+
super.init(frame: frame)
27+
28+
let titleViewStyle = LLSegmentItemTitleViewStyle()
29+
var ctlViewStyle = LLSegmentedControlStyle()
30+
ctlViewStyle.segmentItemViewClass = LLSegmentItemTitleImageView.self
31+
ctlViewStyle.contentInset = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
32+
ctlViewStyle.itemViewStyle = titleViewStyle
33+
segCtlView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
34+
segCtlView.indicatorView.shapeStyle = .background(color: UIColor.red, img: nil)
35+
segCtlView.reloadData(ctlViewStyle: ctlViewStyle)
36+
addSubview(segCtlView)
37+
segCtlView.reloadData()
38+
39+
colView.delegate = self
40+
colView.dataSource = self
41+
addSubview(colView)
42+
segCtlView.associateScrollerView = colView
43+
}
44+
45+
required init?(coder: NSCoder) {
46+
fatalError("init(coder:) has not been implemented")
47+
}
48+
}
49+
50+
extension EmoticonView {
51+
static func creatItem()->[LLSegmentItemTitleImageTabBarItem] {
52+
var items = [LLSegmentItemTitleImageTabBarItem]()
53+
let datas:[(title:String,imageStr:String,style:LLTitleImageButtonStyle)] = [
54+
("螃蟹","watermelon",LLTitleImageButtonStyle.titleEmty),
55+
("麻辣小龙虾","lobster",LLTitleImageButtonStyle.titleEmty),
56+
("苹果","grape", LLTitleImageButtonStyle.titleEmty),
57+
("营养胡萝卜","crab",LLTitleImageButtonStyle.titleEmty),
58+
("葡萄","carrot",LLTitleImageButtonStyle.titleEmty),
59+
("美味西瓜","apple",LLTitleImageButtonStyle.titleEmty),]
60+
for (title,imageStr, diyStyle) in datas {
61+
let tabBarItem = LLSegmentItemTitleImageTabBarItem.init(
62+
title: title,
63+
image: UIImage(named: imageStr),
64+
selectedImage: UIImage(named: imageStr + "_selected"))
65+
66+
tabBarItem.style = diyStyle
67+
tabBarItem.setImageBlock = { (imageView,isSelected,tabBarItem) in
68+
imageView.image = isSelected ? tabBarItem.selectedImage ?? tabBarItem.image : tabBarItem.image
69+
}
70+
items.append(tabBarItem)
71+
}
72+
return items
73+
}
74+
75+
static func creatCol(frame:CGRect) -> UICollectionView {
76+
let viewWidth = frame.width
77+
let viewHeight = frame.height
78+
let interitemSpacing:CGFloat = 0
79+
80+
let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
81+
let itemSizeWidth:CGFloat = viewWidth
82+
let layout = UICollectionViewFlowLayout()
83+
layout.itemSize = CGSize(width: itemSizeWidth, height: viewHeight)
84+
layout.minimumInteritemSpacing = interitemSpacing
85+
layout.minimumLineSpacing = 0
86+
layout.scrollDirection = .horizontal
87+
88+
let colView = UICollectionView(frame: frame, collectionViewLayout: layout)
89+
colView.isPagingEnabled = true
90+
91+
colView.backgroundColor = UIColor.clear
92+
colView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
93+
colView.contentInset = contentInset
94+
return colView
95+
}
96+
}
97+
98+
extension EmoticonView: UICollectionViewDelegate,UICollectionViewDataSource {
99+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
100+
return items.count
101+
}
102+
103+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
104+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
105+
let viewTag = 100
106+
cell.contentView.viewWithTag(viewTag)?.removeFromSuperview()
107+
let addView:UIView
108+
let frame = CGRect.init(origin: CGPoint.zero, size: self.colView.bounds.size)
109+
if let view = indexToList[indexPath.row] {
110+
addView = view
111+
}else{
112+
let newView = EmoticonListView.init(frame: frame)
113+
indexToList[indexPath.row] = newView
114+
addView = newView
115+
}
116+
addView.frame = frame
117+
addView.tag = viewTag
118+
cell.contentView.addSubview(addView)
119+
return cell
120+
}
121+
}
122+
123+
124+
/// 这里就需要自己根据业务需求进行拓展了
125+
class EmoticonListView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {
126+
let colView:UICollectionView
127+
let cellIdentifier = "cell"
128+
let randomColor = UIColor.init(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
129+
override init(frame: CGRect) {
130+
let colViewFrame = CGRect.init(origin: CGPoint.zero, size: frame.size)
131+
colView = EmoticonListView.creatCol(frame: colViewFrame, cellIdentifier: cellIdentifier)
132+
super.init(frame: frame)
133+
colView.delegate = self
134+
colView.dataSource = self
135+
addSubview(colView)
136+
}
137+
138+
required init?(coder: NSCoder) {
139+
fatalError("init(coder:) has not been implemented")
140+
}
141+
142+
static func creatCol(frame:CGRect,cellIdentifier:String) -> UICollectionView {
143+
let rowCount = 5
144+
let viewWidth = frame.width
145+
let interitemSpacing:CGFloat = 22
146+
147+
let contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
148+
let itemSizeWidth:CGFloat = (viewWidth - CGFloat(rowCount-1) * interitemSpacing - contentInset.left - contentInset.right)/CGFloat(rowCount)
149+
let layout = UICollectionViewFlowLayout()
150+
layout.itemSize = CGSize(width: itemSizeWidth, height: itemSizeWidth)
151+
layout.minimumInteritemSpacing = interitemSpacing
152+
layout.minimumLineSpacing = 22
153+
154+
let colView = UICollectionView(frame: frame, collectionViewLayout: layout)
155+
colView.backgroundColor = UIColor.clear
156+
colView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: cellIdentifier)
157+
colView.contentInset = contentInset
158+
return colView
159+
}
160+
161+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
162+
return 28
163+
}
164+
165+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
166+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
167+
cell.contentView.backgroundColor = randomColor
168+
return cell
169+
}
170+
}

LLSegmentViewController/Example/EmoticonViewController.swift

Lines changed: 2 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -15,155 +15,8 @@ class EmoticonViewController: UIViewController {
1515

1616
let screenW = UIScreen.main.bounds.size.width
1717
let screenH = UIScreen.main.bounds.size.height
18-
let height:CGFloat = 300
19-
let emoticonView = EmoticonView.init(frame: CGRect.init(x: 0, y: screenH - height, width: screenW, height: height))
18+
let height:CGFloat = 400
19+
let emoticonView = EmoticonView.init(frame: CGRect.init(x: 0, y: screenH - height - mSafeBottomMargin(), width: screenW, height: height))
2020
view.addSubview(emoticonView)
21-
emoticonView.backgroundColor = UIColor.red
22-
}
23-
}
24-
25-
26-
class EmoticonView: UIView {
27-
let segCtlView:LLSegmentedControl
28-
var colView:UICollectionView!
29-
var items = [LLSegmentItemTitleImageTabBarItem]()
30-
31-
var indexToList = [Int:UIView]()
32-
override init(frame: CGRect) {
33-
let datas:[(title:String,imageStr:String,style:LLTitleImageButtonStyle)] = [
34-
("螃蟹","watermelon",LLTitleImageButtonStyle.titleEmty),
35-
("麻辣小龙虾","lobster",LLTitleImageButtonStyle.titleEmty),
36-
("苹果","grape", LLTitleImageButtonStyle.titleEmty),
37-
("营养胡萝卜","crab",LLTitleImageButtonStyle.titleEmty),
38-
("葡萄","carrot",LLTitleImageButtonStyle.titleEmty),
39-
("美味西瓜","apple",LLTitleImageButtonStyle.titleEmty),]
40-
for (title,imageStr, diyStyle) in datas {
41-
let tabBarItem = LLSegmentItemTitleImageTabBarItem.init(
42-
title: title,
43-
image: UIImage(named: imageStr),
44-
selectedImage: UIImage(named: imageStr + "_selected"))
45-
46-
tabBarItem.style = diyStyle
47-
tabBarItem.setImageBlock = { (imageView,isSelected,tabBarItem) in
48-
imageView.image = isSelected ? tabBarItem.selectedImage ?? tabBarItem.image : tabBarItem.image
49-
}
50-
items.append(tabBarItem)
51-
}
52-
let segFrame = CGRect.init(x: 0, y: 0, width: frame.width, height: 40)
53-
segCtlView = LLSegmentedControl.init(frame: segFrame, tabBarItems: items)
54-
55-
let colFrame = CGRect.init(x: 0, y: segFrame.maxY, width: frame.width, height: frame.height - segFrame.height)
56-
super.init(frame: frame)
57-
58-
segCtlView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
59-
segCtlView.indicatorView.shapeStyle = .background(color: UIColor.red, img: nil)
60-
let titleViewStyle = LLSegmentItemTitleViewStyle()
61-
var ctlViewStyle = LLSegmentedControlStyle()
62-
ctlViewStyle.segmentItemViewClass = LLSegmentItemTitleImageView.self
63-
ctlViewStyle.itemViewStyle = titleViewStyle
64-
segCtlView.reloadData(ctlViewStyle: ctlViewStyle)
65-
addSubview(segCtlView)
66-
segCtlView.reloadData()
67-
68-
colView = creatCol(frame: colFrame)
69-
addSubview(colView)
70-
segCtlView.associateScrollerView = colView
71-
}
72-
73-
required init?(coder: NSCoder) {
74-
fatalError("init(coder:) has not been implemented")
75-
}
76-
}
77-
78-
extension EmoticonView: UICollectionViewDelegate,UICollectionViewDataSource {
79-
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
80-
return items.count
81-
}
82-
83-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
84-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
85-
cell.contentView.backgroundColor = UIColor.init(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
86-
cell.contentView.viewWithTag(100)?.removeFromSuperview()
87-
if let view = indexToList[indexPath.row] {
88-
view.frame = cell.contentView.bounds
89-
cell.contentView.addSubview(view)
90-
}else{
91-
let newView = EmoticonListView.init(frame: cell.contentView.bounds)
92-
indexToList[indexPath.row] = newView
93-
newView.frame = cell.contentView.bounds
94-
cell.contentView.addSubview(newView)
95-
newView.tag = 100
96-
}
97-
return cell
98-
}
99-
100-
func creatCol(frame:CGRect) -> UICollectionView {
101-
let viewWidth = self.bounds.width
102-
let viewHeight = self.bounds.height
103-
104-
let interitemSpacing:CGFloat = 22
105-
106-
let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
107-
let itemSizeWidth:CGFloat = viewWidth
108-
let layout = UICollectionViewFlowLayout()
109-
layout.itemSize = CGSize(width: itemSizeWidth, height: viewHeight)
110-
layout.minimumInteritemSpacing = interitemSpacing
111-
layout.minimumLineSpacing = 0
112-
layout.scrollDirection = .horizontal
113-
114-
let colView = UICollectionView(frame: frame, collectionViewLayout: layout)
115-
colView.delegate = self
116-
colView.dataSource = self
117-
colView.isPagingEnabled = true
118-
119-
colView.backgroundColor = UIColor.clear
120-
colView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
121-
colView.contentInset = contentInset
122-
return colView
123-
}
124-
}
125-
126-
127-
class EmoticonListView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {
128-
var colView:UICollectionView!
129-
override init(frame: CGRect) {
130-
super.init(frame: frame)
131-
colView = creatCol(frame: frame)
132-
addSubview(colView)
133-
}
134-
135-
required init?(coder: NSCoder) {
136-
fatalError("init(coder:) has not been implemented")
137-
}
138-
139-
func creatCol(frame:CGRect) -> UICollectionView {
140-
let rowCount = 4
141-
let viewWidth = self.bounds.width
142-
let interitemSpacing:CGFloat = 22
143-
144-
let contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
145-
let itemSizeWidth:CGFloat = (viewWidth - CGFloat(rowCount-1) * interitemSpacing - contentInset.left - contentInset.right)/CGFloat(rowCount)
146-
let layout = UICollectionViewFlowLayout()
147-
layout.itemSize = CGSize(width: itemSizeWidth, height: itemSizeWidth)
148-
layout.minimumInteritemSpacing = interitemSpacing
149-
layout.minimumLineSpacing = 33
150-
151-
let colView = UICollectionView(frame: frame, collectionViewLayout: layout)
152-
colView.delegate = self
153-
colView.dataSource = self
154-
colView.backgroundColor = UIColor.clear
155-
colView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
156-
colView.contentInset = contentInset
157-
return colView
158-
}
159-
160-
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
161-
return 20
162-
}
163-
164-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
165-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
166-
cell.contentView.backgroundColor = .blue
167-
return cell
16821
}
16922
}

0 commit comments

Comments
 (0)