Swift UICollectionView更新动画
我有一个UICollectionView和一个RESTAPI。我获取数据并将其保存到带有我创建的实体对象的数组中。我有实体对象,它们代表一个用户、两个用户之间的连接和通知(因此用户可以在用户之间建立连接) 我在UICollectionView中展示了我的所有数据,该视图有3个部分用于用户配置文件、通知和他与其他用户的连接。每个都有自己的原型单元和自己的自定义类 当我加载ViewController并开始更新数据(从api获取)时,我会保存一个哈希以跟踪数据是否已更改。如果它已更改为当前显示的数据,我会将新数据对象保存到数据模型中,并在ViewController中调用方法“updateUI() 到目前为止,一切都很好,但我希望我的CollectionView做动画更新 我在互联网上搜索,发现CollectionView已经有了添加和删除单元格的方法。但在我的情况下,我无法跟踪我的数据是如何变化的,我只知道如果哈希值发生了变化,它就发生了变化 因此,当我在“updateUI()”-方法中调用“reloadData()”时,整个CollectionView都会闪烁,更新非常糟糕 如何跟踪数据的更改,或者如何在“updateUI()”-方法中显示到CollectionView时设置数据更改的动画 更新 首先是我的数据处理程序,它保存我的视图的所有数据:Swift UICollectionView更新动画,swift,animation,uicollectionview,Swift,Animation,Uicollectionview,我有一个UICollectionView和一个RESTAPI。我获取数据并将其保存到带有我创建的实体对象的数组中。我有实体对象,它们代表一个用户、两个用户之间的连接和通知(因此用户可以在用户之间建立连接) 我在UICollectionView中展示了我的所有数据,该视图有3个部分用于用户配置文件、通知和他与其他用户的连接。每个都有自己的原型单元和自己的自定义类 当我加载ViewController并开始更新数据(从api获取)时,我会保存一个哈希以跟踪数据是否已更改。如果它已更改为当前显示的数据
class DataHandler {
static var connections : [Connection] = []
static var ownProfile : User?
static var notifications : [Notification] = []
static var connectionsHash : String? = nil
static var ownProfileHash : String? = nil
static var notificationsHash : String? = nil
static var overviewHash : String? = nil
//plus getter and setter for every attribute
}
这就是我如何将api中的json对象存储到datahandler中的方法,例如通知:
var notifications : [Notification] = []
for (_,notification):(String, JSON) in dataObject["notifications"] {
let notificationObj = Notification(json: notification)
notifications.append(notificationObj)
}
DataHandler.setNotifications(notifications)
在我的视图控制器中,我使用DataHandler中的数据设置单元格:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell : UICollectionViewCell!
let notificationCell = collectionView.dequeueReusableCellWithReuseIdentifier("NotificationCell", forIndexPath: indexPath) as! NotificationCell
let ownProfileCell = collectionView.dequeueReusableCellWithReuseIdentifier("OwnProfileCell", forIndexPath: indexPath) as! OwnProfileCell
let overviewUserCell = collectionView.dequeueReusableCellWithReuseIdentifier("OverviewUserCell", forIndexPath: indexPath) as! OverviewUserCell
switch indexPath.section {
case 0:
//setting up cell for notification
//with data from DataHandler
cell = notificationCell
case 1:
//setting up cell for own profile
//with data from DataHandler
cell = ownProfileCell
case 2:
//setting up cell for connected user
//with data from DataHandler
cell = overviewUserCell
default: break
}
return cell
}
我的api的典型响应如下所示:
{
"success": true,
"data": {
"notifications": [
{
"id": 6,
"lastAlarm": 1460493400,
"alarmDelay": 0,
"routine": 20000,
"master": {
"email": "*********",
"id": 12,
"created": 1454117165,
"imageUrl": "http://192.168.2.171:9999/profileimages/4.jpg",
"name": "**********",
"lastReport": 1460857863
},
"active": false
}
],
"connections": [
{
"id": 3,
"slave": {
"email": "*********",
"id": 12,
"created": 1454117165,
"imageUrl": "http://192.168.2.171:9999/profileimages/4.jpg",
"name": "**********",
"lastReport": 1460857863
},
"alarmDelay": 20000,
"lastAlarm": 1460493400,
"routine": 2000,
"active": true
},
{
"id": 4,
"slave": {
"email": "********",
"id": 13,
"created": 1454957407,
"imageUrl": "http://192.168.2.171:9999/profileimages/5.jpg",
"name": "*******",
"lastReport": 0
},
"alarmDelay": 2000,
"lastAlarm": 1460493400,
"routine": 200,
"active": true
}
],
"profile": {
"id": 11,
"shortestInterval": 300,
"imageUrl": "http://192.168.2.171:9999/profileimages/3.jpg",
"email": "********",
"created": 1454117103,
"name": "******",
"lastReport": 1461674777
}
},
"hash": "1c402ff86c8a3f9b7c0b2376d736fe47",
"error": null
}
当我成功地从api接收数据时,我在ViewController中调用updateUI方法:
func updateUI(animated : Bool) {
self.userCollectionView.reloadData()
}
我还为单元格制作了一些动画,但仅通过首次加载:
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if(duration == 0.5) {
cell.layer.opacity = 0.0
UIView.animateWithDuration(duration, animations: {
cell.layer.opacity = 1.0
})
}
}
我想你很难得到一个具体的答案。添加一些代码片段,我认为updateUI和如何检索数据对任何阅读本文的人都会非常有帮助。也许您如何用数据填充集合视图,它是在数组中吗?数组中是否填充了元组?很难知道如何设置集合视图的动画/更改结构,使您能够在没有此类infoReload集合视图的情况下使用GCD asyn handler和uiview来设置动画。动画持续时间它将为您提供平滑的动画,而不是flashing@OlivierWilkinson更新了我的帖子,对不起!别担心,我只是不想让你得不到答案!我想你很难得到一个具体的答案。添加一些代码片段,我认为updateUI和如何检索数据对任何阅读本文的人都会非常有帮助。也许您如何用数据填充集合视图,它是在数组中吗?数组中是否填充了元组?很难知道如何设置集合视图的动画/更改结构,使您能够在没有此类infoReload集合视图的情况下使用GCD asyn handler和uiview来设置动画。动画持续时间它将为您提供平滑的动画,而不是flashing@OlivierWilkinson更新了我的帖子,对不起!别担心,我只是不想让你得不到答案!