我想在swift 3中實現自動滾動到UIScrollView。任何人都可以幫助我。如何使用Swift 3在UIScrollview中實現自動滾動?
其實我是iOS開發新手。
我的代碼:
var bannerscView: UIScrollView!
bannerscView = UIScrollView(frame: CGRect(x: 0, y: 50, width: Int(view.bounds.width), height: 350))
bannerscView.showsVerticalScrollIndicator = true
view.addSubview(bannerscView)
bannerscView.backgroundColor = UIColor.red
bannerscView.translatesAutoresizingMaskIntoConstraints = false
_ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(bannerImgScroll), userInfo: nil, repeats: true)
func bannerImgScroll(){
var xBannerOffset:CGFloat = 5
for k in 0 ... 5 {
let bannerImg = UIButton()
bannerImg.tag = k
bannerImg.backgroundColor = UIColor.blue //UIColor.darkGray
bannerImg.setTitle("\(k)", for: .normal)
//button.addTarget(self, action: #selector(btnTouch), for: UIControlEvents.touchUpInside)
bannerImg.frame = CGRect(x: xBannerOffset, y: CGFloat(buttonPadding), width: 400, height: 300)
xBannerOffset = xBannerOffset + CGFloat(buttonPadding) + bannerImg.frame.size.width
// print("xOffset After : \(xOffset)")
bannerscView.addSubview(bannerImg)
}
bannerscView.contentSize = CGSize(width: xBannerOffset, height: bannerscView.frame.height)
}
由於提前
add more detai LS。 – Maddy
看到答案,你可以使用自動滾動計時器 – Dhiru
' bannerscView.addSubview(bannerImg)'爲什麼你每次加入SubView時間間隔? – Dhiru