可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a UIScrollView with set of images with left and right button. I have implement the left button action for moving the image left side using this code
if ( scroll.contentOffset.x <= scroll.frame.size.width ) {
CGRect frame;
frame.origin.x = scroll.contentOffset.x + scroll.frame.size.width;
frame.origin.y = 0;
frame.size = scroll.frame.size;
//NSLog(@"%f %f %f", frame.origin.y ,scroll.frame.size.width, frame.origin.x);
[scroll scrollRectToVisible:frame animated:YES];
}
its working perfectly
same i implement the functionality for right button action not working properly what i am doing wrong the code is below
if ( scroll.frame.size.width <= scroll.contentOffset.x ) {
CGRect frame;
frame.origin.x = scroll.contentOffset.x - scroll.frame.size.width;
frame.origin.y = 0;
frame.size = scroll.frame.size;
[scroll scrollRectToVisible:frame animated:YES];
}
回答1:
MMm.. I would suggest you to do this in another way. Create two button and separate action method rightArrowNavigation, leftArrowNavigation . Add a global int value here in my code currentPage , and in viewDidLoad assign 0. See below
- (IBAction)rightArrowNavigation:(id)sender {
CGRect bounds = self.scrollView.bounds;
bounds.origin.x = CGRectGetWidth(bounds) * (currentPage + 1);
[self.scrollView scrollRectToVisible:bounds animated:YES];
}
- (IBAction)leftArrowNavigation:(id)sender {
CGRect bounds = self.scrollView.bounds;
bounds.origin.x = CGRectGetWidth(bounds) * (currentPage - 1);
[self.scrollView scrollRectToVisible:bounds animated:YES];
}
回答2:
it should be opposite of the left, check: if it is greater than, and subtracting the scrollview width from the current x position
if ( scroll.contentOffset.x >= scroll.frame.size.width ) {
CGRect frame;
frame.origin.x = scroll.contentOffset.x - scroll.frame.size.width;
frame.origin.y = 0;
frame.size = scroll.frame.size;
//NSLog(@"%f %f %f", frame.origin.y ,scroll.frame.size.width, frame.origin.x);
[scroll scrollRectToVisible:frame animated:YES];
}
回答3:
For right button, why are you checking for contentOffset.y? It should be contentOffset.x, also is there paging enabled?
Use this, I think it will solve
if ( _scroll.contentOffset.x >= _scroll.frame.size.width/2 ) {
CGRect frame;
frame.origin.x = _scroll.contentOffset.x - _scroll.frame.size.width;
frame.origin.y = 0;
frame.size = _scroll.frame.size;
//NSLog(@"%f %f %f", frame.origin.y ,scroll.frame.size.width, frame.origin.x);
[_scroll scrollRectToVisible:frame animated:YES];
}
回答4:
Th left side functioanlity is correct whereas for the right side functionality assuming you want to create horizontal scroller, you should work with x origin and width. Your conditions and code for right side should be based on x origin, width, basically all those parameters which are responsible for horizontal scrolling
回答5:
With scrollview paging enabled. If you don't want paging enabled try some more mathematics.
(IBAction)leftButtonClicked:(id)sender {
if (self.iTemScrollView.contentOffset.x <
self.iTemScrollView.contentSize.width - self.iTemScrollView.frame.size.width){
[self.iTemScrollView setContentOffset:CGPointMake(self.iTemScrollView .contentOffset.x + self.iTemScrollView .frame.size.width, 0) animated:YES];
}
}
(IBAction)rightButtonClicked:(id)sender {
NSLog(@" rightbutton self.iTemScrollView.contentOffset.x %f",self.iTemScrollView.contentOffset.x);
NSLog(@"rightbutton self.iTemScrollView.contentOffset.y %f",self.iTemScrollView.contentOffset.y);
if (self.iTemScrollView.contentOffset.x>0){
[self.iTemScrollView setContentOffset:CGPointMake(self.iTemScrollView .contentOffset.x - self.iTemScrollView .frame.size.width, 0) animated:YES];
}
}
回答6:
-(IBAction)leftScroll:(id)sender
{
//page variable initialize to 0 in view did load method
if(page!=0)
{
CGRect frame = scrollEvent.frame;
frame.origin.x = frame.size.width * (--page);
frame.origin.y = 0;
[scrollEvent scrollRectToVisible:frame animated:YES];
}
}
- (IBAction)rightScroll:(id)sender
{
if(arrData.count>page)
{
CGRect frame = scrollEvent.frame;
frame.origin.x = frame.size.width * (++page);
frame.origin.y = 0;
[scrollEvent scrollRectToVisible:frame animated:YES];
}
}