swift - scrollview 判断左右移动, 以及上下两个view联动

核心代码
1.

2.

3.
界面代码VFL
/* 浏览作品view*/ import UIKit /**
* 图片浏览器(大图和缩略图)
*/
class JYBrowseWorksView: UIView { /// 数据源参数(外界传入)
var dataArray:[WorkImagesProtocol] = [] {
didSet{
self.topCollectionView.reloadData()
self.bottomCollectionView.reloadData()
if dataArray.isEmpty {
self.topCollectionView.backgroundView = self.emptyView
bottomCollectHeight?.constant = 0
self.topCollectionView.backgroundColor = UIColor(hexColor: "F9F9F9")
}else{
self.topCollectionView.backgroundView = nil
bottomCollectHeight?.constant = 70
self.topCollectionView.backgroundColor = UIColor.white
}
}
}
/// 照片比例类型(默认1:1)
var photoSizeType: JYMyCenterPhotoHeightType = .squareType {
didSet{
self.collectionHeightConstraint?.constant = photoSizeType.photoHeight
if photoSizeType == .rectangleType {
self.collectionMegrainConstraint?.constant = 72
}else {
self.collectionMegrainConstraint?.constant = 24
}
}
}
/// 当前显示的图片索引
private var selectIndex:Int = 0 {
didSet{
self.reloadCollectionUI()
}
}
/// 空页面
private let emptyView = JYZDEmptyView(emptyAreement: JYEmptyViewStruct(emptyType: .noImageEmptyType, offsetY: -70, titleTipStr: "暂无作品", buttonTitleStr: nil))
/// 底部相册的高度约束
private var bottomCollectHeight: NSLayoutConstraint?
/// 可分页滑动collectionView
private lazy var topCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: JY_DEVICE_WIDTH, height: self.photoSizeType.photoHeight)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = UIColor.white
collectionView.isPagingEnabled = true
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
return collectionView
}() /// 可选择点中的collectionview
private var bottomCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 70, height: 70)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = UIColor.white
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
return collectionView
}() /// topcollectionView 开始滑动时的位移
private var startContentOffsetX : CGFloat = 0 /// topcollectionView 将要停止滑动时的位移
private var willEndContentOffsetX : CGFloat = 0 /// 控制高度的约束
private var collectionHeightConstraint: NSLayoutConstraint?
/// 控制距离的约束
private var collectionMegrainConstraint: NSLayoutConstraint? override init(frame: CGRect) {
super.init(frame: frame)
self.translatesAutoresizingMaskIntoConstraints = false
self.configerUI()
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} /// 刷新collectionview的UI
private func reloadCollectionUI() { let endContentOffsetX = topCollectionView.contentOffset.x
if endContentOffsetX < willEndContentOffsetX , willEndContentOffsetX < startContentOffsetX {
DDLOG(message: "左移")
if !bottomCollectionView.indexPathsForVisibleItems.contains(IndexPath(item: selectIndex, section: 0)) {
bottomCollectionView.selectItem(at: IndexPath(item: selectIndex, section: 0), animated: true, scrollPosition: UICollectionView.ScrollPosition.left)
}
} else if endContentOffsetX > willEndContentOffsetX , willEndContentOffsetX > startContentOffsetX {
DDLOG(message: "右移")
if !bottomCollectionView.indexPathsForVisibleItems.contains(IndexPath(item: selectIndex, section: 0)) {
bottomCollectionView.selectItem(at: IndexPath(item: selectIndex, section: 0), animated: true, scrollPosition: UICollectionView.ScrollPosition.right)
}
}
self.bottomCollectionView.reloadData()
}
} // MARK: - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
extension JYBrowseWorksView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "JYBrowseWorksCollectionCell", for: indexPath) as! JYBrowseWorksCollectionCell
cell.configerCellData(imageData: dataArray[indexPath.row])
if collectionView == bottomCollectionView {
let select = selectIndex == indexPath.row ? true : false
cell.configerSelectCell(isSelect: select)
}else {
// cell.imageView.contentMode = .scaleAspectFit
// cell.clipsToBounds = false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
if collectionView == topCollectionView {
return 0.0001
}
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if collectionView == topCollectionView {
return 0
}
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if collectionView == topCollectionView {
return UIEdgeInsets.zero
}
return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if collectionView == bottomCollectionView {
selectIndex = indexPath.row
topCollectionView.scrollToItem(at: IndexPath(item: indexPath.row, section: 0), at: UICollectionView.ScrollPosition.left, animated: true)
}
} /// 滑动顶部collectionview,底部collectionview显示顶部当前显示的图片
///
/// - Parameter scrollView: scrollView description
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == topCollectionView {
let x = scrollView.contentOffset.x
let i:Int = Int(x/UIScreen.main.bounds.size.width)
self.selectIndex = i
}
} /// 获取将要滑动时位移
/// 用于判断滑动方向
/// - Parameter scrollView: scrollView description
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView == topCollectionView {
startContentOffsetX = scrollView.contentOffset.x
}
} /// 获取将要结束时 topCollectionView 的位移
/// 用于判断 滑动方向
/// - Parameters:
/// - scrollView: scrollView description
/// - velocity: velocity description
/// - targetContentOffset: targetContentOffset description
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView == topCollectionView {
willEndContentOffsetX = scrollView.contentOffset.x
}
}
} // MARK: - UI布局
extension JYBrowseWorksView { private func configerUI() { topCollectionView.delegate = self
topCollectionView.dataSource = self
bottomCollectionView.delegate = self
bottomCollectionView.dataSource = self topCollectionView.register(JYBrowseWorksCollectionCell.classForCoder(), forCellWithReuseIdentifier: "JYBrowseWorksCollectionCell")
bottomCollectionView.register(JYBrowseWorksCollectionCell.classForCoder(), forCellWithReuseIdentifier: "JYBrowseWorksCollectionCell") self.addSubview(topCollectionView)
self.addSubview(bottomCollectionView) let vd:[String:UIView] = ["topCollectionView":topCollectionView,"bottomCollectionView":bottomCollectionView]
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|[topCollectionView]|", options: [], metrics: nil, views: vd))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|[bottomCollectionView]|", options: [], metrics: nil, views: vd))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[topCollectionView]", options: [], metrics: nil, views: vd))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[bottomCollectionView]|", options: [], metrics: nil, views: vd))
self.collectionHeightConstraint = topCollectionView.heightAnchor.constraint(equalToConstant: JY_DEVICE_WIDTH)
self.collectionHeightConstraint?.isActive = true
self.collectionMegrainConstraint = bottomCollectionView.topAnchor.constraint(equalTo: topCollectionView.bottomAnchor, constant: 24)
self.collectionMegrainConstraint?.isActive = true
bottomCollectHeight = bottomCollectionView.heightAnchor.constraint(equalToConstant: 70)
bottomCollectHeight?.isActive = true }
}
swift - scrollview 判断左右移动, 以及上下两个view联动的更多相关文章
- iOS开发——设备篇Swift篇&判断设备类型
判断设备类型 1,分割视图控制器(UISplitViewController) 在iPhone应用中,使用导航控制器由上一层界面进入下一层界面. 但iPad屏幕较大,通常使用SplitViewCo ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- jQuery实现两个DropDownList联动(MVC)
近段时间原本是学习MVC的,谁知道把jQuery也学上了.而且觉得对jQuery更感兴趣,比如今早上有写了一个练习<jQuery实现DropDownList(MVC)>http://www ...
- 实现外卖选餐时两级 tableView 联动效果
最近实现了下饿了么中选餐时两级tableView联动效果,先上效果图,大家感受一下: 下面说下具体实现步骤: 首先分解一下,实现这个需求主要是两点,一是点击左边tableView,同时滚动右边tabl ...
- MVC编辑状态两个DropDownList联动
前几天使用jQuery在MVC应用程序中,实现了<jQuery实现两个DropDownList联动(MVC)>http://www.cnblogs.com/insus/p/3414480. ...
- GridView中两个DropDownList联动
GridView中两个DropDownList联动 http://www.cnblogs.com/qfb620/archive/2011/05/25/2057163.html Html: <as ...
- ios中两个view动画切换
@interface ViewController () @property(nonatomic,retain)UIView *redview; @property(nonatomic,retain) ...
- iOS 类似外卖 两个tableView联动
在伯乐在线上看到一个挺好玩的文章,自己也参考文章实现了一下. 效果实现如图所示: 具体实现的内容可以参考原文,参考文章:<iOS 类似美团外卖 app 两个 tableView 联动效果实现&g ...
- js判断字符长度 汉字算两个字符
方法一:使用正则表达式,代码如下: function getByteLen(val) { var len = 0; for (var i = 0; i < val.length; i++) { ...
随机推荐
- Leetcode 题解 reverse List II
这个题确实太容易错了. 我已经做了2遍了,之前都是套用reverse List 1中的函数. 现在尝试用新方法,在一个函数里完成,结果又错了. 事实证明,永远不要想当然!!!白板编程真的是要求,你对每 ...
- CSS VISUAL RULES
CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...
- ArcGIS特殊标注效果的简单实现
1. 普通纯色背景:例如望仙亭,水垄沟: 方法: 2. 背景+边框 例如进入点 方法:
- DOM精简版笔记
1.1. 基本概念 1.1.1. DOM DOM Document Object Model 文档对象模型 就是把HTML文档模型化,当作对象来处理 DOM提供的一系列属性和方法可以 ...
- (转)如何禁用Windows 10系统的触摸屏
https://baijiahao.baidu.com/s?id=1593890738706748667 现在许多优质的Windows 10个人电脑都配备了触摸屏,因为触摸屏的日益普及,Windows ...
- Windows Server 2012开启磁盘性能计数器
Windows Server 2012默认情况下已经禁用了磁盘性能计数器,打开任务管理器后,无法像Win8一样在性能选项卡中看到“磁盘”使用情况,可能是因为微软考虑到安装此服务器系统的硬件都会非常好, ...
- 有关于分布式和SOA的理解
我的理解分布式和SOA都差不多,类似功能独立分开.举个例子,做一辆车,按照传统模式,先生产车架,然后生产车轮..然后一辆车完成.现在分布式就是生产车架与生产车轮分离,所有的材料 就是最后一次组装的时候 ...
- kafka清理
由于项目原因,最近经常碰到Kafka消息队列拥堵的情况.碰到这种情况为了不影响在线系统的正常使用,需要大家手动的清理Kafka Log.但是清理Kafka Log又不能单纯的去删除中间环节产生的日志, ...
- 对于“2017面向对象程序设计(Java)第五周工作总结”存在问题的反馈及本周教学计划
一:问题反馈 “上周我们学习的新内容主要是第五章,并对第四章内容做了巩固.从学生上交的实验报告完成情况以及学习Java心得博客中的反馈可以看出,学生对构造器.重载.超类.多态.抽象类这几个概念理解的不 ...
- 2017面向对象程序设计(Java) 第4周学习指导及要求(2017.9.14-2017.9.18)
学习目标 深入理解程序设计中算法与程序的关系: 深入理解java程序设计中类与对象的关系: 理解OO程序设计的第一个特征:封装: 需要掌握基本使用方法的预定义类有:Math类.String类.Arra ...