我有这个扩展,在Swift中为一个视图添加一个渐变:
extension UIView {
func addGradientWithColor(colorTop: UIColor, colorButton: UIColor){
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [colorTop.cgColor, colorButton.cgColor]
self.layer.insertSublayer(gradient, at: 0)
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我在我的UIViewController中使用它:
override func viewDidLoad(){
self.view.addGradientWithColor(colorTop: UIColor.red, colorButton: UIColor.clear)
super.viewDidLoad()
}
Run Code Online (Sandbox Code Playgroud)
我运行模拟器,效果很好.但是,当我想在真实设备上使用我的应用程序时,Gradient不起作用.
PD:我尝试了许多方法来做一个Gradient,但在真实设备上没有任何效果.
语境:
我正在使用“expo”处理“react-native”,并且我有一个倒置的“FlatList”,它渲染了从一月到十二月的几个月的“数组”。
要求:
在列表显示给用户之前,列表应该集中在当月。
现在的情况:
我正在使用“FlatList”“scrollToOffset”属性来执行此请求,当接收到“数据源”列表时,会自动触发“this.flatList.scrollToOffset”语句,列表动画滚动并聚焦特定项目。但是我有一个问题,这个动画是一个破坏用户体验的突然动作。
需要:
你能帮我找到这个问题的解决方案吗?我需要做同样的事情,但用户看不到滚动运动,他只应该看到聚焦的项目。任何的想法?
演示代码:
平单
<FlatList
ref={ref => (this.flatList = ref)}
inverted
data={foods}
keyExtractor={this.keyExtractor}
renderItem={this.renderItem}
/>
Run Code Online (Sandbox Code Playgroud)
聚焦项目的功能
handlePosition(id) {
/* Item Size from FlatList */
const itemSize = deviceWidth / 2 + 30;
/* Id of the current month */
const idCurrentItem = id;
this.flatList.scrollToOffset({
animated: true,
offset: itemSize * idCurrentItem,
});
}
Run Code Online (Sandbox Code Playgroud)
语句 where 是调用 handlePosition 函数
componentDidMount() {
this.handlePosition(this.props.months[2].id);
}
Run Code Online (Sandbox Code Playgroud) gradient ×1
ios ×1
javascript ×1
listview ×1
react-native ×1
reactjs ×1
scrollview ×1
swift ×1
uiview ×1