如何在View Controller中创建文本水平滚动的部分?

时间:2019-01-09 02:24:26

标签: swift xcode text scrollview

我想在我的视图控制器中实现一个部分,使文本从左到右滚动。

我找到了如何使用图片(图片文字)进行操作,但我希望能够拥有一串文本,而不是拥有一串图片

class ViewController: UIViewController, UIScrollViewDelegate {

    // SCROLL VIEW CONTROLLER
    @IBOutlet weak var pagecontrol: UIPageControl!
    @IBOutlet weak var scrollview: UIScrollView!

    var images: [String] = ["image1","image2","image3","image4","image5"]
    var frame = CGRect (x:0,y:0, width:0,height:0)

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.

        pagecontrol.numberOfPages = images.count
        for index in 0..<images.count {
            frame.origin.x = scrollview.frame.size.width
                * CGFloat(index)
            frame.size = scrollview.frame.size

            let imgView = UIImageView(frame: frame)
            imgView.image = UIImage(named: images[index])
            self.scrollview.addSubview(imgView)
        }
        scrollview.contentSize = CGSize(width:
            (scrollview.frame.size.width *
                CGFloat(images.count)), height:
            scrollview.frame.size.height)
        scrollview.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //Scrollview Method
    // =============================
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pagenumber = scrollView.contentOffset.x / scrollview.frame.size.width
        pagecontrol.currentPage = Int(pagenumber)
    }
}

现在,我有了带文本的图像,而不是字符串中的文本。我希望只包含一个文本字符串,以便可以在代码中编辑文本。

谢谢您的帮助。

0 个答案:

没有答案