我们说我有多个滚动视图,我希望在任何给定时间只能打开其中一个(scrollView.contentOffset != 0
)。因此,当scroll view 1
处于打开状态且滚动scroll view 2
时,scroll view 1
应该动画关闭。当我快速打开scroll view 1
和scroll view 2
时会出现此问题;当发生这种情况时,scroll view 1
仍然在减速并且scroll view 2
开启会尝试强制scroll view 1
关闭并打破动画,有时不会关闭。
解决此问题的一种方法是禁用滚动,直到滚动视图完成减速。但是,我正在寻找能够更加无缝地处理这个问题的其他解决方案。
我试过了scrollView.layer.removeAllAnimations()
。这并不能阻止减速动画。我希望scroll view 1
的动画在滚动scroll view 2
后立即停止,然后scroll view 1
可以设置为contentOffset = 0
。
确定
scroll view
是打开还是关闭。
func scrollViewDidScroll(scrollView: UIScrollView) {
scrollView.bounces = scrollView.contentOffset.x > 20
if scrollView.contentOffset.x < self.lastContentOffset && scrollView.contentOffset.x < 70 {
ifOpening = false
}
else if scrollView.contentOffset.x > 5 {
ifOpening = true
}
self.lastContentOffset = scrollView.contentOffset.x
}
拖动
scroll view
后,关闭所有其他scroll view
。
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
for eachScrollView in taskArrayScrollView{
if eachScrollView != scrollView {
eachScrollView.layer.removeAllAnimations()
UIView.animateWithDuration(0.2,
delay: 0.5,
options: .CurveEaseIn ,
animations: { eachScrollView.contentOffset.x = 0 },
completion: nil
)
}
}
}
将
对齐scroll view
与offset
0或70
func scrollViewDidEndDragging(scrollViewa: UIScrollView, willDecelerate decelerate: Bool) {
let intialContentOffset = scrollViewa.contentOffset.x
var originalContentOffset: CGFloat
var finalContentOffset: CGFloat
var minOffset: CGFloat
if intialContentOffset == boundOfScreen.width{
return
}
if ifOpening{
minOffset = 20.0
originalContentOffset = 0
finalContentOffset = 70
}
else {
minOffset = 50.0
originalContentOffset = 0
finalContentOffset = 70
}
if (!decelerate){
if intialContentOffset < minOffset {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: { scrollViewa.contentOffset.x = originalContentOffset },
completion: {
finished in scrollViewa.userInteractionEnabled = true
}
)
}
else {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: { scrollViewa.contentOffset.x = finalContentOffset },
completion: {
finished in scrollViewa.userInteractionEnabled = true
}
)
}
}
}
func scrollViewDidEndDecelerating(scrollViewb: UIScrollView) {
let intialContentOffset = scrollViewb.contentOffset.x
var originalContentOffset: CGFloat
var finalContentOffset: CGFloat
var minOffset: CGFloat
//println("intialContentOffset: \(intialContentOffset)")
if ifOpening{
minOffset = 20.0
originalContentOffset = 0
finalContentOffset = 70
//println("dec: opening")
}
else {
minOffset = 50.0
originalContentOffset = 0
finalContentOffset = 70
//println("dec: closing")
}
if intialContentOffset < minOffset {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: {scrollViewb.contentOffset.x = originalContentOffset },
completion: {
finished in scrollViewb.userInteractionEnabled = true
}
)
}
else {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: {scrollViewb.contentOffset.x = finalContentOffset },
completion: {
finished in scrollViewb.userInteractionEnabled = true
}
)
}
}
答案 0 :(得分:0)
嗯......我找到了一个解决方法。我使用变量scroll view
来跟踪是否打开了另一个scroll view
,如果是,则在完成动画后关闭当前动画func scrollViewDidScroll(scrollView: UIScrollView) {
scrollView.bounces = scrollView.contentOffset.x > 20
if scrollView.contentOffset.x < self.lastContentOffset && scrollView.contentOffset.x < 70 {
isOpening = false
}
else if scrollView.contentOffset.x > 5 {
isOpening = true
viewOpenCounter = true
}
self.lastContentOffset = scrollView.contentOffset.x
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
if !viewOpenCounter {
for eachScrollView in taskArrayScrollView {
if eachScrollView != scrollView {
UIView.animateWithDuration(0.2,
delay: 0.5,
options: .CurveEaseIn ,
animations: { eachScrollView.contentOffset.x = 0 },
completion: nil
)}
}
}
}
func scrollViewDidEndDragging(scrollViewa: UIScrollView, willDecelerate decelerate: Bool) {
viewOpenCounter = false
let intialContentOffset = scrollViewa.contentOffset.x
var originalContentOffset: CGFloat
var finalContentOffset: CGFloat
var minOffset: CGFloat
if intialContentOffset == boundOfScreen.width{
return
}
if isOpening{
minOffset = 20.0
originalContentOffset = 0
finalContentOffset = 70
}
else {
minOffset = 50.0
originalContentOffset = 0
finalContentOffset = 70
}
if (!decelerate){
//println("no dec \(intialContentOffset)")
if intialContentOffset < minOffset {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: { scrollViewa.contentOffset.x = originalContentOffset },
completion: {
finished in scrollViewa.userInteractionEnabled = true
if self.viewOpenCounter {
UIView.animateWithDuration(0.2, animations: {
scrollViewa.contentOffset.x = 0
},
completion: {
finished in
self.viewOpenCounter = false
}
)
}
}
)
}
else {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: { scrollViewa.contentOffset.x = finalContentOffset },
completion: {
finished in scrollViewa.userInteractionEnabled = true
if self.viewOpenCounter {
UIView.animateWithDuration(0.2, animations: {
scrollViewa.contentOffset.x = 0
},
completion: {
finished in
self.viewOpenCounter = false
}
)
}
}
)
}
}
}
func scrollViewDidEndDecelerating(scrollViewb: UIScrollView) {
viewOpenCounter = false
let intialContentOffset = scrollViewb.contentOffset.x
var originalContentOffset: CGFloat
var finalContentOffset: CGFloat
var minOffset: CGFloat
//println("intialContentOffset: \(intialContentOffset)")
if isOpening{
minOffset = 20.0
originalContentOffset = 0
finalContentOffset = 70
//println("dec: opening")
}
else {
minOffset = 50.0
originalContentOffset = 0
finalContentOffset = 70
//println("dec: closing")
}
if intialContentOffset < minOffset {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: {scrollViewb.contentOffset.x = originalContentOffset },
completion: {
finished in scrollViewb.userInteractionEnabled = true
if self.viewOpenCounter {
UIView.animateWithDuration(0.2, animations: {
scrollViewb.contentOffset.x = 0
},
completion: {
finished in
self.viewOpenCounter = false
}
)
}
}
)
}
else {
UIView.animateWithDuration(0.2,
delay: 0.1,
options: .CurveEaseIn ,
animations: {scrollViewb.contentOffset.x = finalContentOffset },
completion: {
finished in scrollViewb.userInteractionEnabled = true
if self.viewOpenCounter {
UIView.animateWithDuration(0.2, animations: {
scrollViewb.contentOffset.x = 0
},
completion: {
finished in
self.viewOpenCounter = false
}
)
}
}
)
}
}
。不确定这是否有效。
{{1}}