EDIT: i tried this code in a new project and it works as expected. the following code is used on top of a opengl view (using cocos2d) added with [[[CCDirector sharedDirector]openGLView]addSubview:lsvc.view]; where lsvc is my viewcontroller with t开发者_开发百科he scrollview.
i have a strange behaviour with my scrollview, when i try to programatically scroll the scrollview all works fine in the simulator (xcode4, 4.3 simul) but is not working on my device (4.3.2)
to scroll i do within a pageControl event (on value change) the following
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
[self.scrollView scrollRectToVisible:frame animated:YES];
and this is how i initialize the scrollview in viewdidload
self.scrollView.delegate = self;
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
self.scrollView.pagingEnabled = YES;
self.scrollView.scrollsToTop = NO;
int page = 0;
int pageNum = 3;
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*pageNum, self.scrollView.frame.size.height)];
for (; page < pageNum; page++) {
LevelViewController *lvlvc = [[LevelViewController alloc] init];
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
lvlvc.view.frame = frame;
[self.scrollView addSubview:lvlvc.view];
// add the viewcontroller to a saved array to keep track
[levelsController addObject:lvlvc];
[lvlvc release];
}
self.pageControl.numberOfPages = page;
Scrolling the scrollview by panning on the device works great, tapping on the pageControl sometimes moves the scroll a little (like 20 points or less) and the rest of the time it doesnt scroll at all. on the simulator all works fine. can someone point on a direction, on what to check because i m lost, i dont know why i get 2 different behaviors on simulator and on device i have also tried
[self.scrollView setContentOffset:CGPointMake(frame.size.width * pageNum, 0) animated:YES];
and this is also working only on the simulator
I had the same issue on my latest project. The solution was to drop the framerate to 30 fps, then do the scrollRectToVisible: then set it back to 60 fps.
...
[[CCDirector sharedDirector] setAnimationInterval:1.0/30.0];
[scrollView scrollRectToVisible:rect animated:YES];
...
then in the scrollView delegate:
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[[CCDirector sharedDirector] setAnimationInterval:1.0/60.0];
}
I did not set it back in the delegate call, because I hade a level select screen with the scrollview, so entering the given screen I dropped the framerate (there wasn't any animation there after all), and leaving the screen I restored the 60 fps.
Hope I could help!
I your for in viewDidLoad add tag to view that you add in scroll view before the addSubview
lvlvc.viw.tag = page+100;
self.scrollView addSubview:lvlvc.view];
And your pageControl event should look like this
UIView *viewToScroll = [self.scrollView viewWithTag: yorPageControl.currentPage+100];
[self.scrollView scrollRectToVisible:viewToScroll.frame animated:YES];
The idee is to set for each view that you add in your scroll view an unique tag. And when the page control is change get the current view for the currentPage and tell to the scroll view to scroll to the corespondent rect.
Good luck:D
I've run into this same problem and the only solution I could come up with is to change the scrollRectToVisible animated property to NO. This isn't optimal but it'll get it to work.
I've checked many other things (memory warnings, scrollview settings, setContentOffset()), read through dozens of SO posts similar to this and no solution was found. The scrollRectToVisible call is never received by the view. I set up scrollViewDidScroll and I see it called when I swipe but the scrollRectToVisible call does not trigger it. The only thing I found was if you set a breakpoint at scrollRectToVisible and then continue, it tends to work more often than not. So it might be some timing bug with UIScrollView API animation.
It wasn't the case above, but in my case it worked on the simulator when I used tableView.scrollToRow
, but it didn't work on the device.
It worked when implemented as setContentOffset(:animated:)
, scrollRectToVisible(:animated:method)
.
I found it here
精彩评论