ios - 何时使用 UITouch 与 UIScroll

标签 ios swift

我想实现您在约会应用程序中看到的设计。您可以在其中垂直滚动个人资料的图像,也可以水平滚动以查看列表中的下一个或上一个人员。

目前我已经阐述了我的观点。

Previous-UIView  -      current UIView -    next UIView 
    UIScrollView.           UIScrollView.       UIScrollView
        Images.                 Images.             Images
    UIView.                 UIView.             UIView
        Profile info.           Profile info.       Profile info
    UIPageControl.          UIPageControl       UIPageControl.

只有一个 View 占据主视图,下一个和上一个 View 位于屏幕外。理想情况下,当用户向左移动 View 时,我会以编程方式删除前一个 View ,将前一个 View 设置为当前 View ,将下一个 View 设置为当前 View ,并为下一个 View 添加新 View 。向右移动反之亦然。

水平 ScrollView 的最佳方式是什么?

我应该将它们全部包装在 UIScrollView 中吗?这会干扰 UIScrollView subview 吗?

或者我应该对触摸控件进行编程来移动 View 吗?

或者有更好的方法吗?

我仍然是 iOS 开发的新手,因此我们将不胜感激。

最佳答案

所以我尝试了一些测试应用程序的实验,我很高兴地说您可以在 UIScrollviews 中使用 UIScrollviews。

我能够让它完美运行。下面是我的代码。

    override func viewDidLoad() {
    super.viewDidLoad()

    self.superView.delegate = self

    // Do any additional setup after loading the view, typically from a nib.
    var subImages1 = ["IMG_0004.JPG","IMG_0005.JPG","IMG_0008.JPG"]
    var subImages2 = ["IMG_0009.JPG","IMG_0010.JPG","IMG_0011.JPG"]
    var subImages3 = ["IMG_0013.JPG","IMG_0017.JPG","IMG_0018.JPG"]
    self.images.append(subImages1)
    self.images.append(subImages2)
    self.images.append(subImages3)
    self.superView.frame = self.view.frame
    self.superView.contentSize = CGSizeMake(self.view.frame.width*3, self.view.frame.height)
    self.superView.contentOffset = CGPoint(x:self.view.frame.width,y:0)
    self.superView.pagingEnabled = true
    self.view.addSubview(self.superView)


    //layout the UIVeiws into the master ScrollView
    for i in 0...2{
        var offset = self.view.frame.width * CGFloat(i)
        var pView = UIView()
        pView.frame = CGRectMake(offset, 0, self.view.frame.width, self.view.frame.height)
        pView.backgroundColor = colours[i]
        self.superView.addSubview(pView)
        self.profileViews.append(pView)

    }

    // Add sub Scroll views and images to the Views.
    for (index, view) in enumerate(self.profileViews){
        var scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.frame = CGRectMake(10, 10, self.view.frame.width-20, self.view.frame.height-20)
        scrollView.pagingEnabled = true
        scrollView.contentSize = CGSizeMake(scrollView.frame.width, scrollView.frame.height * CGFloat(images[index].count))
        for (index2, image) in enumerate(images[index]){
            var subImage = UIImageView()
            subImage.frame = CGRectMake(0, scrollView.frame.height * CGFloat(index2), scrollView.frame.width, scrollView.frame.height)
            subImage.contentMode = UIViewContentMode.ScaleAspectFit
            subImage.image = UIImage(named: image as! String)
            scrollView.addSubview(subImage)
        }
        view.addSubview(scrollView)
        self.scrollViews.append(scrollView)


    }
}

//Use the did end decelerating as it executes the code once the scoll has finished moving.
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    if(scrollView == self.superView){
        var contentOffset = scrollView.contentOffset
        var pageWidth = self.superView.frame.width
        var fractionalPage:Double = Double(self.superView.contentOffset.x / pageWidth)
        var page = lround(fractionalPage)


        // In this example I take the last UIView from the stack and move it to the first.
        // I would do the same in the real app but update the contents of the view after
        if(page == 0){
            var tempView = self.profileViews[2]
            self.profileViews[2].removeFromSuperview()
            self.profileViews.removeAtIndex(2)
            for view in self.profileViews{

                view.frame  = CGRectMake(view.frame.minX + self.view.frame.width, 0, view.frame.width, view.frame.height)
                println(view.frame)
            }
            tempView.frame = CGRectMake(0, 0, tempView.frame.width, tempView.frame.height)
            self.profileViews.insert(tempView, atIndex: 0)
            self.superView.addSubview(tempView)
            var newOffset = contentOffset.x + pageWidth

            self.superView.contentOffset = CGPoint(x: newOffset, y: 0)

        }
        // Take the first view and move it to the last.
        if(page == 2){
            var tempView = self.profileViews[0]
            self.profileViews[0].removeFromSuperview()
            self.profileViews.removeAtIndex(0)
            for view in self.profileViews{

                view.frame  = CGRectMake(view.frame.minX - self.view.frame.width, 0, view.frame.width, view.frame.height)
                println(view.frame)
            }
            tempView.frame = CGRectMake(tempView.frame.width*2, 0, tempView.frame.width, tempView.frame.height)
            self.profileViews.append(tempView)
            self.superView.addSubview(tempView)
            var newOffset = contentOffset.x - pageWidth

            self.superView.contentOffset = CGPoint(x: newOffset, y: 0)

        }

    }
}

关于ios - 何时使用 UITouch 与 UIScroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488317/

相关文章:

objective-c - 关闭 AlertView 时显示新 View

ios - iOS 中具有多个 TableView 的屏幕的容器 View ?

ios - 导航 Controller 在 UISplitViewController 中消失

ios - 应用程序运行时在后台下载和刷新数据

ios - CollectionView 嵌套在 Collectionview 中

ios - 使用 Storyboard的 "inner" View 和 UITableViewCell 的内容 View 之间剩余垂直间距的原因是什么?

ios - 尝试使用 Swift 在 UITableView 的节标题的 UIView 内设置 UILabel 的文本

ios - Swift MVC : Having viewController observe Model property, 在属性更改时更新 View

ios - 设置委托(delegate)以将数据从一个选项卡传递到另一个选项卡

swift - 为什么可选类型中需要两个感叹号不展开?