Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIPickerView确实路过_Ios_Swift_Uiview_Uipickerview_Touch Event - Fatal编程技术网

Ios UIPickerView确实路过

Ios UIPickerView确实路过,ios,swift,uiview,uipickerview,touch-event,Ios,Swift,Uiview,Uipickerview,Touch Event,我有这个演示应用程序: 正如您所看到的,背景的alpha根据该值更改为黑色 但问题是没有平稳过渡: 从GIF中可以看到,只有在滚动结束后背景才会改变。我可不想这样 这是我的代码: 类ViewController:UIViewController、UIPickerViewDeleteGate、UIPickerViewDataSource{ @IBV弱变量pickerView:UIPickerView! @IBVAR后视图:UIView! 设max=100 重写func viewDidLoad(){

我有这个演示应用程序:

正如您所看到的,背景的alpha根据该值更改为黑色

但问题是没有平稳过渡:

从GIF中可以看到,只有在滚动结束后背景才会改变。我可不想这样

这是我的代码:

类ViewController:UIViewController、UIPickerViewDeleteGate、UIPickerViewDataSource{
@IBV弱变量pickerView:UIPickerView!
@IBVAR后视图:UIView!
设max=100
重写func viewDidLoad(){
super.viewDidLoad()
pickerView.delegate=self
pickerView.dataSource=self
}
func numberOfComponents(在pickerView:UIPickerView中)->Int{
返回1
}
func pickerView(pickerView:UIPickerView,numberOfRowsInComponent:Int)->Int{
返回max+1//以包括“0”
}
func pickerView(pickerView:UIPickerView,viewForRow行:Int,forComponent组件:Int,重用视图:UIView?->UIView{
设l=UILabel(帧:.0)
l、 text=字符串(最大值-行)
l、 textColor=.white
l、 font=UIFont.preferredFont(forTextStyle:.标题3)
l、 textAlignment=.center
返回l
}
func pickerView(pickerView:UIPickerView,didSelectRow行:Int,不完整组件:Int){
backView.alpha=CGFloat(最大行)/100
}
}

我给了代表<代码> uiVIEW/COD>而不是String,因为我有一个想法:每次测试每一行的位置都在变化,当行在屏幕中间时,我应该更新背景。但不幸的是,我不知道怎么做

也许你能帮忙?或者提出其他想法


谢谢

只有在滚动停止时才会调用委托方法
didSelectRow
,因此这不是您应该更新alpha的地方
UIPickerView
没有委托方法可以在滚动期间通知您更改,但是
UIPickerView
将调用您的数据源和委托方法来获取标题或给定行的视图,以便在用户滚动时显示。因此,您应该做的是将您的alpa更改逻辑移到此处:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    backView.alpha = CGFloat(max - row) / 100
}
请注意,加载
UIPickerView
时将调用此委托方法,因此您可能应该禁用alpha更改,直到视图布局不正确为止(可能
viewdideappear
将执行此操作)

由于委托方法有时可能会出现意外行为(不仅调用下一行,而且调用选择器中的任何行),因此我们应该存储并检查行是否仅比上次保存的值提前一步或落后一步,否则我们应该忽略这一点。我制作了一个简单的演示来演示它的工作原理:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    private let pickerView = UIPickerView()
    private var isPickerReady = false
    private var lastValue = 0
    private let max = 100

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(pickerView)
        pickerView.translatesAutoresizingMaskIntoConstraints = false

        pickerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        pickerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        pickerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        pickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true


        pickerView.delegate = self
        pickerView.dataSource = self
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        isPickerReady = true
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return max
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        // Do not update the value until the view is not loaded 
        // Only consider delegate methods which are one step ahead or behind the last value
        if row + 1 == lastValue && isPickerReady || row - 1 == lastValue && isPickerReady {
            lastValue = row
            view.alpha = CGFloat(max - lastValue ) / 100
        }
        return "Tiltle \(row)"
    }
}

当我快速滚动时,会产生一个奇怪的结果,当我滚动到0时,它不会完全变黑。我看到了你的编辑,但代码仍然是一样的,即使在我执行了
第1行
或避免在布局子视图@אורי之前更改背景,它也不能正常工作。我更新了我的答案,还包括了一个示例。非常感谢你