Ios 设置UITableView高度的动画
我试图在集合视图滚动时设置tableviews高度的动画。我从ScrollViewWillBeginDraging调用动画函数。然后我执行一个动画块:Ios 设置UITableView高度的动画,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在集合视图滚动时设置tableviews高度的动画。我从ScrollViewWillBeginDraging调用动画函数。然后我执行一个动画块: func scrollViewWillBeginDragging(scrollView: UIScrollView) { if expanded == false && scrollView == IBcalendarCollectionView { expanded = true expa
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
if expanded == false && scrollView == IBcalendarCollectionView {
expanded = true
expandCollectionView()
}
}
func expandCollectionView() {
let screenSize: CGRect = UIScreen.mainScreen().bounds
UIView.animateWithDuration(3, delay: 0, options: UIViewAnimationOptions.TransitionNone, animations: { () -> Void in
self.IBcalendarTableView.frame = CGRectMake(self.IBcalendarTableView.frame.origin.x, screenSize.height/2, self.IBcalendarTableView.frame.width, screenSize.height/2)
}) { (success) -> Void in
print(self.IBcalendarTableView.frame.origin.y)
}
}
发生的事情是,TableView设置动画直到状态栏,然后返回到其原始位置。我希望它的动画缩小到屏幕的一半大小。动画块只执行一次,因为我在调用动画函数时设置了布尔值
您说在调用动画块时设置了布尔值,这样动画就不会一次又一次地被调用了吗?我在你的代码中也没有看到 这应该行得通
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
let mainScreen = UIScreen.mainScreen().bounds
if (self.IBcalendarTableView.frame.origin.y != mainScreen.height / 2) {
var frame = self.IBcalendarTableView.frame
frame.origin.y = mainScreen.height / 2
frame.size.height = frame.origin.y
UIView.animateWithDuration(0.5) { () -> Void in
self.IBcalendarTableView.frame = frame
}
}
}
我设置了一个较低的动画速度,3秒似乎很多。。。我也以不同的方式构建了框架,我这么做只是因为我发现它更容易阅读
如果还有什么我能帮忙的,请告诉我 您说在调用动画块时设置了布尔值,这样动画就不会被一次又一次调用了吗?我在你的代码中也没有看到 这应该行得通
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
let mainScreen = UIScreen.mainScreen().bounds
if (self.IBcalendarTableView.frame.origin.y != mainScreen.height / 2) {
var frame = self.IBcalendarTableView.frame
frame.origin.y = mainScreen.height / 2
frame.size.height = frame.origin.y
UIView.animateWithDuration(0.5) { () -> Void in
self.IBcalendarTableView.frame = frame
}
}
}
我设置了一个较低的动画速度,3秒似乎很多。。。我也以不同的方式构建了框架,我这么做只是因为我发现它更容易阅读
如果还有什么我能帮忙的,请告诉我 问题在于自动布局。还必须设置tableview的高度约束,以便将tableview保持在新的高度/原点 对于可能存在此问题的任何其他人: 这个答案帮助我解决了这个问题。我的固定代码如下所示:
func expandCollectionView() {
let screenSize: CGRect = UIScreen.mainScreen().bounds
var frame = self.IBcalendarTableView.frame
frame.origin.y = screenSize.height / 2
frame.size.height = frame.origin.y
UIView.animateWithDuration(0.5) { () -> Void in
self.IBcalendarTableView.frame = frame
self.IBtableViewHeightConstraint.constant = screenSize.height/2
}
}
问题在于自动布局。还必须设置tableview的高度约束,以便将tableview保持在新的高度/原点 对于可能存在此问题的任何其他人: 这个答案帮助我解决了这个问题。我的固定代码如下所示:
func expandCollectionView() {
let screenSize: CGRect = UIScreen.mainScreen().bounds
var frame = self.IBcalendarTableView.frame
frame.origin.y = screenSize.height / 2
frame.size.height = frame.origin.y
UIView.animateWithDuration(0.5) { () -> Void in
self.IBcalendarTableView.frame = frame
self.IBtableViewHeightConstraint.constant = screenSize.height/2
}
}
谢谢我编辑代码以显示布尔值。还有,我在3秒的时候拍的,这样我就可以看到动画中发生了什么。使用您的代码,动画可以工作,但仍然有奇怪的行为。动画完成后,我滚动tableview,tableview将返回其原始高度。你认为这可能是一个自动布局问题吗?我在没有任何约束的情况下测试了它,但仍然存在相同的问题。如果collectionview保持滚动,或者我滚动tableview,tableview将返回到其原始大小/原始值谢谢。我编辑代码以显示布尔值。还有,我在3秒的时候拍的,这样我就可以看到动画中发生了什么。使用您的代码,动画可以工作,但仍然有奇怪的行为。动画完成后,我滚动tableview,tableview将返回其原始高度。你认为这可能是一个自动布局问题吗?我在没有任何约束的情况下测试了它,但仍然存在相同的问题。如果collectionview继续滚动或我滚动tableview,tableview将恢复到原始大小/原点,您可以找到答案。很高兴您能够找到答案。