program received signal exc_bad_access at scrollViewDidScroll - iphone

In my app I tried to do a scrollView with a pageController. The pageController is working but the scrollView is not working. When I try to go to next page I always get program received signal exc_bad_access at line [self loadScrollViewWithPage:page];
from method scrollViewDidScroll
I can't see where is my mystake. Please look at this code...
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.contentSize = CGSizeMake(vosvehiculeScrollView.frame.size.width * pageControlVehiculePossedee.numberOfPages, vosvehiculeScrollView.frame.size.height);
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
for (int i=0; i<pageControlVehiculePossedee.numberOfPages;i++){
[self loadScrollViewWithPage:i];
}
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
CGRect frame = vosvehiculeScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
viewVehicules.frame = frame;
[vosvehiculeScrollView addSubview:viewVehicules];
NSLog(#"%d",page);
}
- (void) scrollViewDidScroll: (UIScrollView *) sender {
if (pageControlUsed) {
return;
}
CGFloat pageWidth = vosvehiculeScrollView.frame.size.width;
int page = floor((vosvehiculeScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControlVehiculePossedee.currentPage = page;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView {
pageControlUsed = NO;
}
- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {
pageControlUsed = NO;
}
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
CGRect frame;
frame.origin.x = self.vosvehiculeScrollView.frame.size.width * self.pageControlVehiculePossedee.currentPage;
frame.origin.y = 0;
frame.size = self.vosvehiculeScrollView.frame.size;
[self.vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}

Related

How do I change the current page for Ray Wenderlich's UIScrollView Tutorial?

I am using Ray Wenderlich's (Matt Galloway's) tutorial on UIScrollViews:
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
I am using the section, "Paging with UIScrollView" in my app. This works great when you always want the page to load on 0, but I would like the current page to be dynamic. I will be passing this information in based on which thumbnail the user picked from the previous view controller, which is an NSInteger called "listIndex."
How do I change the current page to my listIndex? I have tried changing
self.pageControl.currentPage = 0;
to
self.pageControl.currentPage = self.listIndex;
in viewDidLoad, but this doesn't seem to help - possibly because it's getting reset in loadVisiblePages?
Thank you so much for the help! Here's the code:
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableString *myHTML = [[NSMutableString alloc] initWithString:#"<html><head><style type='text/css'> p {font-family:sans-serif; text-shadow: 3px 4px 5px #000; color:#FFF; position: absolute; bottom:0; font-size:10pt; font-weight:bold; margin-bottom:5px;}</style><body><p>"];
[myHTML appendString:self.caption];
[myHTML appendString:#"</p></body></html>"];
[captionWebView loadHTMLString:myHTML baseURL:nil];
self.pageImages = [NSMutableArray array];
for (int i=0; i < [imageList count]; i++) {
NSString *path = [(Image *)[imageList objectAtIndex:i] path];
[self.pageImages addObject:[UIImage imageNamed:path]];
}
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
//self.pageControl.currentPage = self.listIndex; //Tried this
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.frame = CGRectMake(0, 0, 320, 468);
} else {
self.scrollView.frame = CGRectMake(0, 0, 320, 380);
}
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}
- (void)viewWillDisappear:(BOOL)animated {
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.contentSize = CGSizeMake(320, 468);
} else {
self.scrollView.contentSize = CGSizeMake(320, 380);
}
}
Try setting the scrollview's initial contentOffset in viewWillAppear::
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the content size of the scroll view
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// Set initial content offset of scrollview
self.scrollView.contentOffset = CGPointMake(pagesScrollViewSize.width * self.listIndex, self.scrollView.contentOffset.y);
// Load the initial set of pages that are on screen
[self loadVisiblePages];
}

vertical images in UIScrollView

I tried to create a list of UIimages in a UIScrollview. When I implement it horizontally it works the way I want like:
As you see the images stick together.
Then I tried it vertically. it works but an unwanted space between the images happens like:
This is my code:
#synthesize scrollView, pageControl;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
NSArray *pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.png"],
[UIImage imageNamed:#"photo2.png"],
[UIImage imageNamed:#"photo3.png"],
[UIImage imageNamed:#"photo4.png"],
[UIImage imageNamed:#"photo5.png"],
nil];
for (int i = 0; i < pageImages.count; i++) {
CGRect frame;
//frame.origin.x = self.scrollView.frame.size.width * i;
//frame.origin.y = 0;
frame.origin.x = 0;
frame.origin.y = self.scrollView.frame.size.height * i;
frame.size = self.scrollView.frame.size;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
}
//self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * pageImages.count , self.scrollView.frame.size.height);
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width , self.scrollView.frame.size.height* pageImages.count);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageImages.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.height;
int page = floor((self.scrollView.contentOffset.y - pageWidth / 2) / pageWidth) + 1;
//CGFloat pageWidth = self.scrollView.frame.size.width;
//int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = 0;
frame.origin.y = self.scrollView.frame.size.height * self.pageControl.currentPage;
// frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
// frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
#end
in the code the horizontal code lines are commented.
Can you help me to remove this unwanted space in vertical one?
Here the image size makes the problem.
Check with this newPageView.contentMode = UIViewContentModeScaleAspectFill; instead of newPageView.contentMode = UIViewContentModeScaleAspectFit;
edit this part of your code
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
newPageView.contentMode = UIViewContentModeScaleAspectFit; // this statement is making your newPageView bigger then the image, hence spaces are coming when scrolling. !!
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
dynamically create your view "newPageView" such that the view is of exact size with your images.

Change Page Control With Button

I have a simple page control set up with two different pages. I have a button called nextButton, and if the user hits that button, I want to change the page and scroll to the next page.
My page control works, and I can scroll to change pages, but the button does not work at all. It is getting called though.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * TUTORIALNUMBEROFPAGES, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
self.pageControl.numberOfPages = TUTORIALNUMBEROFPAGES;
self.pageControl.currentPage = 0;
CGRect pageFrame = self.scrollView.frame;
pageFrame.origin.x = pageFrame.size.width * 0;
pageFrame.origin.y = 0;
self.pageOne.frame = pageFrame;
[self.scrollView addSubview:self.pageOne];
pageFrame.origin.x = pageFrame.size.width * 1;
self.pageTwo.frame = pageFrame;
[self.scrollView addSubview:self.pageTwo];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed) {
return;
}
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
NSInteger currentPage = self.pageControl.currentPage;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
[self changePage:self];
}
Try something like this:
- (IBAction)nextPage:(id)sender
{        
    NSInteger currentPage = self.pageControl.currentPage + 1;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
  [self nextPage:self];
}
Notice the +1 in that code. Before, you were literally setting the content offset exactley equal to the original offset because you were just taking the value of the page control.
You can try with this:
- (IBAction)changePage:(id)sender
{
NSInteger currentPage = self.pageControl.currentPage;
if([sender isKindOfClass:[UIButton class]){
currentPage+=1;
}
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
[self changePage:sender];//if this action is attached just to your button
}

UIScroll View next previous button

I have a scroll view. Inside scroll view, I have image view. I want to go next and previous image by button click. In below it works on only three images of array not more than that so how to work for all. I have 70 images in array
- (IBAction)nextButtonAction {
int arraycount = [appDelegate.articles count];
NSLog(#" arraycount : %d", arraycount);
NSLog(#" [pageControl currentPage] : %d", [pageControl currentPage]);
for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); 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{
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)nextButtonAction {
int arraycount = [appDelegate.articles count];
if ( self.scrollView.contentOffset.x < (self.scrollView.frame.size.width *arraycount) ) {
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)btn_NextPressed:(id)sender
{
CGFloat pageWidth = self.scrollview.frame.size.width;
intPage = (NSInteger)floor((self.scrollview.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
[self.scrollview setContentOffset:CGPointMake(self.scrollview.contentOffset.x + self.scrollview.contentSize.width/numberOfPages ,0) animated:YES];
}
- (IBAction)btn_PrevPressed:(id)sender
{
CGFloat pageWidth = self.scrollview.frame.size.width;
intPage = (NSInteger)floor((self.scrollview.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
[self.scrollview setContentOffset:CGPointMake(self.scrollview.contentOffset.x - self.scrollview.contentSize.width/numberOfPages ,0) animated:YES];
}

How can i provide Zoomin-Zoomout,Paging functionality in to UIScrollView?

I have an array of Image URl's from that array i have displayed my Images in Scrollview because i want to take the advantage of Paging and zooming so what should i do to achieve this task?suppose my array contains 10 URL then by each time scrolling i should get different image and i could also performing Zooming-In and Zooming-Out any of the 10 Images so please give me some guidelines to achieve my task.Thanks in Advance.
Try this
- (void)setCurrentPage:(NSUInteger)page {
if (page == offsetX)
return;
offsetX = page;
// in a real app, this would be a good place to instantiate more view controllers -- see SDK examples
}
- (CGSize)pageSize {
CGSize pageSize = scrollView1.frame.size;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
return CGSizeMake(pageSize.height, pageSize.width);
else
return pageSize;
}
- (void)setPagingMode {
// reposition pages side by side, add them back to the view
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imgViewArray) {
if (!view.superview)
[scrollView1 addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView1.pagingEnabled = YES;
scrollView1.showsVerticalScrollIndicator = scrollView1.showsHorizontalScrollIndicator = NO;
scrollView1.contentSize = CGSizeMake(pageSize.width * [imgViewArray count], pageSize.height);
scrollView1.contentOffset = CGPointMake(pageSize.width * offsetX, 0);
scrollViewMode = ScrollViewModePaging;
}
- (void)setZoomingMode {
NSLog(#"setZoomingMode");
scrollViewMode = ScrollViewModeZooming; // has to be set early, or else currentPage will be mistakenly reset by scrollViewDidScroll
// hide all pages except the current one
NSUInteger page = 0;
for (UIView *view in imgViewArray)
if (offsetX != page++)
[view removeFromSuperview];
scrollView1.pagingEnabled = NO;
scrollView1.showsVerticalScrollIndicator = scrollView1.showsHorizontalScrollIndicator = YES;
pendingOffsetDelta = scrollView1.contentOffset.x;
scrollView1.bouncesZoom = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// offsetX=(aScrollView.contentOffset.x)/768;
//NSLog(#"Page No:%d",offsetX);
if (scrollViewMode == ScrollViewModePaging)
[self setCurrentPage:roundf(scrollView1.contentOffset.x / [self pageSize].width)];
}
else
{
//offsetX=(aScrollView.contentOffset.x)/320;
//NSLog(#"Page No:%d",offsetX);
if (scrollViewMode == ScrollViewModePaging)
[self setCurrentPage:roundf(scrollView1.contentOffset.x / [self pageSize].width)];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView
{
NSLog(#"viewForZoomingInScrollView");
if (scrollViewMode != ScrollViewModeZooming)
[self setZoomingMode];
return [imgViewArray objectAtIndex:offsetX];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)aScrollView withView:(UIView *)view atScale:(float)scale {
NSLog(#"scrollViewDidEndZooming");
if (scrollView1.zoomScale == scrollView1.minimumZoomScale)
[self setPagingMode];
else if (pendingOffsetDelta > 0) {
UIView *view = [imgViewArray objectAtIndex:offsetX];
view.center = CGPointMake(view.center.x - pendingOffsetDelta, view.center.y);
CGSize pageSize = [self pageSize];
scrollView1.contentOffset = CGPointMake(scrollView1.contentOffset.x - pendingOffsetDelta, scrollView1.contentOffset.y);
scrollView1.contentSize = CGSizeMake(pageSize.width * scrollView1.zoomScale, pageSize.height * scrollView1.zoomScale);
pendingOffsetDelta = 0;
}
}