何时使用 UITouch 与 UIScroll
Posted
技术标签:
【中文标题】何时使用 UITouch 与 UIScroll【英文标题】:When to use UITouch vs UIScroll 【发布时间】:2015-07-18 06:05:01 【问题描述】:我想实现您在约会应用中看到的设计。您可以在其中垂直滚动个人资料的图像,也可以水平滚动以查看列表中的下一个或上一个人。
目前我的观点是这样的。
Previous-UIView - current UIView - next UIView
UIScrollView. UIScrollView. UIScrollView
Images. Images. Images
UIView. UIView. UIView
Profile info. Profile info. Profile info
UIPageControl. UIPageControl UIPageControl.
只有一个视图占据主视图,下一个和上一个在屏幕外。理想情况下,当用户向左移动视图时,我会以编程方式删除前一个视图,将当前视图设为前一个,下一个当前并为下一个添加新视图。右移反之亦然。
水平滚动视图的最佳方式是什么?
我应该将它们全部包装在 UIScrollView 中吗?这会干扰 UIScrollView 子视图吗?
或者我应该对触摸控件进行编程以移动视图吗?
或者有没有更好的方法?
我仍然是 ios 开发的新手,因此我们将不胜感激。
【问题讨论】:
基于页面的滚动视图可能会有所帮助。 raywenderlich.com/76436/… 本教程很棒,谢谢。我想最大的问题是你能在 Scrollviews 里面有 Scrollviews 吗? 我建议你避免嵌套滚动视图,包括嵌套使用滚动视图构建的东西,如表格视图。 【参考方案1】:所以我尝试了一些测试应用程序,我很高兴地说你可以在 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)
【讨论】:
以上是关于何时使用 UITouch 与 UIScroll的主要内容,如果未能解决你的问题,请参考以下文章
与 UIGestureRecognizer 一起监听 UITouch 事件