我正在使用Swift 3在iPhone中開發一個簡單的商業應用程序,而我是開發新手。我有一個ViewController與一個UIView,通過圖像與UIPageControl循環。我也在同一個ViewController中有標籤和水平滾動的UICollectionViews。我使用Storyboard配置了我的滾動視圖,並在代碼中配置了UIPageControl。如何在兩個自定義TableViewCells中顯示UIView圖像幻燈片和CollectionViews?
問題:當我將滾動視圖嵌入到ViewController中時,UIPageControl和CollectionViewCells保持在同一位置。
我在這裏有一個示例。請參閱圖片以獲得更好的理解:Image showing my ViewController where the PageControl stays same while the View is scrolled和另一張圖片ViewController while scrolling up。
所以,我發現scrollview與我的CollectionView和PageControl滾動發生衝突。
有人可以告訴我如何可以嵌入我的UIView滑動圖像在自定義TableViewCell和其他自定義TableViewCell中的CollectionViews,以啓用垂直滾動?
這裏是我的ViewController.swift:
import UIKit
class Home: UIViewController{
@IBOutlet var myView: UIView!
@IBOutlet var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
self.addSlideMenuButton()
self.title="Home"
//Image Loop
self.pageControl.currentPage = -1
configurePageControl()
slide = -1
self.changeSlide()
var timer = Timer.scheduledTimer(timeInterval: 10
, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true);
myView.isUserInteractionEnabled = true
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swiped(_gesture:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
myView.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped(_gesture:)))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
myView.addGestureRecognizer(swipeLeft)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
var images = [UIImage(named:"banner2"),UIImage(named:"banner1"),UIImage(named:"banner3"),UIImage(named:"banner4"),UIImage(named:"banner5")]
var slide:Int = 0
func changeSlide(){
var loop : UIImageView!
loop = UIImageView(frame: myView.bounds)
loop.contentMode = UIViewContentMode.scaleToFill
if(slide == images.count-1)
{
slide=0
loop.image = images[slide]
self.pageControl.currentPage=slide
}
else
{
slide+=1
loop.image = images[slide]
self.pageControl.currentPage=slide
}
loop.layer.affineTransform()
myView.addSubview(loop)
}
@IBAction func pageChange(_ sender: AnyObject) {
}
func swiped(_gesture: UIGestureRecognizer) {
var loop : UIImageView!
loop = UIImageView(frame: myView.bounds)
loop.contentMode = UIViewContentMode.scaleToFill
loop.image = images[slide]
myView.addSubview(loop)
if let swipeGesture = _gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
// decrease index first
// check if index is in range
if (slide == 0) {
slide = images.count-1
loop.image = images[slide]
self.pageControl.currentPage = slide
}
else
{
slide-=1
loop.image = images[slide]
self.pageControl.currentPage = slide
}
case UISwipeGestureRecognizerDirection.left:
// increase index first
// check if index is in range
if (slide == images.count-1) {
slide = 0
loop.image = images[slide]
self.pageControl.currentPage = slide
}
else{
slide+=1
loop.image = images[slide]
self.pageControl.currentPage = slide
}
default:
break //stops the code/codes nothing.
}
}
}
func configurePageControl() {
self.pageControl.numberOfPages = images.count
self.pageControl.currentPage = slide
self.pageControl.tintColor = UIColor.red
self.pageControl.pageIndicatorTintColor = UIColor.gray
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.myView.addSubview(pageControl)
self.view.addSubview(pageControl)
}
}
這包含UIView的圖像循環和UIPageControl還通過圖像刷卡選項代碼。
有人可以幫我嗎?提前致謝。
嘿謝謝你的回覆。是的,你的回答有幫助但是,我對此很陌生並且有些疑惑。什麼是MyIndicator和DetailsViewViewModel!爲什麼使用它?你也可以發佈完整的代碼,讓我更好的理解?特別是用於textCell和starRatingCell的TableViewCell類代碼。 –
txtCell是tableView其餘部分的文本單元格,starRatingCell是我的電影應用程序的評分單元格。 txtCell單元格與MovieCollectionViewCell相同,區別在於它的tableViewCell和UIImageView不同,它們都是UIlabel。 代碼簽出[我即將上映的電影項目](https://github.com/PravinNagargoje/movies) –