使用按钮手动滚动分页的UIScrollView

Wri*_*sCS 4 iphone uibutton uiscrollview

我有一个UIScrollView,它有10个来自数组的图像.我需要使用按钮向左或向右滚动到下一个或上一个图像

button scrollview button
   <     [ ... ]     >
Run Code Online (Sandbox Code Playgroud)

Wri*_*sCS 9

好吧,这就是我能够使用UIButton正确滚动UIScrollView的方法.这里的IF语句确保scrollview不会超出My NSArray图像的范围.

#pragma mark -
#pragma mark Use a UIButton to scroll a UIScrollView Left or Right

-(IBAction)scrollRight{ 
    if(pos<9){pos +=1;
        [scrollView scrollRectToVisible:CGRectMake(pos*scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
        NSLog(@"Position: %i",pos);
    }
}
-(IBAction)scrollLeft{  
    if(pos>0){pos -=1;
        [scrollView scrollRectToVisible:CGRectMake(pos*scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
        NSLog(@"Position: %i",pos);
    }
}
Run Code Online (Sandbox Code Playgroud)