【发布时间】:2011-04-11 07:11:44
【问题描述】:
我有一个UIScrollView 来显示图像。我需要一个计时器,每 5 秒后一个接一个地显示图像。如何使用定时器发起UIScrollView事件5秒后移动到下一张图片?
【问题讨论】:
标签: iphone uiscrollview
我有一个UIScrollView 来显示图像。我需要一个计时器,每 5 秒后一个接一个地显示图像。如何使用定时器发起UIScrollView事件5秒后移动到下一张图片?
【问题讨论】:
标签: iphone uiscrollview
将变量 int h 添加到您的界面,并将 yourScrollView 作为属性。然后:
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
- (void) onTimer {
// Updates the variable h, adding 100 (put your own value here!)
h += 100;
//This makes the scrollView scroll to the desired position
yourScrollView.contentOffset = CGPointMake(0, h);
}
【讨论】:
[yourScrollView setContentOffset:CGPointMake(0, h) animated:YES];
修改上面的代码
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
-(void) onTimer {
// NSLog(@"content offset %f",scrollVw.contentOffset.y);
if (scrollVw.contentOffset.y<MAX_ALLOWED_OFFSET) {
//scroll to desire position
scrollVw.contentOffset = CGPointMake(0, scrollVw.contentOffset.y+1);
}
}
【讨论】:
获取当前值作为内容的 w(x 值)或 h(y 值)。这有助于手动/半自动滚动 UIScollView 内容中的当前参考点。
CGFloat w = scroller.contentOffset.x;
或
CGFloat h = scroller.contentOffset.y;
当然:
h += 100; // h-=100 for decrement
yourScrollView.contentOffset = CGPointMake(0, h);
【讨论】:
斯威夫特版本
添加以下属性
var offSet: CGFloat = 0
@IBOutlet weak var imagePageControl: UIPageControl!
@IBOutlet weak var imageScrollView: UIScrollView!
let imageArray = [your images]
修改viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
self.offSet = 0
let timer = Timer.scheduledTimer(timeInterval: 8, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)
imagePageControl.numberOfPages = bannerArray.count
imageScrollView.isPagingEnabled = true
imageScrollView.contentSize.height = 200
imageScrollView.contentSize.width = self.view.bounds.width * CGFloat(bannerArray.count)
imageScrollView.showsHorizontalScrollIndicator = false
imageScrollView.delegate = self
for (index, image) in imageArray.enumerated() {
let image = image
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame.size.width = self.view.bounds.size.width
imageView.frame.size.height = 200
imageView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
imageScrollView.addSubview(imageView)
}
}
为自动滚动创建此功能
func autoScroll() {
let totalPossibleOffset = CGFloat(imageArray.count - 1) * self.view.bounds.size.width
if offSet == totalPossibleOffset {
offSet = 0 // come back to the first image after the last image
}
else {
offSet += self.view.bounds.size.width
}
DispatchQueue.main.async() {
UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.imageScrollView.contentOffset.x = CGFloat(self.offSet)
}, completion: nil)
}
}
为手动滚动添加此功能
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
imagePageControl.currentPage = Int(page)
self.offSet = page * scrollView.frame.size.width // this updates offset value so that automatic scroll begins from the image you arrived at manually
}
注意:虽然问题是针对自动滚动的,但我也添加了手动滚动。这样自动滚动和手动滚动都可以协同工作。
另外,我添加了一个UIPageControl。如果您不需要,请删除imagePageControl。
【讨论】:
也许,[scroll scrollRectToVisibile:animated:] 比 [scroll setContentOffset] 更好。 因为 setContentOffset 会导致滚出边缘。 试试看吧!
【讨论】: