我在滚动视图上放置了多个标签,例如:
private func drawDates(_ multiplier: Double){
contentView.subviews.forEach{it in
it.removeFromSuperview()
}
let maximumVisibleDates = 6.0
let dateLabelOffset = Double(UIScreen.main.bounds.size.width / CGFloat(maximumVisibleDates))
let doublePoints = data.findClosestDatePointsForInterval(interval: dateLabelOffset,
multiplier: multiplier)
print(doublePoints)
let datesToShow = data.datesForPoints(points: doublePoints, multiplier: multiplier)
for (index, value) in datesToShow.enumerated(){
if index == datesToShow.count - 1 {
continue
}
let label = UILabel.init(frame: CGRect.init(x: CGFloat(doublePoints[index]),
y: Dimensions.chartHeight.value + Offsets.small.value,
width: Dimensions.dateLabelWidth.value,
height: Dimensions.dateLabelHeight.value))
label.text = redableString(value)
label.font = UIFont.systemFont(ofSize: FontSizes.yLabel.value)
label.textColor = Settings.isLightTheme() ? Colors.LightTheme.textGray.value : Colors.DarkTheme.textGray.value
label.sizeToFit()
contentView.addSubview(label)
}
}
当我打电话时:
contentView.subviews.forEach{it in
it.removeFromSuperview()
}
实际上,我想淡化旧函数调用中的“旧”标签,然后添加动画新标签。该怎么做?
答案 0 :(得分:1)
有3种方法可以删除旧视图并使用动画添加新视图。
解决方案1:
在这种情况下,请在animations
块中使用for循环来设置alpha,在completion
块中使用for循环来删除旧视图。
let viewToAdd = ...
viewToAdd.alpha = 0
contentView.addSubview(viewToAdd)
UIView.animate(withDuration: 1, animations: {
viewToRemove.alpha = 0
viewToAdd.alpha = 1
}, completion: { (_) in
viewToRemove.removeFromSuperview()
})
解决方案2:
调用transition(with:duration:options:animations:completion:)
方法。使用.transitionCrossDissolve
作为options
的值。
在这种情况下,请在animations
块中使用for循环来删除旧视图并添加新视图。
UIView.transition(with: contentView,
duration: 1,
options: .transitionCrossDissolve,
animations: {
viewToRemove.removeFromSuperview()
contentView.addSubview(viewToAdd)
}, completion: nil)
解决方案3:
如果旧视图的数量等于新视图的数量,请调用transition(from:to:duration:options:completion:)
方法。使用.transitionCrossDissolve
作为options
的值。
在这种情况下,请使用for循环调用该方法。
UIView.transition(from: viewToRemove,
to: viewToAdd,
duration: 1,
options: .transitionCrossDissolve,
completion: nil)
答案 1 :(得分:0)