简体   繁体   English

Swift(iOS)将UIImage数组插入子视图?

[英]Swift(iOS) inserting UIImage array to subview?

Swift(iOS) problem: how can I insert UIImage array to subview? Swift(iOS)问题:如何将UIImage数组插入子视图? I wanted to make a horizontal scroll view in view controller with very little experience with swift. 我想在视图控制器中制作水平滚动视图,而很少有使用Swift的经验。 Here is the code: 这是代码:

override func viewDidLoad() {
    super.viewDidLoad()

    configurePageControl()

    var pictures:[UIImage] = [ ]
    pictures.append(UIImage(named: "SOVERMENT1.jpg")!)
    pictures.append(UIImage(named: "SOVERMENT2.jpg")!)
    pictures.append(UIImage(named: "SOVERMENT3.jpg")!)
    pictures.append(UIImage(named: "SOVERMENT4.jpg")!)



    scrollView.delegate = self
    self.view.addSubview(scrollView)
    for index in 0..<4 {

        frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
        frame.size = self.scrollView.frame.size
        self.scrollView.pagingEnabled = true

        var subView = UIView(frame: frame)
        **subView.backgroundColor = [UIColor colorWithPatternImage : ]**
        self.scrollView.addSubview(subView)
    }

This is first problem. 这是第一个问题。 How can I insert pictures into subview.backgroundColor? 如何将图片插入subview.backgroundColor? I searched it for 1~2hours, but I couldn't find any answer. 我搜索了1到2个小时,但找不到任何答案。

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4, self.scrollView.frame.size.height)
    pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged)


}


func configurePageControl() {


   ** self.pageControl.numberOfPages = pictures.count **
    self.pageControl.currentPage = 0

And here. 和这里。 Why array pictures cannot be counted? 为什么无法计算阵列图片?

Unless these are repeatable pattern images, I think you want to use a UIImageView. 除非这些是可重复的图案图像,否则我认为您想使用UIImageView。 I also don't think you need to use a UIPageControl, because you're only using one UIViewController. 我也不认为您需要使用UIPageControl,因为您只使用一个UIViewController。 You can say something like: 您可以这样说:

    override func viewDidLoad() {
        super.viewDidLoad()


        var pictures:[UIImage] = [ ]
        pictures.append(UIImage(named: "SOVERMENT1.jpg")!)
        pictures.append(UIImage(named: "SOVERMENT2.jpg")!)
        pictures.append(UIImage(named: "SOVERMENT3.jpg")!)
        pictures.append(UIImage(named: "SOVERMENT4.jpg")!)



        self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(pictures.count), scrollView.frame.size.height)
        for index in 0..<pictures.count {
            var frame = CGRectZero
            frame.origin.x = scrollView.frame.size.width * CGFloat(index)
            frame.size = scrollView.frame.size
            scrollView.pagingEnabled = true

            var subView = UIImageView(frame: frame)
            subView.image = pictures[index]
            subView.contentMode = .ScaleAspectFit
            self.scrollView.addSubview(subView)
        }



    }

您最好将UICollectionView用于此类目的

For setting the backgroundColor call the UIColor initializer: 要设置backgroundColor,请调用UIColor初始化程序:

for picture in pictures {
    subView.backgroundColor = UIColor(patternImage: picture)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM