当我按下上一个按钮时,它不起任何作用。下一步按钮工作正常,但上一个按钮不显示上一张图像。我正在使用scroll view
来加载图片:
- (IBAction)next{
int arraycount = [appDelegate.articles count];
for (int nextIndex = [pageControl currentPage]; nextIndex < arraycount; nextIndex++) {
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}
-(IBAction)previousButtonAction
{
int arraycount = [appDelegate.articles count];
NSLog(@" arraycount : %d", arraycount);
NSLog(@" [pageControl currentPage] : %d", [pageControl currentPage]);
for (int nextIndex = [pageControl currentPage]; nextIndex>arraycount; nextIndex--) {
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}
答案 0 :(得分:4)
好的,我现在认为我会采用不同的方法。我会在重新分配它之前检查当前的偏移量。
未经测试的代码:
- (IBAction)next{
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
答案 1 :(得分:0)
当用户到达第三张图片时,我会禁用“下一步”按钮。如果您希望在用户按下第三张图像中的“下一张”时返回第一张图像,则必须在if后添加其他图像以表示该情况:
- (IBAction)next{
int arraycount = [appDelegate.articles count];
for (int nextIndex = [pageControl currentPage]; nextIndex < arraycount; nextIndex++) {
if (nextIndex < arraycount) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * nextIndex;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
// If the user clicks on next in the last item, return to the first
else {
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
}
答案 2 :(得分:0)
arraycount = 3
[pageControl currentPage]为零索引,因此当您在第3页时它等于2.
所以你的for循环nextIndex的开始等于2,小于arrayCount,所以它进入你的循环。
将其更改为:
for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) {
好的是这个输出:
- (IBAction)next{
int arraycount = [appDelegate.articles count];
NSLog(@" arraycount : %d", arraycount);
NSLog(@" [pageControl currentPage] : %d", [pageControl currentPage]);
for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) {
NSLog(@" nextIndex : %d", nextIndex);
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * nextIndex;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
}
答案 3 :(得分:0)
对我说它在swift 3中工作。
func prevAction (sender:UIButton)
{
print("self.scView.contentOffset.x ..\(self.scView.contentOffset.x)")
if ( self.scView.contentOffset.x <= self.scView.contentSize.width )
{
var frame = CGRect()
frame.origin.x = self.scView.contentOffset.x - 80;
frame.origin.y = 0;
frame.size = self.scView.frame.size;
self.scView .scrollRectToVisible(frame, animated: true)
}
}
感谢@ader。