UIView轮播Xcode

时间:2018-09-21 14:20:21

标签: ios iphone swift xcode

我对Xcode还是很陌生。我看过一个水平旋转木马的示例(请参见下面的屏幕截图),该示例允许在不同的视图中滑动。红色框表示随视图一起移动的另一张幻灯片。我很困惑实现这些功能需要哪些元素。请建议我该怎么做

enter image description here

1 个答案:

答案 0 :(得分:0)

这是我的方法:

想法是使用UIPickerView并将其旋转90度。

var pickerView: UIPickerView!
@IBOutlet weak var pickerViewContainer: UIView!
var dataHolder = ["...", "July", "August", "This Month", "..."]

override func viewDidLoad() {
    super.viewDidLoad()
    // Initialization code
    pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 339, height: 60))

    pickerView.delegate = self
    pickerView.dataSource = self

    pickerView.transform = CGAffineTransform(rotationAngle: 90 * (.pi / 180))
    pickerView.frame = CGRect(x: -60, y: 0, width: pickerViewContainer.frame.width+120, height: pickerViewContainer.frame.height)

    pickerViewContainer.addSubview(pickerView)
}

有了它,您可以水平滚动。

现在您所需要做的就是将行的视图旋转回90度。

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    let view = UIView(frame: CGRect(x: 0, y: 5, width: 50, height: 60))
    view.transform = CGAffineTransform(rotationAngle: (-90 * (.pi / 180)))

    let label = UILabel(frame: view.frame)
    label.text = dataHolder[row]
    label.textAlignment = .center
    view.addSubview(label)

    return view
}

确保您需要实现所有其他数据源和委托方法,例如

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    print(dataHolder[row])
}

别忘了

  

UIPickerViewDelegate,UIPickerViewDataSource

然后您要做的就是将UIView添加到故事板中的viewController中,将选择器放置在其中,并将其与@IBOutlet weak var pickerViewContainer: UIView!

连接

也许您需要使用代码中的帧编号来很好地显示它并根据需要调整帧。