Ios 如何水平自动滚动滚动视图?
我使用此代码在滑动scrollview时水平滚动scrollviewIos 如何水平自动滚动滚动视图?,ios,swift,autoscroll,Ios,Swift,Autoscroll,我使用此代码在滑动scrollview时水平滚动scrollview func getScrollView() { upperScroll.delegate = self upperScroll.isPagingEnabled = true // pageControll.numberOfPages = logoImage.count upperScroll.isScrollEnabled = true
func getScrollView() {
upperScroll.delegate = self
upperScroll.isPagingEnabled = true
// pageControll.numberOfPages = logoImage.count
upperScroll.isScrollEnabled = true
let scrollWidth: Int = Int(self.view.frame.width)
upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
print(upperScroll.frame.size.width)
upperScroll.backgroundColor = UIColor.clear
for index in 0..<logoImage.count {
let xPosition = self.view.frame.width * CGFloat(index)
upperScroll.layoutIfNeeded()
let img = UIImageView(frame: CGRect(x: CGFloat(xPosition), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
img.layoutIfNeeded()
let str = logoImage[index]
let url = URL(string: str)
img.sd_setImage(with: url, placeholderImage: UIImage(named:"vbo_logo2.png"))
upperScroll.addSubview(img)
}
view.addSubview(upperScroll)
upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth) * logoImage.count), height: self.upperScroll.frame.height)
}
func getScrollView(){
upperScroll.delegate=self
upperScroll.isPaginEnabled=true
//pageControll.numberOfPages=logoImage.count
upperScroll.IsCrollenabled=true
让滚动宽度:Int=Int(self.view.frame.width)
upperScroll.contentSize=CGSize(宽度:CGFloat(scrollWidth),高度:(self.upperScroll.frame.height))
打印(上卷轴、边框、大小、宽度)
upperScroll.backgroundColor=UIColor.clear
对于0.中的索引
创建要传递到func的点时,可以指定x轴:
CGPoint(x:100,y:0)
在scrollView
中有方法可以滚动查看可见的rect
。您需要在方法中传递CGRect
,这意味着您要滚动的区域
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
那么:
let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: (#selector(ViewController.timerAction)), userInfo: nil, repeats: true)
@objc func timerAction() {
var xOffSet = 0
xOffSet += 10
UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.scrollView.contentOffset.x = xOffSet
}, completion: nil)
}
我找到了解决办法
@objc func animateScrollView() {
let scrollWidth = upperScroll.bounds.width
let currentXOffset = upperScroll.contentOffset.x
let lastXPos = currentXOffset + scrollWidth
if lastXPos != upperScroll.contentSize.width {
print("Scroll")
upperScroll.setContentOffset(CGPoint(x: lastXPos, y: 0), animated: true)
}
else {
print("Scroll to start")
upperScroll.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.animateScrollView), userInfo: nil, repeats: true)
}
并在ViewDidDisplay中调用此函数当您说autoscroll时,您的意思是滚动到滚动视图中的某个特定点?我想在特定时间自动滚动,如10秒后?@aatishmolasiven如果您使用upperScroll.setContentOffset(点/*您想滚动到的地方*/,动画:true)
单独?我们将在何处调用timer.fire()@KowbojviewDidLoad?无论您希望它在何处启动[\u SwiftValue timerAction]:未识别的选择器发送到实例0x6000065fce0'我在viewDidLoad中编写此代码时收到此错误请勿使用.fire(),抱歉。只需将其放在viewDidLoad中,并添加“ViewController”这里:let timer=timer.scheduledTimer(timeInterval:2,target:self,selector:(#selector(ViewController.timerAction)),userInfo:nil,repeats:true)做得很好。我在.width之后添加了advanced(by:)并对时间间隔进行了调整,现在它滑动得很好
@objc func animateScrollView() {
let scrollWidth = upperScroll.bounds.width
let currentXOffset = upperScroll.contentOffset.x
let lastXPos = currentXOffset + scrollWidth
if lastXPos != upperScroll.contentSize.width {
print("Scroll")
upperScroll.setContentOffset(CGPoint(x: lastXPos, y: 0), animated: true)
}
else {
print("Scroll to start")
upperScroll.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.animateScrollView), userInfo: nil, repeats: true)
}